Skip to content

Commit

Permalink
Updated to allow for implementing 'interests' as either a property or…
Browse files Browse the repository at this point in the history
… function. Bumped to version 1.1.0.
  • Loading branch information
efeminella committed Apr 13, 2013
1 parent 4b9d86f commit 4b5739c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -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...
Expand Down
6 changes: 4 additions & 2 deletions 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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-eventbroker.amd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions 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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-eventbroker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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>"
Expand Down
17 changes: 17 additions & 0 deletions spec/backbone-eventbroker-spec.js
Expand Up @@ -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();
});
});
});
});
4 changes: 3 additions & 1 deletion src/backbone-eventbroker.js
Expand Up @@ -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);
Expand Down

0 comments on commit 4b5739c

Please sign in to comment.