-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Irene Ros
committed
Nov 16, 2012
1 parent
e0f2f61
commit 8013cb9
Showing
20 changed files
with
207 additions
and
214 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
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,119 @@ | ||
(function(global, _) { | ||
|
||
var Miso = global.Miso = (global.Miso || {}); | ||
|
||
/** | ||
* Miso Events is a small set of methods that can be mixed into any object | ||
* to make it evented. It allows one to then subscribe to specific object events, | ||
* to publish events, unsubscribe and subscribeOnce. | ||
*/ | ||
Miso.Events = { | ||
|
||
/** | ||
* Triggers a specific event and passes any additional arguments | ||
* to the callbacks subscribed to that event. | ||
* Params: | ||
* name - the name of the event to trigger | ||
* .* - any additional arguments to pass to callbacks. | ||
*/ | ||
publish : function(name) { | ||
var args = _.toArray(arguments); | ||
args.shift(); | ||
|
||
if (this._events && this._events[name]) { | ||
_.each(this._events[name], function(subscription) { | ||
subscription.callback.apply(subscription.context || this, args); | ||
}, this); | ||
} | ||
return this; | ||
}, | ||
|
||
/** | ||
* Allows subscribing on an evented object to a specific name. | ||
* Provide a callback to trigger. | ||
* Params: | ||
* name - event to subscribe to | ||
* callback - callback to trigger | ||
* options - optional arguments | ||
* priority - allows rearranging of existing callbacks based on priority | ||
* context - allows attaching diff context to callback | ||
* token - allows callback identification by token. | ||
*/ | ||
subscribe : function(name, callback, options) { | ||
options = options || {}; | ||
this._events = this._events || {}; | ||
this._events[name] = this._events[name] || []; | ||
|
||
var subscription = { | ||
callback : callback, | ||
priority : options.priority || 0, | ||
token : options.token || _.uniqueId('t'), | ||
context : options.context || this | ||
}; | ||
var position; | ||
_.each(this._events[name], function(event, index) { | ||
if (!_.isUndefined(position)) { return; } | ||
if (event.priority <= subscription.priority) { | ||
position = index; | ||
} | ||
}); | ||
|
||
this._events[name].splice(position, 0, subscription); | ||
return subscription.token; | ||
}, | ||
|
||
/** | ||
* Allows subscribing to an event once. When the event is triggered | ||
* this subscription will be removed. | ||
* Params: | ||
* name - name of event | ||
* callback - The callback to trigger | ||
* options - optional arguments | ||
* priority - allows rearranging of existing callbacks based on priority | ||
* context - allows attaching diff context to callback | ||
* token - allows callback identification by token. | ||
*/ | ||
subscribeOnce : function(name, callback, options) { | ||
this._events = this._events || {}; | ||
options = options || {}; | ||
var self = this; | ||
|
||
if (typeof options.token === "undefined") { | ||
options.token = _.uniqueId('t'); | ||
} | ||
|
||
return this.subscribe(name, function() { | ||
self.unsubscribe(name, { token : options.token }); | ||
callback.apply(this, arguments); | ||
}, options); | ||
}, | ||
|
||
/** | ||
* Allows unsubscribing from a specific event | ||
* Params: | ||
* name - event to unsubscribe from | ||
* identifier - callback to remove OR token. | ||
*/ | ||
unsubscribe : function(name, identifier) { | ||
|
||
if (_.isUndefined(this._events[name])) { return this; } | ||
|
||
if (_.isFunction(identifier)) { | ||
this._events[name] = _.reject(this._events[name], function(b) { | ||
return b.callback === identifier; | ||
}); | ||
|
||
} else if ( _.isString(identifier)) { | ||
this._events[name] = _.reject(this._events[name], function(b) { | ||
return b.token === identifier; | ||
}); | ||
|
||
} else { | ||
this._events[name] = []; | ||
} | ||
return this; | ||
} | ||
|
||
}; | ||
|
||
}(this, _)); |
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
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
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
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
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
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
Oops, something went wrong.