Skip to content

Commit

Permalink
feat(CommonApi): adds $off method to unregister callbacks
Browse files Browse the repository at this point in the history
Closes #257
  • Loading branch information
iobaixas committed May 7, 2015
1 parent afcde0b commit 591db81
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/module/api/collection-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,17 @@ RMModule.factory('RMCollectionApi', ['RMUtils', function(Utils) {
*
* @description Finds the location of an object in the array.
*
* If a function is provided then the index of the first item for which the function returns true is returned.
* If a function is provided then the index of the first item for which the function returns true.
*
* @param {RecordApi|function} _obj Object to find
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
* @return {number} Object index or -1 if not found
*/
$indexOf: function(_obj) {
var accept = typeof _obj === 'function' ? _obj : false;
for(var i = 0, l = this.length; i < l; i++) {
if(accept ? accept(this[i]) : this[i] === _obj) return i;
}
return -1;
$indexOf: function(_obj, _fromIdx) {
var accept = typeof _obj !== 'function' ?
function(e) { return e === _obj; } : _obj;

return Utils.indexWhere(this, accept, _fromIdx);
}
};

Expand Down
19 changes: 18 additions & 1 deletion src/module/api/common-api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, $log) {
RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function($http, $q, $log, Utils) {

var EMPTY_ARRAY = [];

Expand Down Expand Up @@ -160,6 +160,23 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
return this;
},

/**
* @memberof CommonApi#
*
* @description Unregisters an instance hook registered with `$on`
*
* @param {string} _hook Hook name
* @param {function} _fun Original callback
* @return {CommonApi} self
*/
$off: function(_hook, _fun) {
if(this.$$cb && this.$$cb[_hook]) {
var idx = Utils.indexWhere(this.$$cb[_hook], function(e) { return e === _fun; });
if(idx !== -1) this.$$cb[_hook].splice(idx, 1);
}
return this;
},

/**
* @memberof CommonApi#
*
Expand Down
19 changes: 19 additions & 0 deletions src/module/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ RMModule.factory('RMUtils', ['$log', function($log) {
};
},

/**
* @memberof Utils
*
* @description
*
* Finds the location of a matching object in an array.
*
* @param {array} _array target array
* @param {function} _accept matching function
* @param {integer} _fromIdx Index from which to start searching, defaults to 0
* @return {number} Object index or -1 if not found
*/
indexWhere: function(_array, _accept, _fromIdx) {
for(var i = _fromIdx || 0, l = _array.length; i < l; i++) {
if(_accept(_array[i])) return i;
}
return -1;
},

/**
* @memberof Utils
*
Expand Down
16 changes: 16 additions & 0 deletions test/common-api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ describe('Restmod model class:', function() {
});
});

describe('$off', function() {

it('should unregister a callback registered using $on', function() {
var bike = Bike.$build(),
spy = jasmine.createSpy('callback');

bike.$on('poke', spy);
bike.$dispatch('poke');
expect(spy.calls.count()).toEqual(1);

bike.$off('poke', spy);
bike.$dispatch('poke');
expect(spy.calls.count()).toEqual(1);
});
});

describe('$dispatch', function() {
// TODO!
});
Expand Down

0 comments on commit 591db81

Please sign in to comment.