Permalink
Browse files

Initial commit

  • Loading branch information...
timoxley committed Oct 15, 2012
0 parents commit 97c70881fd14bcb9019f8d8dcc4ad3d5a55aa335
Showing with 5,360 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +22 −0 LICENSE
  3. +11 −0 Makefile
  4. +78 −0 Readme.md
  5. +18 −0 component.json
  6. +116 −0 index.js
  7. +198 −0 test/backbone-events.js
  8. +199 −0 test/mocha.css
  9. +4,699 −0 test/mocha.js
  10. +17 −0 test/tests.html
@@ -0,0 +1,2 @@
+components
+build
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2010-2012 Jeremy Ashkenas, DocumentCloud
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
+
+build: components index.js
+ @component build --dev
+
+components: component.json
+ @component install --dev
+
+clean:
+ rm -fr build components template.js
+
+.PHONY: clean
@@ -0,0 +1,78 @@
+# backbone-events
+
+### Backbone's Events API ported to a component.
+
+ Events is a module that can be mixed in to any object, giving the object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments.
+
+Note: Documentation is adapted directly from the [backbone
+website](http://backbonejs.org/).
+
+## Installation
+
+ $ component install timoxley/backbone-events
+
+## Example
+
+
+```js
+var object = {};
+
+_.extend(object, Backbone.Events);
+
+object.on("alert", function(msg) {
+ alert("Triggered " + msg);
+});
+
+object.trigger("alert", "an event");
+
+```
+
+## API
+
+### on
+#### object.on(event, callback, [context]) Alias: bind
+Bind a callback function to an object. The callback will be invoked whenever the event is fired. If you have a large number of different events on a page, the convention is to use colons to namespace them: "poll:start", or "change:selection". The event string may also be a space-delimited list of several events...
+
+```js
+book.on("change:title change:author", ...);
+```
+
+To supply a context value for this when the callback is invoked, pass the optional third argument:
+```js
+model.on('change', this.render, this)
+```
+
+Callbacks bound to the special "all" event will be triggered when any event occurs, and are passed the name of the event as the first argument. For example, to proxy all events from one object to another:
+```js
+proxy.on("all", function(eventName) {
+ object.trigger(eventName);
+});
+```
+### off
+#### object.off([event], [callback], [context]) Alias: unbind
+Remove a previously-bound callback function from an object. If no context is specified, all of the versions of the callback with different contexts will be removed. If no callback is specified, all callbacks for the event will be removed. If no event is specified, all event callbacks on the object will be removed.
+
+```js
+// Removes just the `onChange` callback.
+object.off("change", onChange);
+
+// Removes all "change" callbacks.
+object.off("change");
+
+// Removes the `onChange` callback for all events.
+object.off(null, onChange);
+
+// Removes all callbacks for `context` for all events.
+object.off(null, null, context);
+
+// Removes all callbacks on `object`.
+object.off();
+```
+
+### trigger
+#### object.trigger(event, [*args])
+Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
+
+## License
+
+MIT
@@ -0,0 +1,18 @@
+{
+ "name": "backbone-events",
+ "repo": "timoxley/backbone-events",
+ "description": "Backbone Events API emulation on component/emitter",
+ "version": "0.0.1",
+ "keywords": [],
+ "dependencies": {
+ "component/object": "*"
+ },
+ "development": {
+ "component/underscore": "*",
+ "component/indexof": "*",
+ "timoxley/assert": "*"
+ },
+ "scripts": [
+ "index.js"
+ ]
+}
116 index.js
@@ -0,0 +1,116 @@
+// Backbone.Events
+// -----------------
+
+var object = require('object')
+
+// Regular expression used to split event strings
+var eventSplitter = /\s+/;
+
+// A module that can be mixed in to *any object* in order to provide it with
+// custom events. You may bind with `on` or remove with `off` callback functions
+// to an event; `trigger`-ing an event fires all callbacks in succession.
+//
+// var object = {};
+// _.extend(object, Backbone.Events);
+// object.on('expand', function(){ alert('expanded'); });
+// object.trigger('expand');
+//
+var Events = module.exports = {
+
+ // Bind one or more space separated events, `events`, to a `callback`
+ // function. Passing `"all"` will bind the callback to all events fired.
+ on: function(events, callback, context) {
+ var calls, event, list;
+ if (!callback) return this;
+
+ events = events.split(eventSplitter);
+ calls = this._callbacks || (this._callbacks = {});
+
+ while (event = events.shift()) {
+ list = calls[event] || (calls[event] = []);
+ list.push(callback, context);
+ }
+
+ return this;
+ },
+
+ // Remove one or many callbacks. If `context` is null, removes all callbacks
+ // with that function. If `callback` is null, removes all callbacks for the
+ // event. If `events` is null, removes all bound callbacks for all events.
+ off: function(events, callback, context) {
+ var event, calls, list, i;
+
+ // No events, or removing *all* events.
+ if (!(calls = this._callbacks)) return this;
+ if (!(events || callback || context)) {
+ delete this._callbacks;
+ return this;
+ }
+
+ events = events ? events.split(eventSplitter) : object.keys(calls);
+
+ // Loop through the callback list, splicing where appropriate.
+ while (event = events.shift()) {
+ if (!(list = calls[event]) || !(callback || context)) {
+ delete calls[event];
+ continue;
+ }
+
+ for (i = list.length - 2; i >= 0; i -= 2) {
+ if (!(callback && list[i] !== callback || context && list[i + 1] !== context)) {
+ list.splice(i, 2);
+ }
+ }
+ }
+
+ return this;
+ },
+
+ // 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).
+ trigger: function(events) {
+ var event, calls, list, i, length, args, all, rest;
+ if (!(calls = this._callbacks)) return this;
+
+ rest = [];
+ events = events.split(eventSplitter);
+
+ // Fill up `rest` with the callback arguments. Since we're only copying
+ // the tail of `arguments`, a loop is much faster than Array#slice.
+ for (i = 1, length = arguments.length; i < length; i++) {
+ rest[i - 1] = arguments[i];
+ }
+
+ // For each event, walk through the list of callbacks twice, first to
+ // trigger the event, then to trigger any `"all"` callbacks.
+ while (event = events.shift()) {
+ // Copy callback lists to prevent modification.
+ if (all = calls.all) all = all.slice();
+ if (list = calls[event]) list = list.slice();
+
+ // Execute event callbacks.
+ if (list) {
+ for (i = 0, length = list.length; i < length; i += 2) {
+ list[i].apply(list[i + 1] || this, rest);
+ }
+ }
+
+ // Execute "all" callbacks.
+ if (all) {
+ args = [event].concat(rest);
+ for (i = 0, length = all.length; i < length; i += 2) {
+ all[i].apply(all[i + 1] || this, args);
+ }
+ }
+ }
+
+ return this;
+ }
+
+};
+
+// Aliases for backwards compatibility.
+Events.bind = Events.on;
+Events.unbind = Events.off;
Oops, something went wrong.

0 comments on commit 97c7088

Please sign in to comment.