-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a pure copy from backbone into reliable_trigger.js for diff
FREEBIE
- Loading branch information
1 parent
7b55fde
commit 700272c
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(function () { | ||
// Pure copy from Backbone. | ||
|
||
// jscs:disable | ||
|
||
var arr = []; | ||
|
||
var slice = arr.slice; | ||
|
||
// Regular expression used to split event strings. | ||
var eventSplitter = /\s+/; | ||
|
||
// Implement fancy features of the Events API such as multiple event | ||
// names `"change blur"` and jQuery-style event maps `{change: action}` | ||
// in terms of the existing API. | ||
var eventsApi = function(obj, action, name, rest) { | ||
if (!name) return true; | ||
|
||
// Handle event maps. | ||
if (typeof name === 'object') { | ||
for (var key in name) { | ||
obj[action].apply(obj, [key, name[key]].concat(rest)); | ||
} | ||
return false; | ||
} | ||
|
||
// Handle space separated event names. | ||
if (eventSplitter.test(name)) { | ||
var names = name.split(eventSplitter); | ||
for (var i = 0, l = names.length; i < l; i++) { | ||
obj[action].apply(obj, [names[i]].concat(rest)); | ||
} | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
// A difficult-to-believe, but optimized internal dispatch function for | ||
// triggering events. Tries to keep the usual cases speedy (most internal | ||
// Backbone events have 3 arguments). | ||
var triggerEvents = function(events, args) { | ||
var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2]; | ||
switch (args.length) { | ||
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return; | ||
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return; | ||
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return; | ||
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return; | ||
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); return; | ||
} | ||
}; | ||
|
||
// Trigger one or many events, firing all bound callbacks. Callbacks are | ||
// passed the same arguments as `trigger` is, apart from the event name | ||
// (unless you're listening on `"all"`, which will cause your callback to | ||
// receive the true name of the event as the first argument). | ||
Backbone.Model.prototype.trigger = Backbone.View.prototype.trigger = Backbone.Events.trigger = function(name) { | ||
if (!this._events) return this; | ||
var args = slice.call(arguments, 1); | ||
if (!eventsApi(this, 'trigger', name, args)) return this; | ||
var events = this._events[name]; | ||
var allEvents = this._events.all; | ||
if (events) triggerEvents(events, args); | ||
if (allEvents) triggerEvents(allEvents, arguments); | ||
return this; | ||
}; | ||
})(); |