Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to allow for implementing 'interests' as either a property or…

… function. Bumped to version 1.1.0.
  • Loading branch information...
commit 4b5739c9610403849fbe20268d4a5bb3af84169e 1 parent 4b9d86f
@efeminella authored
View
30 README.md
@@ -154,6 +154,36 @@ var Users = Backbone.Collection.extend({
remove: function() { ... }
});
```
+Objects can also implement their "interests" as a function which returns an object of specific event/callback mappings, allowing for runtime configurations of interests:
+
+``` javascript
+// Register event/callbacks based on a hash and associated context
+var Users = Backbone.Collection.extend({
+ // defines events of interest and their corresponding callbacks
+ this.interests: function(){
+ return _.extend({
+ 'user:select' : 'select'
+ , 'user:deselect' : 'deselect'
+ }, ( this.isAdmin() ? {
+ 'user:select' : 'select'
+ , 'user:deselect' : 'deselect'
+ , 'user:edit' : 'edit'
+ , 'user:update' : 'update'
+ , 'user:remove' : 'remove'
+ } : {} );
+ },
+ initialize: function() {
+ // register this object with the EventBroker
+ Backbone.EventBroker.register( this );
+ },
+ select: function() { ... },
+ deselect: function() { ... },
+ edit: function() { ... },
+ update: function() { ... },
+ remove: function() { ... }
+});
+```
+
As of version 1.0.0, if a given callback is not a function, the EventBroker will throw an exception, similar to declaratively mapping an event callback in a Backbone.View.
Modules can use different namespaced `EventBrokers` for different things...
View
6 dist/backbone-eventbroker.amd.js
@@ -1,5 +1,5 @@
/*
- * Backbone Eventbroker v1.0.0
+ * Backbone Eventbroker v1.1.0
*
* This version is for use with RequireJS
* If using regular <script> tags to include your files, use backbone-memory.min.js
@@ -33,11 +33,13 @@ define( function($, Backbone, _) {
* for specific objects registered with an EventBroker.
*/
var _registration = function(interests, context, broker, method) {
+ var events;
if (!context && interests.interests) {
context = interests;
interests = interests.interests;
}
- _.each(interests, function(callback, event){
+ events = _.isFunction(interests) ? interests() : interests;
+ _.each( events, function(callback, event){
var cb = context[callback]
if ( _.isFunction(cb) ) {
broker[method](event, cb, context);
View
2  dist/backbone-eventbroker.amd.min.js
@@ -1 +1 @@
-define(function(e,t,n){var n=require("underscore"),e=require("jquery"),t=require("backbone");return"use strict",t.EventBroker=t.EventBroker||function(){var e={},r=function(e,t,r,i){return!t&&e.interests&&(t=e,e=e.interests),n.each(e,function(s,o){var u=t[s];if(!n.isFunction(u))throw new Error("method '"+e[o]+"' not found for event '"+o+"'");r[i](o,u,t)}),r},i={register:function(e,t){return e||t?r(e,t,this,"on"):this},unregister:function(e,t){return e||t?r(e,t,this,"off"):this}};return n.extend({namespace:"",get:function(r){return this.has(r)||(e[r]=n.extend({namespace:r},t.Events,i)),e[r]},has:function(t){return typeof e[t]!="undefined"},destroy:function(t){return t?this.has(t)&&(e[t].off(),delete e[t]):n.each(e,function(e,t){this.destroy(t)},this),this}},t.Events,i)}(),t.EventBroker})
+define(function(e,t,n){var n=require("underscore"),e=require("jquery"),t=require("backbone");return"use strict",t.EventBroker=t.EventBroker||function(){var e={},r=function(e,t,r,i){var s;return!t&&e.interests&&(t=e,e=e.interests),s=n.isFunction(e)?e():e,n.each(s,function(s,o){var u=t[s];if(!n.isFunction(u))throw new Error("method '"+e[o]+"' not found for event '"+o+"'");r[i](o,u,t)}),r},i={register:function(e,t){return e||t?r(e,t,this,"on"):this},unregister:function(e,t){return e||t?r(e,t,this,"off"):this}};return n.extend({namespace:"",get:function(r){return this.has(r)||(e[r]=n.extend({namespace:r},t.Events,i)),e[r]},has:function(t){return typeof e[t]!="undefined"},destroy:function(t){return t?this.has(t)&&(e[t].off(),delete e[t]):n.each(e,function(e,t){this.destroy(t)},this),this}},t.Events,i)}(),t.EventBroker})
View
6 dist/backbone-eventbroker.js
@@ -1,5 +1,5 @@
/*
- * Backbone Eventbroker v1.0.0
+ * Backbone Eventbroker v1.1.0
*
* Copyright (c) 2012 - 2013 Eric Feminella, Sven Lito
* License and more information at: http://code.ericfeminella.com/license/LICENSE.txt
@@ -25,11 +25,13 @@
* for specific objects registered with an EventBroker.
*/
var _registration = function(interests, context, broker, method) {
+ var events;
if (!context && interests.interests) {
context = interests;
interests = interests.interests;
}
- _.each(interests, function(callback, event){
+ events = _.isFunction(interests) ? interests() : interests;
+ _.each( events, function(callback, event){
var cb = context[callback]
if ( _.isFunction(cb) ) {
broker[method](event, cb, context);
View
2  dist/backbone-eventbroker.min.js
@@ -1 +1 @@
-(function(e,t,n){"use strict";n.EventBroker=n.EventBroker||function(){var e={},r=function(e,n,r,i){return!n&&e.interests&&(n=e,e=e.interests),t.each(e,function(s,o){var u=n[s];if(!t.isFunction(u))throw new Error("method '"+e[o]+"' not found for event '"+o+"'");r[i](o,u,n)}),r},i={register:function(e,t){return e||t?r(e,t,this,"on"):this},unregister:function(e,t){return e||t?r(e,t,this,"off"):this}};return t.extend({namespace:"",get:function(r){return this.has(r)||(e[r]=t.extend({namespace:r},n.Events,i)),e[r]},has:function(t){return typeof e[t]!="undefined"},destroy:function(n){return n?this.has(n)&&(e[n].off(),delete e[n]):t.each(e,function(e,t){this.destroy(t)},this),this}},n.Events,i)}();if(typeof define=="function"&&define.amd)return define(function(){return n.EventBroker});if(typeof module=="object"&&typeof module.exports=="object"){module.exports=n.EventBroker;return}})(jQuery,_,Backbone)
+(function(e,t,n){"use strict";n.EventBroker=n.EventBroker||function(){var e={},r=function(e,n,r,i){var s;return!n&&e.interests&&(n=e,e=e.interests),s=t.isFunction(e)?e():e,t.each(s,function(s,o){var u=n[s];if(!t.isFunction(u))throw new Error("method '"+e[o]+"' not found for event '"+o+"'");r[i](o,u,n)}),r},i={register:function(e,t){return e||t?r(e,t,this,"on"):this},unregister:function(e,t){return e||t?r(e,t,this,"off"):this}};return t.extend({namespace:"",get:function(r){return this.has(r)||(e[r]=t.extend({namespace:r},n.Events,i)),e[r]},has:function(t){return typeof e[t]!="undefined"},destroy:function(n){return n?this.has(n)&&(e[n].off(),delete e[n]):t.each(e,function(e,t){this.destroy(t)},this),this}},n.Events,i)}();if(typeof define=="function"&&define.amd)return define(function(){return n.EventBroker});if(typeof module=="object"&&typeof module.exports=="object"){module.exports=n.EventBroker;return}})(jQuery,_,Backbone)
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "backbone-eventbroker",
- "version": "1.0.0",
+ "version": "1.1.0",
"authors": [
"Eric Feminella <eric@ericfeminella.com>",
"Sven Lito <me@svenlito.com>"
View
17 spec/backbone-eventbroker-spec.js
@@ -216,5 +216,22 @@ describe('EventBroker', function()
expect(_.isEmpty(this.users.users)).toBeTruthy(this.user);
});
});
+
+ describe('when registering interests implemented as a function', function() {
+ it ('should register each event / callback mapping', function() {
+ spyOn(this.users, 'remove');
+ this.interests = function(){
+ return {
+ 'users:add': 'add',
+ 'users:delete': 'remove'
+ }
+ };
+ this.broker.register(this.interests, this.users);
+ this.broker.trigger('users:delete', this.user);
+ expect(this.users.remove).toHaveBeenCalled();
+ expect(this.users.remove).toHaveBeenCalledWith(this.user);
+ expect(this.users.users[this.user.id]).toBeUndefined();
+ });
+ });
});
});
View
4 src/backbone-eventbroker.js
@@ -16,11 +16,13 @@
* for specific objects registered with an EventBroker.
*/
var _registration = function(interests, context, broker, method) {
+ var events;
if (!context && interests.interests) {
context = interests;
interests = interests.interests;
}
- _.each(interests, function(callback, event){
+ events = _.isFunction(interests) ? interests() : interests;
+ _.each( events, function(callback, event){
var cb = context[callback]
if ( _.isFunction(cb) ) {
broker[method](event, cb, context);
Please sign in to comment.
Something went wrong with that request. Please try again.