Permalink
Browse files

Refactoring implementation to use Underscore/Lo-dash utility methods.

  • Loading branch information...
1 parent 3cfb30d commit 4b9d86f2ed73687d773cf35fc3ef54289da9570d @efeminella committed Apr 13, 2013
@@ -33,21 +33,18 @@ define( function($, Backbone, _) {
* for specific objects registered with an EventBroker.
*/
var _registration = function(interests, context, broker, method) {
- var event, callback;
if (!context && interests.interests) {
context = interests;
interests = interests.interests;
}
- for ( event in interests ) {
- if ( interests.hasOwnProperty(event) ) {
- callback = context[interests[event]]
- if ( _.isFunction(callback) ) {
- broker[method](event, callback, context);
- } else {
- throw new Error('method \'' + interests[event] + '\' not found for event \'' + event + '\'');
- }
+ _.each(interests, function(callback, event){
+ var cb = context[callback]
+ if ( _.isFunction(cb) ) {
+ broker[method](event, cb, context);
+ } else {
+ throw new Error("method '" + interests[event] + "' not found for event '" + event + "'");
}
- }
+ });
return broker;
};
@@ -164,7 +161,7 @@ define( function($, Backbone, _) {
* </pre>
*
*/
- unregister: function( interests, context ) {
+ unregister: function(interests, context) {
return interests || context ? _registration(interests, context, this, 'off') : this;
}
};
@@ -186,20 +183,20 @@ define( function($, Backbone, _) {
* a reference to the same broker; that is, only one unique broker will
* be created per namespace.
*/
- get: function( namespace ) {
- if ( _brokers[ namespace ] === undefined ) {
- _brokers[ namespace ] = _.extend( { 'namespace': namespace }, Backbone.Events, EventRegistry );
+ get: function(namespace) {
+ if (!this.has(namespace)) {
+ _brokers[namespace] = _.extend({'namespace': namespace}, Backbone.Events, EventRegistry);
}
- return _brokers[ namespace ];
+ return _brokers[namespace];
},
/*
* Determines if the specified broker has been created for the given
* namespace.
*
*/
- has: function( namespace ) {
- return _brokers[ namespace ] !== undefined;
+ has: function(namespace) {
+ return typeof _brokers[namespace] !== 'undefined';
},
/*
@@ -209,16 +206,14 @@ define( function($, Backbone, _) {
* no arguments.
*
*/
- destroy: function( namespace ) {
- if ( !namespace ) {
- for ( namespace in _brokers ) {
- if ( _brokers.hasOwnProperty( namespace ) ) {
- this.destroy( namespace );
- }
- }
- } else if ( _brokers[ namespace ] ) {
- _brokers[ namespace ].off();
- delete _brokers[ namespace ];
+ destroy: function(namespace) {
+ if (!namespace) {
+ _.each(_brokers, function(broker, ns){
+ this.destroy(ns);
+ }, this );
+ } else if (this.has(namespace)) {
+ _brokers[namespace].off();
+ delete _brokers[namespace];
}
return this;
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -25,21 +25,18 @@
* for specific objects registered with an EventBroker.
*/
var _registration = function(interests, context, broker, method) {
- var event, callback;
if (!context && interests.interests) {
context = interests;
interests = interests.interests;
}
- for ( event in interests ) {
- if ( interests.hasOwnProperty(event) ) {
- callback = context[interests[event]]
- if ( _.isFunction(callback) ) {
- broker[method](event, callback, context);
- } else {
- throw new Error('method \'' + interests[event] + '\' not found for event \'' + event + '\'');
- }
+ _.each(interests, function(callback, event){
+ var cb = context[callback]
+ if ( _.isFunction(cb) ) {
+ broker[method](event, cb, context);
+ } else {
+ throw new Error("method '" + interests[event] + "' not found for event '" + event + "'");
}
- }
+ });
return broker;
};
@@ -156,7 +153,7 @@
* </pre>
*
*/
- unregister: function( interests, context ) {
+ unregister: function(interests, context) {
return interests || context ? _registration(interests, context, this, 'off') : this;
}
};
@@ -178,20 +175,20 @@
* a reference to the same broker; that is, only one unique broker will
* be created per namespace.
*/
- get: function( namespace ) {
- if ( _brokers[ namespace ] === undefined ) {
- _brokers[ namespace ] = _.extend( { 'namespace': namespace }, Backbone.Events, EventRegistry );
+ get: function(namespace) {
+ if (!this.has(namespace)) {
+ _brokers[namespace] = _.extend({'namespace': namespace}, Backbone.Events, EventRegistry);
}
- return _brokers[ namespace ];
+ return _brokers[namespace];
},
/*
* Determines if the specified broker has been created for the given
* namespace.
*
*/
- has: function( namespace ) {
- return _brokers[ namespace ] !== undefined;
+ has: function(namespace) {
+ return typeof _brokers[namespace] !== 'undefined';
},
/*
@@ -201,16 +198,14 @@
* no arguments.
*
*/
- destroy: function( namespace ) {
- if ( !namespace ) {
- for ( namespace in _brokers ) {
- if ( _brokers.hasOwnProperty( namespace ) ) {
- this.destroy( namespace );
- }
- }
- } else if ( _brokers[ namespace ] ) {
- _brokers[ namespace ].off();
- delete _brokers[ namespace ];
+ destroy: function(namespace) {
+ if (!namespace) {
+ _.each(_brokers, function(broker, ns){
+ this.destroy(ns);
+ }, this );
+ } else if (this.has(namespace)) {
+ _brokers[namespace].off();
+ delete _brokers[namespace];
}
return this;
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -12,7 +12,10 @@
<script src="../vendor/underscore/1.4.4/underscore-min.js"></script>
<script src="../vendor/backbone/1.0.0/backbone-min.js"></script>
- <!--<script src="../src/backbone-eventbroker.js"></script>-->
+ <!--
+ <script src="../dist/backbone-eventbroker.js"></script>
+ <script src="../dist/backbone-eventbroker.min.js"></script>
+ -->
<script src="../src/backbone-eventbroker.js"></script>
<script src="backbone-eventbroker-spec.js"></script>
</head>
@@ -16,21 +16,18 @@
* for specific objects registered with an EventBroker.
*/
var _registration = function(interests, context, broker, method) {
- var event, callback;
if (!context && interests.interests) {
context = interests;
interests = interests.interests;
}
- for ( event in interests ) {
- if ( interests.hasOwnProperty(event) ) {
- callback = context[interests[event]]
- if ( _.isFunction(callback) ) {
- broker[method](event, callback, context);
- } else {
- throw new Error('method \'' + interests[event] + '\' not found for event \'' + event + '\'');
- }
+ _.each(interests, function(callback, event){
+ var cb = context[callback]
+ if ( _.isFunction(cb) ) {
+ broker[method](event, cb, context);
+ } else {
+ throw new Error("method '" + interests[event] + "' not found for event '" + event + "'");
}
- }
+ });
return broker;
};
@@ -147,7 +144,7 @@
* </pre>
*
*/
- unregister: function( interests, context ) {
+ unregister: function(interests, context) {
return interests || context ? _registration(interests, context, this, 'off') : this;
}
};
@@ -169,20 +166,20 @@
* a reference to the same broker; that is, only one unique broker will
* be created per namespace.
*/
- get: function( namespace ) {
- if ( _brokers[ namespace ] === undefined ) {
- _brokers[ namespace ] = _.extend( { 'namespace': namespace }, Backbone.Events, EventRegistry );
+ get: function(namespace) {
+ if (!this.has(namespace)) {
+ _brokers[namespace] = _.extend({'namespace': namespace}, Backbone.Events, EventRegistry);
}
- return _brokers[ namespace ];
+ return _brokers[namespace];
},
/*
* Determines if the specified broker has been created for the given
* namespace.
*
*/
- has: function( namespace ) {
- return _brokers[ namespace ] !== undefined;
+ has: function(namespace) {
+ return typeof _brokers[namespace] !== 'undefined';
},
/*
@@ -192,16 +189,14 @@
* no arguments.
*
*/
- destroy: function( namespace ) {
- if ( !namespace ) {
- for ( namespace in _brokers ) {
- if ( _brokers.hasOwnProperty( namespace ) ) {
- this.destroy( namespace );
- }
- }
- } else if ( _brokers[ namespace ] ) {
- _brokers[ namespace ].off();
- delete _brokers[ namespace ];
+ destroy: function(namespace) {
+ if (!namespace) {
+ _.each(_brokers, function(broker, ns){
+ this.destroy(ns);
+ }, this );
+ } else if (this.has(namespace)) {
+ _brokers[namespace].off();
+ delete _brokers[namespace];
}
return this;
}

0 comments on commit 4b9d86f

Please sign in to comment.