From c9cafdd3edcbfd62259fa19941ca6f9223a053cc Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 27 Feb 2015 09:55:55 +0000 Subject: [PATCH] adding missing docblocks and tiny tweak --- bower.json | 2 +- dist/angular-locker.js | 41 ++++++++++++++++++++++++---------- dist/angular-locker.min.js | 2 +- dist/angular-locker.min.js.map | 2 +- package.json | 2 +- src/angular-locker.js | 41 ++++++++++++++++++++++++---------- 6 files changed, 62 insertions(+), 28 deletions(-) diff --git a/bower.json b/bower.json index dd07db3..d1b7bb1 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-locker", - "version": "1.2.0", + "version": "1.2.1", "homepage": "https://github.com/tymondesigns/angular-locker", "authors": [ "Sean Tymon " diff --git a/dist/angular-locker.js b/dist/angular-locker.js index de2e2e5..88d9b88 100644 --- a/dist/angular-locker.js +++ b/dist/angular-locker.js @@ -59,7 +59,7 @@ }; /** - * Set the default driver and namespace + * Set the defaults * * @type {Object} */ @@ -76,7 +76,8 @@ * Allow setting of default storage driver via `lockerProvider` * e.g. lockerProvider.setDefaultDriver('session'); * - * @param {String} driver + * @param {String|Function} driver + * @return {self} */ setDefaultDriver: function (driver) { defaults.driver = _value(driver); @@ -86,6 +87,8 @@ /** * Get the default driver + * + * @return {String} */ getDefaultDriver: function () { return defaults.driver; @@ -95,7 +98,8 @@ * Allow setting of default namespace via `lockerProvider` * e.g. lockerProvider.setDefaultNamespace('myAppName'); * - * @param {String} namespace + * @param {String|Function} namespace + * @return {self} */ setDefaultNamespace: function (namespace) { defaults.namespace = _value(namespace); @@ -105,6 +109,8 @@ /** * Get the default namespace + * + * @return {String} */ getDefaultNamespace: function () { return defaults.namespace; @@ -113,7 +119,8 @@ /** * Set whether the events are enabled * - * @param {Boolean} enabled + * @param {Boolean|Function} enabled + * @return {self} */ setEventsEnabled: function (enabled) { defaults.eventsEnabled = _value(enabled); @@ -123,6 +130,8 @@ /** * Get whether the events are enabled + * + * @return {Boolean} */ getEventsEnabled: function () { return defaults.eventsEnabled; @@ -131,7 +140,8 @@ /** * Set the separator to use with namespace in keys * - * @param {String} separator + * @param {String|Function} separator + * @return {self} */ setSeparator: function (separator) { defaults.separator = _value(separator); @@ -141,6 +151,8 @@ /** * Get the separator + * + * @return {String} */ getSeparator: function () { return defaults.separator; @@ -160,6 +172,8 @@ function Locker (driver, namespace) { /** + * Out of the box drivers + * * @type {Object} */ this._registeredDrivers = { @@ -281,8 +295,8 @@ /** * Trigger an event * - * @param {String} name - * @param {Object} payload + * @param {String} name + * @param {Object} payload * @return {void} */ this._event = function (name, payload) { @@ -473,7 +487,7 @@ }, /** - * Return all items in storage within the current namespace + * Return all items in storage within the current namespace/driver * * @return {Object} */ @@ -492,7 +506,7 @@ }, /** - * Remove all items set within the current namespace + * Remove all items set within the current namespace/driver * * @return {self} */ @@ -554,10 +568,13 @@ unbind: function ($scope, key) { $parse(key).assign($scope, void 0); this.forget(key); - if (this._watchers[key + $scope.$id]) { + + var watchId = key + $scope.$id; + + if (this._watchers[watchId]) { // execute the de-registration function - this._watchers[key + $scope.$id](); - delete this._watchers[key + $scope.$id]; + this._watchers[watchId](); + delete this._watchers[watchId]; } return this; diff --git a/dist/angular-locker.min.js b/dist/angular-locker.min.js index ef6f9f0..c49ec72 100644 --- a/dist/angular-locker.min.js +++ b/dist/angular-locker.min.js @@ -1,3 +1,3 @@ /*! angular-locker v1.2.0 | (c) 2015 @tymondesigns | https://github.com/tymondesigns/angular-locker */ -!function(e,t){"function"==typeof define&&define.amd?define(function(){return t(e.angular)}):"object"==typeof exports?module.exports=t(e.angular||window&&window.angular):t(e.angular)}(this,function(e){"use strict";e.module("angular-locker",[]).provider("locker",function(){var t=function(t,r){return e.isFunction(t)?t(r):t},r=function(e,t){return Object.keys(e).filter(function(r){return e[r]===t})[0]},i=function(e){throw new Error("[angular-locker] "+e)},s={driver:"local",namespace:"locker",eventsEnabled:!0,separator:"."};return{setDefaultDriver:function(e){return s.driver=t(e),this},getDefaultDriver:function(){return s.driver},setDefaultNamespace:function(e){return s.namespace=t(e),this},getDefaultNamespace:function(){return s.namespace},setEventsEnabled:function(e){return s.eventsEnabled=t(e),this},getEventsEnabled:function(){return s.eventsEnabled},setSeparator:function(e){return s.separator=t(e),this},getSeparator:function(){return s.separator},$get:["$window","$rootScope","$parse",function(n,o,a){function h(a,h){this._registeredDrivers={local:n.localStorage,session:n.sessionStorage},this._resolveDriver=function(e){return this._registeredDrivers.hasOwnProperty(e)||i('The driver "'+e+'" was not found.'),this._registeredDrivers[e]},this._deriveDriver=function(e){return r(this._registeredDrivers,e)},this._driver=this._resolveDriver(a),this._namespace=h,this._eventsEnabled=s.eventsEnabled,this._separator=s.separator,this._watchers={},this._checkSupport=function(t){if(e.isUndefined(this._supported)){var r="l";try{this._resolveDriver(t||"local").setItem(r,r),this._resolveDriver(t||"local").removeItem(r),this._supported=!0}catch(i){this._supported=!1}}return this._supported},this._getPrefix=function(e){return this._namespace?this._namespace+this._separator+e:e},this._serialize=function(t){try{return e.toJson(t)}catch(r){return t}},this._unserialize=function(t){try{return e.fromJson(t)}catch(r){return t}},this._event=function(t,r){this._eventsEnabled&&o.$emit(t,e.extend(r,{driver:this._deriveDriver(this._driver),namespace:this._namespace}))},this._setItem=function(t,r){this._checkSupport()||i("The browser does not support localStorage");try{var s=this._getItem(t);this._driver.setItem(this._getPrefix(t),this._serialize(r)),this._exists(t)&&!e.equals(s,r)?this._event("locker.item.updated",{key:t,oldValue:s,newValue:r}):this._event("locker.item.added",{key:t,value:r})}catch(n){-1!==["QUOTA_EXCEEDED_ERR","NS_ERROR_DOM_QUOTA_REACHED","QuotaExceededError"].indexOf(n.name)?i("The browser storage quota has been exceeded"):i('Could not add item with key "'+t+'"')}},this._getItem=function(e){return this._checkSupport()||i("The browser does not support localStorage"),this._unserialize(this._driver.getItem(this._getPrefix(e)))},this._exists=function(e){return this._checkSupport()||i("The browser does not support localStorage"),this._driver.hasOwnProperty(this._getPrefix(t(e)))},this._removeItem=function(e){return this._checkSupport()||i("The browser does not support localStorage"),this._exists(e)?(this._driver.removeItem(this._getPrefix(e)),this._event("locker.item.forgotten",{key:e}),!0):!1}}return h.prototype={put:function(r,i){if(!r)return!1;if(r=t(r),e.isObject(r))e.forEach(r,function(e,t){this._setItem(t,e)},this);else{if(!e.isDefined(i))return!1;this._setItem(r,t(i,this._getItem(r)))}return this},add:function(e,t){return this.has(e)?!1:(this.put(e,t),!0)},get:function(t,r){if(e.isArray(t)){var i={};return e.forEach(t,function(e){this.has(e)&&(i[e]=this._getItem(e))},this),i}return this.has(t)?this._getItem(t):2===arguments.length?r:void 0},has:function(e){return this._exists(e)},forget:function(r){return r=t(r),e.isArray(r)?r.map(this._removeItem,this):this._removeItem(r),this},pull:function(e,t){var r=this.get(e,t);return this.forget(e),r},all:function(){var t={};return e.forEach(this._driver,function(e,r){var i=r.split(this._separator);i.length>1&&i[0]===this._namespace&&(i.splice(0,1),r=i.join(this._separator)),this.has(r)&&(t[r]=this.get(r))},this),t},clean:function(){return this.forget(Object.keys(this.all())),this},empty:function(){return this._driver.clear(),this},count:function(){return Object.keys(this.all()).length},bind:function(t,r,i){e.isUndefined(t.$eval(r))&&(a(r).assign(t,this.get(r,i)),this.has(r)||this.put(r,i));var s=this;return this._watchers[r+t.$id]=t.$watch(r,function(t){e.isDefined(t)&&s.put(r,t)},e.isObject(t[r])),this},unbind:function(e,t){return a(t).assign(e,void 0),this.forget(t),this._watchers[t+e.$id]&&(this._watchers[t+e.$id](),delete this._watchers[t+e.$id]),this},driver:function(e){return this.instance(e,this._namespace)},getDriver:function(){return this._driver},namespace:function(e){return this.instance(this._deriveDriver(this._driver),e)},getNamespace:function(){return this._namespace},supported:function(e){return this._checkSupport(e)},instance:function(e,t){return new h(e,t)}},new h(s.driver,s.namespace)}]}})}); +!function(e,t){"function"==typeof define&&define.amd?define(function(){return t(e.angular)}):"object"==typeof exports?module.exports=t(e.angular||window&&window.angular):t(e.angular)}(this,function(e){"use strict";e.module("angular-locker",[]).provider("locker",function(){var t=function(t,r){return e.isFunction(t)?t(r):t},r=function(e,t){return Object.keys(e).filter(function(r){return e[r]===t})[0]},i=function(e){throw new Error("[angular-locker] "+e)},s={driver:"local",namespace:"locker",eventsEnabled:!0,separator:"."};return{setDefaultDriver:function(e){return s.driver=t(e),this},getDefaultDriver:function(){return s.driver},setDefaultNamespace:function(e){return s.namespace=t(e),this},getDefaultNamespace:function(){return s.namespace},setEventsEnabled:function(e){return s.eventsEnabled=t(e),this},getEventsEnabled:function(){return s.eventsEnabled},setSeparator:function(e){return s.separator=t(e),this},getSeparator:function(){return s.separator},$get:["$window","$rootScope","$parse",function(n,o,a){function h(a,h){this._registeredDrivers={local:n.localStorage,session:n.sessionStorage},this._resolveDriver=function(e){return this._registeredDrivers.hasOwnProperty(e)||i('The driver "'+e+'" was not found.'),this._registeredDrivers[e]},this._deriveDriver=function(e){return r(this._registeredDrivers,e)},this._driver=this._resolveDriver(a),this._namespace=h,this._eventsEnabled=s.eventsEnabled,this._separator=s.separator,this._watchers={},this._checkSupport=function(t){if(e.isUndefined(this._supported)){var r="l";try{this._resolveDriver(t||"local").setItem(r,r),this._resolveDriver(t||"local").removeItem(r),this._supported=!0}catch(i){this._supported=!1}}return this._supported},this._getPrefix=function(e){return this._namespace?this._namespace+this._separator+e:e},this._serialize=function(t){try{return e.toJson(t)}catch(r){return t}},this._unserialize=function(t){try{return e.fromJson(t)}catch(r){return t}},this._event=function(t,r){this._eventsEnabled&&o.$emit(t,e.extend(r,{driver:this._deriveDriver(this._driver),namespace:this._namespace}))},this._setItem=function(t,r){this._checkSupport()||i("The browser does not support localStorage");try{var s=this._getItem(t);this._driver.setItem(this._getPrefix(t),this._serialize(r)),this._exists(t)&&!e.equals(s,r)?this._event("locker.item.updated",{key:t,oldValue:s,newValue:r}):this._event("locker.item.added",{key:t,value:r})}catch(n){-1!==["QUOTA_EXCEEDED_ERR","NS_ERROR_DOM_QUOTA_REACHED","QuotaExceededError"].indexOf(n.name)?i("The browser storage quota has been exceeded"):i('Could not add item with key "'+t+'"')}},this._getItem=function(e){return this._checkSupport()||i("The browser does not support localStorage"),this._unserialize(this._driver.getItem(this._getPrefix(e)))},this._exists=function(e){return this._checkSupport()||i("The browser does not support localStorage"),this._driver.hasOwnProperty(this._getPrefix(t(e)))},this._removeItem=function(e){return this._checkSupport()||i("The browser does not support localStorage"),this._exists(e)?(this._driver.removeItem(this._getPrefix(e)),this._event("locker.item.forgotten",{key:e}),!0):!1}}return h.prototype={put:function(r,i){if(!r)return!1;if(r=t(r),e.isObject(r))e.forEach(r,function(e,t){this._setItem(t,e)},this);else{if(!e.isDefined(i))return!1;this._setItem(r,t(i,this._getItem(r)))}return this},add:function(e,t){return this.has(e)?!1:(this.put(e,t),!0)},get:function(t,r){if(e.isArray(t)){var i={};return e.forEach(t,function(e){this.has(e)&&(i[e]=this._getItem(e))},this),i}return this.has(t)?this._getItem(t):2===arguments.length?r:void 0},has:function(e){return this._exists(e)},forget:function(r){return r=t(r),e.isArray(r)?r.map(this._removeItem,this):this._removeItem(r),this},pull:function(e,t){var r=this.get(e,t);return this.forget(e),r},all:function(){var t={};return e.forEach(this._driver,function(e,r){var i=r.split(this._separator);i.length>1&&i[0]===this._namespace&&(i.splice(0,1),r=i.join(this._separator)),this.has(r)&&(t[r]=this.get(r))},this),t},clean:function(){return this.forget(Object.keys(this.all())),this},empty:function(){return this._driver.clear(),this},count:function(){return Object.keys(this.all()).length},bind:function(t,r,i){e.isUndefined(t.$eval(r))&&(a(r).assign(t,this.get(r,i)),this.has(r)||this.put(r,i));var s=this;return this._watchers[r+t.$id]=t.$watch(r,function(t){e.isDefined(t)&&s.put(r,t)},e.isObject(t[r])),this},unbind:function(e,t){a(t).assign(e,void 0),this.forget(t);var r=t+e.$id;return this._watchers[r]&&(this._watchers[r](),delete this._watchers[r]),this},driver:function(e){return this.instance(e,this._namespace)},getDriver:function(){return this._driver},namespace:function(e){return this.instance(this._deriveDriver(this._driver),e)},getNamespace:function(){return this._namespace},supported:function(e){return this._checkSupport(e)},instance:function(e,t){return new h(e,t)}},new h(s.driver,s.namespace)}]}})}); //# sourceMappingURL=angular-locker.min.js.map \ No newline at end of file diff --git a/dist/angular-locker.min.js.map b/dist/angular-locker.min.js.map index ad6f1a7..04fd4a8 100644 --- a/dist/angular-locker.min.js.map +++ b/dist/angular-locker.min.js.map @@ -1,2 +1,2 @@ /*! angular-locker v1.2.0 | (c) 2015 @tymondesigns | https://github.com/tymondesigns/angular-locker */ -{"version":3,"sources":["angular-locker.min.js"],"names":["root","factory","define","amd","angular","exports","module","window","this","provider","_value","value","param","isFunction","_keyByVal","object","Object","keys","filter","key","_error","msg","Error","defaults","driver","namespace","eventsEnabled","separator","setDefaultDriver","getDefaultDriver","setDefaultNamespace","getDefaultNamespace","setEventsEnabled","enabled","getEventsEnabled","setSeparator","getSeparator","$get","$window","$rootScope","$parse","Locker","_registeredDrivers","local","localStorage","session","sessionStorage","_resolveDriver","hasOwnProperty","_deriveDriver","_driver","_namespace","_eventsEnabled","_separator","_watchers","_checkSupport","isUndefined","_supported","l","setItem","removeItem","e","_getPrefix","_serialize","toJson","_unserialize","fromJson","_event","name","payload","$emit","extend","_setItem","oldVal","_getItem","_exists","equals","oldValue","newValue","indexOf","getItem","_removeItem","prototype","put","isObject","forEach","isDefined","add","has","get","def","isArray","items","k","arguments","length","forget","map","pull","all","split","splice","join","clean","empty","clear","count","bind","$scope","$eval","assign","self","$id","$watch","newVal","unbind","instance","getDriver","getNamespace","supported"],"mappings":"CAUA,SAAWA,EAAMC,GACS,kBAAXC,SAAyBA,OAAOC,IACvCD,OAAO,WACH,MAAOD,GAAQD,EAAKI,WAEE,gBAAZC,SACdC,OAAOD,QAAUJ,EAAQD,EAAKI,SAAYG,QAAUA,OAAOH,SAE3DH,EAAQD,EAAKI,UAElBI,KAAM,SAAUJ,GAEf,YAEAA,GAAQE,OAAO,qBAEdG,SAAS,SAAU,WAShB,GAAIC,GAAS,SAAUC,EAAOC,GAC1B,MAAOR,G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AAQ,QAC3BpG,KAAKwF,OAAO7E,GACRX,KAAK8C,UAAUnC,EAAMyF,EAAOI,OAE5BxG,KAAK8C,UAAUnC,EAAMyF,EAAOI,aACrBxG,MAAK8C,UAAUnC,EAAMyF,EAAOI,MAGhCxG,MASXgB,OAAQ,SAAUA,GACd,MAAOhB,MAAK4G,SAAS5F,EAAQhB,KAAK2C,aAQtCkE,UAAW,WACP,MAAO7G,MAAK0C,SAShBzB,UAAW,SAAUA,GACjB,MAAOjB,MAAK4G,SAAS5G,KAAKyC,cAAczC,KAAK0C,SAAUzB,IAQ3D6F,aAAc,WACV,MAAO9G,MAAK2C,YAUhBoE,UAAW,SAAU/F,GACjB,MAAOhB,MAAK+C,cAAc/B,IAU9B4F,SAAU,SAAU5F,EAAQC,GACxB,MAAO,IAAIgB,GAAOjB,EAAQC,KAK3B,GAAIgB,GAAOlB,EAASC,OAAQD,EAASE","file":"angular-locker.min.js","sourcesContent":["/**\n * angular-locker\n *\n * A simple & configurable abstraction for local/session storage in angular projects.\n *\n * @link https://github.com/tymondesigns/angular-locker\n * @author Sean Tymon @tymondesigns\n * @license MIT License, http://www.opensource.org/licenses/MIT\n */\n\n(function (root, factory) {\n if (typeof define === 'function' && define.amd) {\n define(function () {\n return factory(root.angular);\n });\n } else if (typeof exports === 'object') {\n module.exports = factory(root.angular || (window && window.angular));\n } else {\n factory(root.angular);\n }\n})(this, function (angular) {\n\n 'use strict';\n\n angular.module('angular-locker', [])\n\n .provider('locker', function () {\n\n /**\n * If value is a function then execute, otherwise return\n *\n * @param {Mixed} value\n * @param {Mixed} parameter\n * @return {Mixed}\n */\n var _value = function (value, param) {\n return angular.isFunction(value) ? value(param) : value;\n };\n\n /**\n * Get the key of an object by the value\n *\n * @param {Object} object\n * @param {Mixed} value\n * @return {String}\n */\n var _keyByVal = function (object, value) {\n return Object.keys(object).filter(function (key) { return object[key] === value; })[0];\n };\n\n /**\n * Trigger an error\n *\n * @param {String} msg\n * @return {void}\n */\n var _error = function (msg) {\n throw new Error('[angular-locker] ' + msg);\n };\n\n /**\n * Set the default driver and namespace\n *\n * @type {Object}\n */\n var defaults = {\n driver: 'local',\n namespace: 'locker',\n eventsEnabled: true,\n separator: '.'\n };\n\n return {\n\n /**\n * Allow setting of default storage driver via `lockerProvider`\n * e.g. lockerProvider.setDefaultDriver('session');\n *\n * @param {String} driver\n */\n setDefaultDriver: function (driver) {\n defaults.driver = _value(driver);\n\n return this;\n },\n\n /**\n * Get the default driver\n */\n getDefaultDriver: function () {\n return defaults.driver;\n },\n\n /**\n * Allow setting of default namespace via `lockerProvider`\n * e.g. lockerProvider.setDefaultNamespace('myAppName');\n *\n * @param {String} namespace\n */\n setDefaultNamespace: function (namespace) {\n defaults.namespace = _value(namespace);\n\n return this;\n },\n\n /**\n * Get the default namespace\n */\n getDefaultNamespace: function () {\n return defaults.namespace;\n },\n\n /**\n * Set whether the events are enabled\n *\n * @param {Boolean} enabled\n */\n setEventsEnabled: function (enabled) {\n defaults.eventsEnabled = _value(enabled);\n\n return this;\n },\n\n /**\n * Get whether the events are enabled\n */\n getEventsEnabled: function () {\n return defaults.eventsEnabled;\n },\n\n /**\n * Set the separator to use with namespace in keys\n *\n * @param {String} separator\n */\n setSeparator: function (separator) {\n defaults.separator = _value(separator);\n\n return this;\n },\n\n /**\n * Get the separator\n */\n getSeparator: function () {\n return defaults.separator;\n },\n\n /**\n * The locker service\n */\n $get: ['$window', '$rootScope', '$parse', function ($window, $rootScope, $parse) {\n\n /**\n * Define the Locker class\n *\n * @param {Storage} driver\n * @param {String} namespace\n */\n function Locker (driver, namespace) {\n\n /**\n * @type {Object}\n */\n this._registeredDrivers = {\n local: $window.localStorage,\n session: $window.sessionStorage\n };\n\n /**\n * Get the Storage instance from the key\n *\n * @param {String} driver\n * @return {Storage}\n */\n this._resolveDriver = function (driver) {\n if (! this._registeredDrivers.hasOwnProperty(driver)) {\n _error('The driver \"' + driver + '\" was not found.');\n }\n\n return this._registeredDrivers[driver];\n };\n\n /**\n * Get the driver key (local/session) by the Storage instance\n *\n * @param {Storage} driver\n * @return {String}\n */\n this._deriveDriver = function (driver) {\n return _keyByVal(this._registeredDrivers, driver);\n };\n\n /**\n * @type {Storage}\n */\n this._driver = this._resolveDriver(driver);\n\n /**\n * @type {String}\n */\n this._namespace = namespace;\n\n /**\n * @type {Boolean}\n */\n this._eventsEnabled = defaults.eventsEnabled;\n\n /**\n * @type {String}\n */\n this._separator = defaults.separator;\n\n /**\n * @type {Object}\n */\n this._watchers = {};\n\n /**\n * Check browser support\n *\n * @see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js#L38-L47\n * @param {String} driver\n * @return {Boolean}\n */\n this._checkSupport = function (driver) {\n if (angular.isUndefined(this._supported)) {\n var l = 'l';\n try {\n this._resolveDriver(driver || 'local').setItem(l, l);\n this._resolveDriver(driver || 'local').removeItem(l);\n this._supported = true;\n } catch (e) {\n this._supported = false;\n }\n }\n\n return this._supported;\n };\n\n /**\n * Build the storage key from the namspace\n *\n * @param {String} key\n * @return {String}\n */\n this._getPrefix = function (key) {\n if (! this._namespace) return key;\n\n return this._namespace + this._separator + key;\n };\n\n /**\n * Try to encode value as json, or just return the value upon failure\n *\n * @param {Mixed} value\n * @return {Mixed}\n */\n this._serialize = function (value) {\n try {\n return angular.toJson(value);\n } catch (e) {\n return value;\n }\n };\n\n /**\n * Try to parse value as json, if it fails then it probably isn't json so just return it\n *\n * @param {String} value\n * @return {Object|String}\n */\n this._unserialize = function (value) {\n try {\n return angular.fromJson(value);\n } catch (e) {\n return value;\n }\n };\n\n /**\n * Trigger an event\n *\n * @param {String} name\n * @param {Object} payload\n * @return {void}\n */\n this._event = function (name, payload) {\n if (! this._eventsEnabled) return;\n\n $rootScope.$emit(name, angular.extend(payload, {\n driver: this._deriveDriver(this._driver),\n namespace: this._namespace,\n }));\n };\n\n /**\n * Add to storage\n *\n * @param {String} key\n * @param {Mixed} value\n */\n this._setItem = function (key, value) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n try {\n var oldVal = this._getItem(key);\n this._driver.setItem(this._getPrefix(key), this._serialize(value));\n if (this._exists(key) && ! angular.equals(oldVal, value)) {\n this._event('locker.item.updated', { key: key, oldValue: oldVal, newValue: value });\n } else {\n this._event('locker.item.added', { key: key, value: value });\n }\n } catch (e) {\n if (['QUOTA_EXCEEDED_ERR', 'NS_ERROR_DOM_QUOTA_REACHED', 'QuotaExceededError'].indexOf(e.name) !== -1) {\n _error('The browser storage quota has been exceeded');\n } else {\n _error('Could not add item with key \"' + key + '\"');\n }\n }\n };\n\n /**\n * Get from storage\n *\n * @param {String} key\n * @return {Mixed}\n */\n this._getItem = function (key) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n return this._unserialize(this._driver.getItem(this._getPrefix(key)));\n };\n\n /**\n * Exists in storage\n *\n * @param {String} key\n * @return {Boolean}\n */\n this._exists = function (key) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n return this._driver.hasOwnProperty(this._getPrefix(_value(key)));\n };\n\n /**\n * Remove from storage\n *\n * @param {String} key\n * @return {Boolean}\n */\n this._removeItem = function (key) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n if (! this._exists(key)) return false;\n this._driver.removeItem(this._getPrefix(key));\n\n this._event('locker.item.forgotten', { key: key });\n\n return true;\n };\n }\n\n /**\n * Define the public api\n *\n * @type {Object}\n */\n Locker.prototype = {\n\n /**\n * Add a new item to storage (even if it already exists)\n *\n * @param {Mixed} key\n * @param {Mixed} value\n * @return {self}\n */\n put: function (key, value) {\n if (! key) return false;\n key = _value(key);\n\n if (angular.isObject(key)) {\n angular.forEach(key, function (value, key) {\n this._setItem(key, value);\n }, this);\n } else {\n if (! angular.isDefined(value)) return false;\n this._setItem(key, _value(value, this._getItem(key)));\n }\n\n return this;\n },\n\n /**\n * Add an item to storage if it doesn't already exist\n *\n * @param {Mixed} key\n * @param {Mixed} value\n * @return {Boolean}\n */\n add: function (key, value) {\n if (! this.has(key)) {\n this.put(key, value);\n return true;\n }\n\n return false;\n },\n\n /**\n * Retrieve the specified item from storage\n *\n * @param {String|Array} key\n * @param {Mixed} def\n * @return {Mixed}\n */\n get: function (key, def) {\n if (angular.isArray(key)) {\n var items = {};\n angular.forEach(key, function (k) {\n if (this.has(k)) items[k] = this._getItem(k);\n }, this);\n\n return items;\n }\n\n if (! this.has(key)) return arguments.length === 2 ? def : void 0;\n\n return this._getItem(key);\n },\n\n /**\n * Determine whether the item exists in storage\n *\n * @param {String|Function} key\n * @return {Boolean}\n */\n has: function (key) {\n return this._exists(key);\n },\n\n /**\n * Remove specified item(s) from storage\n *\n * @param {Mixed} key\n * @return {Object}\n */\n forget: function (key) {\n key = _value(key);\n\n if (angular.isArray(key)) {\n key.map(this._removeItem, this);\n } else {\n this._removeItem(key);\n }\n\n return this;\n },\n\n /**\n * Retrieve the specified item from storage and then remove it\n *\n * @param {String|Array} key\n * @param {Mixed} def\n * @return {Mixed}\n */\n pull: function (key, def) {\n var value = this.get(key, def);\n this.forget(key);\n\n return value;\n },\n\n /**\n * Return all items in storage within the current namespace\n *\n * @return {Object}\n */\n all: function () {\n var items = {};\n angular.forEach(this._driver, function (value, key) {\n var split = key.split(this._separator);\n if (split.length > 1 && split[0] === this._namespace) {\n split.splice(0, 1);\n key = split.join(this._separator);\n }\n if (this.has(key)) items[key] = this.get(key);\n }, this);\n\n return items;\n },\n\n /**\n * Remove all items set within the current namespace\n *\n * @return {self}\n */\n clean: function () {\n this.forget(Object.keys(this.all()));\n\n return this;\n },\n\n /**\n * Empty the current storage driver completely. careful now.\n *\n * @return {self}\n */\n empty: function () {\n this._driver.clear();\n\n return this;\n },\n\n /**\n * Get the total number of items within the current namespace\n *\n * @return {Integer}\n */\n count: function () {\n return Object.keys(this.all()).length;\n },\n\n /**\n * Bind a storage key to a $scope property\n *\n * @param {Object} $scope\n * @param {String} key\n * @param {Mixed} def\n * @return {self}\n */\n bind: function ($scope, key, def) {\n if (angular.isUndefined( $scope.$eval(key) )) {\n $parse(key).assign($scope, this.get(key, def));\n if (! this.has(key)) this.put(key, def);\n }\n\n var self = this;\n this._watchers[key + $scope.$id] = $scope.$watch(key, function (newVal) {\n if (angular.isDefined(newVal)) self.put(key, newVal);\n }, angular.isObject($scope[key]));\n\n return this;\n },\n\n /**\n * Unbind a storage key from a $scope property\n *\n * @param {Object} $scope\n * @param {String} key\n * @return {self}\n */\n unbind: function ($scope, key) {\n $parse(key).assign($scope, void 0);\n this.forget(key);\n if (this._watchers[key + $scope.$id]) {\n // execute the de-registration function\n this._watchers[key + $scope.$id]();\n delete this._watchers[key + $scope.$id];\n }\n\n return this;\n },\n\n /**\n * Set the storage driver on a new instance to enable overriding defaults\n *\n * @param {String} driver\n * @return {self}\n */\n driver: function (driver) {\n return this.instance(driver, this._namespace);\n },\n\n /**\n * Get the currently set driver\n *\n * @return {Storage}\n */\n getDriver: function () {\n return this._driver;\n },\n\n /**\n * Set the namespace on a new instance to enable overriding defaults\n *\n * @param {String} namespace\n * @return {self}\n */\n namespace: function (namespace) {\n return this.instance(this._deriveDriver(this._driver), namespace);\n },\n\n /**\n * Get the currently set namespace\n *\n * @return {String}\n */\n getNamespace: function () {\n return this._namespace;\n },\n\n /**\n * Check browser support\n *\n * @see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js#L38-L47\n * @param {String} driver\n * @return {Boolean}\n */\n supported: function (driver) {\n return this._checkSupport(driver);\n },\n\n /**\n * Get a new instance of Locker\n *\n * @param {String} driver\n * @param {String} namespace\n * @return {Locker}\n */\n instance: function (driver, namespace) {\n return new Locker(driver, namespace);\n }\n };\n\n // return the default instance\n return new Locker(defaults.driver, defaults.namespace);\n }]\n };\n\n });\n\n});\n"],"sourceRoot":"/source/"} \ No newline at end of file +{"version":3,"sources":["angular-locker.min.js"],"names":["root","factory","define","amd","angular","exports","module","window","this","provider","_value","value","param","isFunction","_keyByVal","object","Object","keys","filter","key","_error","msg","Error","defaults","driver","namespace","eventsEnabled","separator","setDefaultDriver","getDefaultDriver","setDefaultNamespace","getDefaultNamespace","setEventsEnabled","enabled","getEventsEnabled","setSeparator","getSeparator","$get","$window","$rootScope","$parse","Locker","_registeredDrivers","local","localStorage","session","sessionStorage","_resolveDriver","hasOwnProperty","_deriveDriver","_driver","_namespace","_eventsEnabled","_separator","_watchers","_checkSupport","isUndefined","_supported","l","setItem","removeItem","e","_getPrefix","_serialize","toJson","_unserialize","fromJson","_event","name","payload","$emit","extend","_setItem","oldVal","_getItem","_exists","equals","oldValue","newValue","indexOf","getItem","_removeItem","prototype","put","isObject","forEach","isDefined","add","has","get","def","isArray","items","k","arguments","length","forget","map","pull","all","split","splice","join","clean","empty","clear","count","bind","$scope","$eval","assign","self","$id","$watch","newVal","unbind","watchId","instance","getDriver","getNamespace","supported"],"mappings":"CAUA,SAAWA,EAAMC,GACS,kBAAXC,SAAyBA,OAAOC,IACvCD,OAAO,WACH,MAAOD,GAAQD,EAAKI,WAEE,gBAAZC,SACdC,OAAOD,QAAUJ,EAAQD,EAAKI,SAAYG,QAAUA,OAAOH,SAE3DH,EAAQD,EAAKI,UAElBI,KAAM,SAAUJ,GAEf,YAEAA,GAAQE,OAAO,qBAEdG,SAAS,SAAU,WAShB,GAAIC,GAAS,SAAUC,EAAOC,GAC1B,MAAOR,GAAQS,WAAWF,GAASA,EAAMC,GAASD,GAUlDG,EAAY,SAAUC,EAAQJ,GAC9B,MAAOK,QAAOC,KAAKF,GAAQG,OAAO,SAAUC,GAAO,MAAOJ,GAAOI,KAASR,IAAU,IASpFS,EAAS,SAAUC,GACnB,KAAM,IAAIC,OAAM,oBAAsBD,IAQtCE,GACAC,OAAQ,QACRC,UAAW,SACXC,eAAe,EACfC,UAAW,IAGf,QASIC,iBAAkB,SAAUJ,GAGxB,MAFAD,GAASC,OAASd,EAAOc,GAElBhB,MAQXqB,iBAAkB,WACd,MAAON,GAASC,QAUpBM,oBAAqB,SAAUL,GAG3B,MAFAF,GAASE,UAAYf,EAAOe,GAErBjB,MAQXuB,oB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AU9B6F,SAAU,SAAU7F,EAAQC,GACxB,MAAO,IAAIgB,GAAOjB,EAAQC,KAK3B,GAAIgB,GAAOlB,EAASC,OAAQD,EAASE","file":"angular-locker.min.js","sourcesContent":["/**\n * angular-locker\n *\n * A simple & configurable abstraction for local/session storage in angular projects.\n *\n * @link https://github.com/tymondesigns/angular-locker\n * @author Sean Tymon @tymondesigns\n * @license MIT License, http://www.opensource.org/licenses/MIT\n */\n\n(function (root, factory) {\n if (typeof define === 'function' && define.amd) {\n define(function () {\n return factory(root.angular);\n });\n } else if (typeof exports === 'object') {\n module.exports = factory(root.angular || (window && window.angular));\n } else {\n factory(root.angular);\n }\n})(this, function (angular) {\n\n 'use strict';\n\n angular.module('angular-locker', [])\n\n .provider('locker', function () {\n\n /**\n * If value is a function then execute, otherwise return\n *\n * @param {Mixed} value\n * @param {Mixed} parameter\n * @return {Mixed}\n */\n var _value = function (value, param) {\n return angular.isFunction(value) ? value(param) : value;\n };\n\n /**\n * Get the key of an object by the value\n *\n * @param {Object} object\n * @param {Mixed} value\n * @return {String}\n */\n var _keyByVal = function (object, value) {\n return Object.keys(object).filter(function (key) { return object[key] === value; })[0];\n };\n\n /**\n * Trigger an error\n *\n * @param {String} msg\n * @return {void}\n */\n var _error = function (msg) {\n throw new Error('[angular-locker] ' + msg);\n };\n\n /**\n * Set the defaults\n *\n * @type {Object}\n */\n var defaults = {\n driver: 'local',\n namespace: 'locker',\n eventsEnabled: true,\n separator: '.'\n };\n\n return {\n\n /**\n * Allow setting of default storage driver via `lockerProvider`\n * e.g. lockerProvider.setDefaultDriver('session');\n *\n * @param {String|Function} driver\n * @return {self}\n */\n setDefaultDriver: function (driver) {\n defaults.driver = _value(driver);\n\n return this;\n },\n\n /**\n * Get the default driver\n *\n * @return {String}\n */\n getDefaultDriver: function () {\n return defaults.driver;\n },\n\n /**\n * Allow setting of default namespace via `lockerProvider`\n * e.g. lockerProvider.setDefaultNamespace('myAppName');\n *\n * @param {String|Function} namespace\n * @return {self}\n */\n setDefaultNamespace: function (namespace) {\n defaults.namespace = _value(namespace);\n\n return this;\n },\n\n /**\n * Get the default namespace\n *\n * @return {String}\n */\n getDefaultNamespace: function () {\n return defaults.namespace;\n },\n\n /**\n * Set whether the events are enabled\n *\n * @param {Boolean|Function} enabled\n * @return {self}\n */\n setEventsEnabled: function (enabled) {\n defaults.eventsEnabled = _value(enabled);\n\n return this;\n },\n\n /**\n * Get whether the events are enabled\n *\n * @return {Boolean}\n */\n getEventsEnabled: function () {\n return defaults.eventsEnabled;\n },\n\n /**\n * Set the separator to use with namespace in keys\n *\n * @param {String|Function} separator\n * @return {self}\n */\n setSeparator: function (separator) {\n defaults.separator = _value(separator);\n\n return this;\n },\n\n /**\n * Get the separator\n *\n * @return {String}\n */\n getSeparator: function () {\n return defaults.separator;\n },\n\n /**\n * The locker service\n */\n $get: ['$window', '$rootScope', '$parse', function ($window, $rootScope, $parse) {\n\n /**\n * Define the Locker class\n *\n * @param {Storage} driver\n * @param {String} namespace\n */\n function Locker (driver, namespace) {\n\n /**\n * Out of the box drivers\n * \n * @type {Object}\n */\n this._registeredDrivers = {\n local: $window.localStorage,\n session: $window.sessionStorage\n };\n\n /**\n * Get the Storage instance from the key\n *\n * @param {String} driver\n * @return {Storage}\n */\n this._resolveDriver = function (driver) {\n if (! this._registeredDrivers.hasOwnProperty(driver)) {\n _error('The driver \"' + driver + '\" was not found.');\n }\n\n return this._registeredDrivers[driver];\n };\n\n /**\n * Get the driver key (local/session) by the Storage instance\n *\n * @param {Storage} driver\n * @return {String}\n */\n this._deriveDriver = function (driver) {\n return _keyByVal(this._registeredDrivers, driver);\n };\n\n /**\n * @type {Storage}\n */\n this._driver = this._resolveDriver(driver);\n\n /**\n * @type {String}\n */\n this._namespace = namespace;\n\n /**\n * @type {Boolean}\n */\n this._eventsEnabled = defaults.eventsEnabled;\n\n /**\n * @type {String}\n */\n this._separator = defaults.separator;\n\n /**\n * @type {Object}\n */\n this._watchers = {};\n\n /**\n * Check browser support\n *\n * @see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js#L38-L47\n * @param {String} driver\n * @return {Boolean}\n */\n this._checkSupport = function (driver) {\n if (angular.isUndefined(this._supported)) {\n var l = 'l';\n try {\n this._resolveDriver(driver || 'local').setItem(l, l);\n this._resolveDriver(driver || 'local').removeItem(l);\n this._supported = true;\n } catch (e) {\n this._supported = false;\n }\n }\n\n return this._supported;\n };\n\n /**\n * Build the storage key from the namspace\n *\n * @param {String} key\n * @return {String}\n */\n this._getPrefix = function (key) {\n if (! this._namespace) return key;\n\n return this._namespace + this._separator + key;\n };\n\n /**\n * Try to encode value as json, or just return the value upon failure\n *\n * @param {Mixed} value\n * @return {Mixed}\n */\n this._serialize = function (value) {\n try {\n return angular.toJson(value);\n } catch (e) {\n return value;\n }\n };\n\n /**\n * Try to parse value as json, if it fails then it probably isn't json so just return it\n *\n * @param {String} value\n * @return {Object|String}\n */\n this._unserialize = function (value) {\n try {\n return angular.fromJson(value);\n } catch (e) {\n return value;\n }\n };\n\n /**\n * Trigger an event\n *\n * @param {String} name\n * @param {Object} payload\n * @return {void}\n */\n this._event = function (name, payload) {\n if (! this._eventsEnabled) return;\n\n $rootScope.$emit(name, angular.extend(payload, {\n driver: this._deriveDriver(this._driver),\n namespace: this._namespace,\n }));\n };\n\n /**\n * Add to storage\n *\n * @param {String} key\n * @param {Mixed} value\n */\n this._setItem = function (key, value) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n try {\n var oldVal = this._getItem(key);\n this._driver.setItem(this._getPrefix(key), this._serialize(value));\n if (this._exists(key) && ! angular.equals(oldVal, value)) {\n this._event('locker.item.updated', { key: key, oldValue: oldVal, newValue: value });\n } else {\n this._event('locker.item.added', { key: key, value: value });\n }\n } catch (e) {\n if (['QUOTA_EXCEEDED_ERR', 'NS_ERROR_DOM_QUOTA_REACHED', 'QuotaExceededError'].indexOf(e.name) !== -1) {\n _error('The browser storage quota has been exceeded');\n } else {\n _error('Could not add item with key \"' + key + '\"');\n }\n }\n };\n\n /**\n * Get from storage\n *\n * @param {String} key\n * @return {Mixed}\n */\n this._getItem = function (key) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n return this._unserialize(this._driver.getItem(this._getPrefix(key)));\n };\n\n /**\n * Exists in storage\n *\n * @param {String} key\n * @return {Boolean}\n */\n this._exists = function (key) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n return this._driver.hasOwnProperty(this._getPrefix(_value(key)));\n };\n\n /**\n * Remove from storage\n *\n * @param {String} key\n * @return {Boolean}\n */\n this._removeItem = function (key) {\n if (! this._checkSupport()) _error('The browser does not support localStorage');\n\n if (! this._exists(key)) return false;\n this._driver.removeItem(this._getPrefix(key));\n\n this._event('locker.item.forgotten', { key: key });\n\n return true;\n };\n }\n\n /**\n * Define the public api\n *\n * @type {Object}\n */\n Locker.prototype = {\n\n /**\n * Add a new item to storage (even if it already exists)\n *\n * @param {Mixed} key\n * @param {Mixed} value\n * @return {self}\n */\n put: function (key, value) {\n if (! key) return false;\n key = _value(key);\n\n if (angular.isObject(key)) {\n angular.forEach(key, function (value, key) {\n this._setItem(key, value);\n }, this);\n } else {\n if (! angular.isDefined(value)) return false;\n this._setItem(key, _value(value, this._getItem(key)));\n }\n\n return this;\n },\n\n /**\n * Add an item to storage if it doesn't already exist\n *\n * @param {Mixed} key\n * @param {Mixed} value\n * @return {Boolean}\n */\n add: function (key, value) {\n if (! this.has(key)) {\n this.put(key, value);\n return true;\n }\n\n return false;\n },\n\n /**\n * Retrieve the specified item from storage\n *\n * @param {String|Array} key\n * @param {Mixed} def\n * @return {Mixed}\n */\n get: function (key, def) {\n if (angular.isArray(key)) {\n var items = {};\n angular.forEach(key, function (k) {\n if (this.has(k)) items[k] = this._getItem(k);\n }, this);\n\n return items;\n }\n\n if (! this.has(key)) return arguments.length === 2 ? def : void 0;\n\n return this._getItem(key);\n },\n\n /**\n * Determine whether the item exists in storage\n *\n * @param {String|Function} key\n * @return {Boolean}\n */\n has: function (key) {\n return this._exists(key);\n },\n\n /**\n * Remove specified item(s) from storage\n *\n * @param {Mixed} key\n * @return {Object}\n */\n forget: function (key) {\n key = _value(key);\n\n if (angular.isArray(key)) {\n key.map(this._removeItem, this);\n } else {\n this._removeItem(key);\n }\n\n return this;\n },\n\n /**\n * Retrieve the specified item from storage and then remove it\n *\n * @param {String|Array} key\n * @param {Mixed} def\n * @return {Mixed}\n */\n pull: function (key, def) {\n var value = this.get(key, def);\n this.forget(key);\n\n return value;\n },\n\n /**\n * Return all items in storage within the current namespace/driver\n *\n * @return {Object}\n */\n all: function () {\n var items = {};\n angular.forEach(this._driver, function (value, key) {\n var split = key.split(this._separator);\n if (split.length > 1 && split[0] === this._namespace) {\n split.splice(0, 1);\n key = split.join(this._separator);\n }\n if (this.has(key)) items[key] = this.get(key);\n }, this);\n\n return items;\n },\n\n /**\n * Remove all items set within the current namespace/driver\n *\n * @return {self}\n */\n clean: function () {\n this.forget(Object.keys(this.all()));\n\n return this;\n },\n\n /**\n * Empty the current storage driver completely. careful now.\n *\n * @return {self}\n */\n empty: function () {\n this._driver.clear();\n\n return this;\n },\n\n /**\n * Get the total number of items within the current namespace\n *\n * @return {Integer}\n */\n count: function () {\n return Object.keys(this.all()).length;\n },\n\n /**\n * Bind a storage key to a $scope property\n *\n * @param {Object} $scope\n * @param {String} key\n * @param {Mixed} def\n * @return {self}\n */\n bind: function ($scope, key, def) {\n if (angular.isUndefined( $scope.$eval(key) )) {\n $parse(key).assign($scope, this.get(key, def));\n if (! this.has(key)) this.put(key, def);\n }\n\n var self = this;\n this._watchers[key + $scope.$id] = $scope.$watch(key, function (newVal) {\n if (angular.isDefined(newVal)) self.put(key, newVal);\n }, angular.isObject($scope[key]));\n\n return this;\n },\n\n /**\n * Unbind a storage key from a $scope property\n *\n * @param {Object} $scope\n * @param {String} key\n * @return {self}\n */\n unbind: function ($scope, key) {\n $parse(key).assign($scope, void 0);\n this.forget(key);\n\n var watchId = key + $scope.$id;\n \n if (this._watchers[watchId]) {\n // execute the de-registration function\n this._watchers[watchId]();\n delete this._watchers[watchId];\n }\n\n return this;\n },\n\n /**\n * Set the storage driver on a new instance to enable overriding defaults\n *\n * @param {String} driver\n * @return {self}\n */\n driver: function (driver) {\n return this.instance(driver, this._namespace);\n },\n\n /**\n * Get the currently set driver\n *\n * @return {Storage}\n */\n getDriver: function () {\n return this._driver;\n },\n\n /**\n * Set the namespace on a new instance to enable overriding defaults\n *\n * @param {String} namespace\n * @return {self}\n */\n namespace: function (namespace) {\n return this.instance(this._deriveDriver(this._driver), namespace);\n },\n\n /**\n * Get the currently set namespace\n *\n * @return {String}\n */\n getNamespace: function () {\n return this._namespace;\n },\n\n /**\n * Check browser support\n *\n * @see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js#L38-L47\n * @param {String} driver\n * @return {Boolean}\n */\n supported: function (driver) {\n return this._checkSupport(driver);\n },\n\n /**\n * Get a new instance of Locker\n *\n * @param {String} driver\n * @param {String} namespace\n * @return {Locker}\n */\n instance: function (driver, namespace) {\n return new Locker(driver, namespace);\n }\n };\n\n // return the default instance\n return new Locker(defaults.driver, defaults.namespace);\n }]\n };\n\n });\n\n});\n"],"sourceRoot":"/source/"} \ No newline at end of file diff --git a/package.json b/package.json index 84df53d..e51c6fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-locker", - "version": "1.2.0", + "version": "1.2.1", "description": "A simple & configurable abstraction for local/session storage in angular projects", "author": "@tymondesigns", "license": "MIT", diff --git a/src/angular-locker.js b/src/angular-locker.js index de2e2e5..88d9b88 100644 --- a/src/angular-locker.js +++ b/src/angular-locker.js @@ -59,7 +59,7 @@ }; /** - * Set the default driver and namespace + * Set the defaults * * @type {Object} */ @@ -76,7 +76,8 @@ * Allow setting of default storage driver via `lockerProvider` * e.g. lockerProvider.setDefaultDriver('session'); * - * @param {String} driver + * @param {String|Function} driver + * @return {self} */ setDefaultDriver: function (driver) { defaults.driver = _value(driver); @@ -86,6 +87,8 @@ /** * Get the default driver + * + * @return {String} */ getDefaultDriver: function () { return defaults.driver; @@ -95,7 +98,8 @@ * Allow setting of default namespace via `lockerProvider` * e.g. lockerProvider.setDefaultNamespace('myAppName'); * - * @param {String} namespace + * @param {String|Function} namespace + * @return {self} */ setDefaultNamespace: function (namespace) { defaults.namespace = _value(namespace); @@ -105,6 +109,8 @@ /** * Get the default namespace + * + * @return {String} */ getDefaultNamespace: function () { return defaults.namespace; @@ -113,7 +119,8 @@ /** * Set whether the events are enabled * - * @param {Boolean} enabled + * @param {Boolean|Function} enabled + * @return {self} */ setEventsEnabled: function (enabled) { defaults.eventsEnabled = _value(enabled); @@ -123,6 +130,8 @@ /** * Get whether the events are enabled + * + * @return {Boolean} */ getEventsEnabled: function () { return defaults.eventsEnabled; @@ -131,7 +140,8 @@ /** * Set the separator to use with namespace in keys * - * @param {String} separator + * @param {String|Function} separator + * @return {self} */ setSeparator: function (separator) { defaults.separator = _value(separator); @@ -141,6 +151,8 @@ /** * Get the separator + * + * @return {String} */ getSeparator: function () { return defaults.separator; @@ -160,6 +172,8 @@ function Locker (driver, namespace) { /** + * Out of the box drivers + * * @type {Object} */ this._registeredDrivers = { @@ -281,8 +295,8 @@ /** * Trigger an event * - * @param {String} name - * @param {Object} payload + * @param {String} name + * @param {Object} payload * @return {void} */ this._event = function (name, payload) { @@ -473,7 +487,7 @@ }, /** - * Return all items in storage within the current namespace + * Return all items in storage within the current namespace/driver * * @return {Object} */ @@ -492,7 +506,7 @@ }, /** - * Remove all items set within the current namespace + * Remove all items set within the current namespace/driver * * @return {self} */ @@ -554,10 +568,13 @@ unbind: function ($scope, key) { $parse(key).assign($scope, void 0); this.forget(key); - if (this._watchers[key + $scope.$id]) { + + var watchId = key + $scope.$id; + + if (this._watchers[watchId]) { // execute the de-registration function - this._watchers[key + $scope.$id](); - delete this._watchers[key + $scope.$id]; + this._watchers[watchId](); + delete this._watchers[watchId]; } return this;