From 591db81b223c57b7b94b306c629aa3ab22d34f6e Mon Sep 17 00:00:00 2001 From: Ignacio Baixas Date: Thu, 7 May 2015 17:23:57 -0300 Subject: [PATCH] feat(CommonApi): adds $off method to unregister callbacks Closes #257 --- src/module/api/collection-api.js | 14 +++++++------- src/module/api/common-api.js | 19 ++++++++++++++++++- src/module/utils.js | 19 +++++++++++++++++++ test/common-api-spec.js | 16 ++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/module/api/collection-api.js b/src/module/api/collection-api.js index 660356c..1705e54 100644 --- a/src/module/api/collection-api.js +++ b/src/module/api/collection-api.js @@ -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); } }; diff --git a/src/module/api/common-api.js b/src/module/api/common-api.js index c2d28d1..aedaf54 100644 --- a/src/module/api/common-api.js +++ b/src/module/api/common-api.js @@ -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 = []; @@ -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# * diff --git a/src/module/utils.js b/src/module/utils.js index 7d0f1e9..9fd51e0 100644 --- a/src/module/utils.js +++ b/src/module/utils.js @@ -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 * diff --git a/test/common-api-spec.js b/test/common-api-spec.js index 8c70ebd..d810435 100644 --- a/test/common-api-spec.js +++ b/test/common-api-spec.js @@ -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! });