1
0
mirror of synced 2024-11-25 22:36:03 +03:00

Use the star event for internal binding

The star event will allow us to relay any custom events as well as
the ones used internally by Select2. This also makes it much
easier to start triggering events in general, as we don't need to
relay it in the core anymore.

A module has been created for the jQuery-facing events, and the
release announcement has been updated to mention the event name
changes.

This also adds some documentation about the public events.
This commit is contained in:
Kevin Brown 2015-01-08 10:40:13 -05:00
parent 68286e3cff
commit bba967ba21
9 changed files with 113 additions and 161 deletions

View File

@ -3846,13 +3846,14 @@ define('select2/core',[
Select2.prototype._registerDataEvents = function () { Select2.prototype._registerDataEvents = function () {
var self = this; var self = this;
this.data.on('results:message', function (params) { this.data.on('*', function (name, params) {
self.trigger('results:message', params); self.trigger(name, params);
}); });
}; };
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['open', 'close', 'toggle', 'unselected'];
this.selection.on('open', function () { this.selection.on('open', function () {
self.open(); self.open();
@ -3864,49 +3865,32 @@ define('select2/core',[
self.toggleDropdown(); self.toggleDropdown();
}); });
this.selection.on('results:select', function () {
self.trigger('results:select');
});
this.selection.on('results:previous', function () {
self.trigger('results:previous');
});
this.selection.on('results:next', function () {
self.trigger('results:next');
});
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.close(); self.close();
}); });
this.selection.on('query', function (params) { this.selection.on('*', function (name, params) {
self.trigger('query', params); if (nonRelayEvents.indexOf(name) !== -1) {
}); return;
}
this.selection.on('keypress', function (e) { self.trigger(name, params);
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerDropdownEvents = function () { Select2.prototype._registerDropdownEvents = function () {
var self = this; var self = this;
this.dropdown.on('query', function (params) { this.dropdown.on('*', function (name, params) {
self.trigger('query', params); self.trigger(name, params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['selected', 'unselected'];
this.results.on('query:append', function (params) {
self.trigger('query:append', params);
});
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
@ -3920,8 +3904,12 @@ define('select2/core',[
self.close(); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('*', function (name, params) {
self.trigger('results:focus', params); if (nonRelayEvents.indexOf(name) !== -1) {
return;
}
self.trigger(name, params);
}); });
}; };

View File

@ -3846,13 +3846,14 @@ define('select2/core',[
Select2.prototype._registerDataEvents = function () { Select2.prototype._registerDataEvents = function () {
var self = this; var self = this;
this.data.on('results:message', function (params) { this.data.on('*', function (name, params) {
self.trigger('results:message', params); self.trigger(name, params);
}); });
}; };
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['open', 'close', 'toggle', 'unselected'];
this.selection.on('open', function () { this.selection.on('open', function () {
self.open(); self.open();
@ -3864,49 +3865,32 @@ define('select2/core',[
self.toggleDropdown(); self.toggleDropdown();
}); });
this.selection.on('results:select', function () {
self.trigger('results:select');
});
this.selection.on('results:previous', function () {
self.trigger('results:previous');
});
this.selection.on('results:next', function () {
self.trigger('results:next');
});
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.close(); self.close();
}); });
this.selection.on('query', function (params) { this.selection.on('*', function (name, params) {
self.trigger('query', params); if (nonRelayEvents.indexOf(name) !== -1) {
}); return;
}
this.selection.on('keypress', function (e) { self.trigger(name, params);
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerDropdownEvents = function () { Select2.prototype._registerDropdownEvents = function () {
var self = this; var self = this;
this.dropdown.on('query', function (params) { this.dropdown.on('*', function (name, params) {
self.trigger('query', params); self.trigger(name, params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['selected', 'unselected'];
this.results.on('query:append', function (params) {
self.trigger('query:append', params);
});
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
@ -3920,8 +3904,12 @@ define('select2/core',[
self.close(); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('*', function (name, params) {
self.trigger('results:focus', params); if (nonRelayEvents.indexOf(name) !== -1) {
return;
}
self.trigger(name, params);
}); });
}; };

View File

@ -13381,13 +13381,14 @@ define('select2/core',[
Select2.prototype._registerDataEvents = function () { Select2.prototype._registerDataEvents = function () {
var self = this; var self = this;
this.data.on('results:message', function (params) { this.data.on('*', function (name, params) {
self.trigger('results:message', params); self.trigger(name, params);
}); });
}; };
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['open', 'close', 'toggle', 'unselected'];
this.selection.on('open', function () { this.selection.on('open', function () {
self.open(); self.open();
@ -13399,49 +13400,32 @@ define('select2/core',[
self.toggleDropdown(); self.toggleDropdown();
}); });
this.selection.on('results:select', function () {
self.trigger('results:select');
});
this.selection.on('results:previous', function () {
self.trigger('results:previous');
});
this.selection.on('results:next', function () {
self.trigger('results:next');
});
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.close(); self.close();
}); });
this.selection.on('query', function (params) { this.selection.on('*', function (name, params) {
self.trigger('query', params); if (nonRelayEvents.indexOf(name) !== -1) {
}); return;
}
this.selection.on('keypress', function (e) { self.trigger(name, params);
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerDropdownEvents = function () { Select2.prototype._registerDropdownEvents = function () {
var self = this; var self = this;
this.dropdown.on('query', function (params) { this.dropdown.on('*', function (name, params) {
self.trigger('query', params); self.trigger(name, params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['selected', 'unselected'];
this.results.on('query:append', function (params) {
self.trigger('query:append', params);
});
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
@ -13455,8 +13439,12 @@ define('select2/core',[
self.close(); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('*', function (name, params) {
self.trigger('results:focus', params); if (nonRelayEvents.indexOf(name) !== -1) {
return;
}
self.trigger(name, params);
}); });
}; };

File diff suppressed because one or more lines are too long

46
dist/js/select2.js vendored
View File

@ -4274,13 +4274,14 @@ define('select2/core',[
Select2.prototype._registerDataEvents = function () { Select2.prototype._registerDataEvents = function () {
var self = this; var self = this;
this.data.on('results:message', function (params) { this.data.on('*', function (name, params) {
self.trigger('results:message', params); self.trigger(name, params);
}); });
}; };
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['open', 'close', 'toggle', 'unselected'];
this.selection.on('open', function () { this.selection.on('open', function () {
self.open(); self.open();
@ -4292,49 +4293,32 @@ define('select2/core',[
self.toggleDropdown(); self.toggleDropdown();
}); });
this.selection.on('results:select', function () {
self.trigger('results:select');
});
this.selection.on('results:previous', function () {
self.trigger('results:previous');
});
this.selection.on('results:next', function () {
self.trigger('results:next');
});
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.close(); self.close();
}); });
this.selection.on('query', function (params) { this.selection.on('*', function (name, params) {
self.trigger('query', params); if (nonRelayEvents.indexOf(name) !== -1) {
}); return;
}
this.selection.on('keypress', function (e) { self.trigger(name, params);
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerDropdownEvents = function () { Select2.prototype._registerDropdownEvents = function () {
var self = this; var self = this;
this.dropdown.on('query', function (params) { this.dropdown.on('*', function (name, params) {
self.trigger('query', params); self.trigger(name, params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['selected', 'unselected'];
this.results.on('query:append', function (params) {
self.trigger('query:append', params);
});
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
@ -4348,8 +4332,12 @@ define('select2/core',[
self.close(); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('*', function (name, params) {
self.trigger('results:focus', params); if (nonRelayEvents.indexOf(name) !== -1) {
return;
}
self.trigger(name, params);
}); });
}; };

File diff suppressed because one or more lines are too long

View File

@ -615,15 +615,15 @@ $(".js-example-language").select2({
<pre data-fill-from=".js-code-theme"></pre> <pre data-fill-from=".js-code-theme"></pre>
<script type="text/x-example-code" class="js-code-theme"> <script type="text/x-example-code" class="js-code-theme">
$(".js-example-theme-single").select2({ $(".js-example-theme-single").select2({
theme: "classic" theme: "classic"
}); });
$(".js-example-theme-multiple").select2({ $(".js-example-theme-multiple").select2({
theme: "classic" theme: "classic"
}); });
</script> </script>
</div> </div>
</section> </section>
@ -647,13 +647,13 @@ $(".js-example-language").select2({
<div class="col-md-8"> <div class="col-md-8">
<h2>Example code</h2> <h2>Example code</h2>
<pre data-fill-from=".js-code-theme"></pre> <pre data-fill-from=".js-code-rtl"></pre>
<script type="text/x-example-code" class="js-code-rtl"> <script type="text/x-example-code" class="js-code-rtl">
$(".js-example-rtl").select2({ $(".js-example-rtl").select2({
dir: "rtl" dir: "rtl"
}); });
</script> </script>
</div> </div>
</section> </section>
</div> </div>

View File

@ -622,6 +622,18 @@ ajax: {
</p> </p>
</section> </section>
<section id="events">
<div class="page-header">
<h1>Events</h1>
</div>
<p>
Select2 has an internal event system that is used to notify parts of the
component that state has changed, as well as an adapter that allows some
of these events to be relayed to the outside word.
</p>
</section>
<section id="adapters"> <section id="adapters">
<div class="page-header"> <div class="page-header">
<h1>Adapters</h1> <h1>Adapters</h1>

View File

@ -150,13 +150,14 @@ define([
Select2.prototype._registerDataEvents = function () { Select2.prototype._registerDataEvents = function () {
var self = this; var self = this;
this.data.on('results:message', function (params) { this.data.on('*', function (name, params) {
self.trigger('results:message', params); self.trigger(name, params);
}); });
}; };
Select2.prototype._registerSelectionEvents = function () { Select2.prototype._registerSelectionEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['open', 'close', 'toggle', 'unselected'];
this.selection.on('open', function () { this.selection.on('open', function () {
self.open(); self.open();
@ -168,49 +169,32 @@ define([
self.toggleDropdown(); self.toggleDropdown();
}); });
this.selection.on('results:select', function () {
self.trigger('results:select');
});
this.selection.on('results:previous', function () {
self.trigger('results:previous');
});
this.selection.on('results:next', function () {
self.trigger('results:next');
});
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.close(); self.close();
}); });
this.selection.on('query', function (params) { this.selection.on('*', function (name, params) {
self.trigger('query', params); if (nonRelayEvents.indexOf(name) !== -1) {
}); return;
}
this.selection.on('keypress', function (e) { self.trigger(name, params);
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerDropdownEvents = function () { Select2.prototype._registerDropdownEvents = function () {
var self = this; var self = this;
this.dropdown.on('query', function (params) { this.dropdown.on('*', function (name, params) {
self.trigger('query', params); self.trigger(name, params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
}); });
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
var self = this; var self = this;
var nonRelayEvents = ['selected', 'unselected'];
this.results.on('query:append', function (params) {
self.trigger('query:append', params);
});
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
@ -224,8 +208,12 @@ define([
self.close(); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('*', function (name, params) {
self.trigger('results:focus', params); if (nonRelayEvents.indexOf(name) !== -1) {
return;
}
self.trigger(name, params);
}); });
}; };