@@ -92,35 +92,61 @@ angular.module('localStorage', ['ngCookies']).factory('$store', ['$parse', '$coo
9292
9393 /**
9494 * Bind - let's you directly bind a localStorage value to a $scope variable
95- * @param $scope - the current scope you want the variable available in
96- * @param key - the name of the variable you are binding
97- * @param def - the default value (OPTIONAL)
95+ * @param {Angular $scope } $scope - the current scope you want the variable available in
96+ * @param {String } key - the name of the variable you are binding
97+ * @param {Object } opts - (optional) custom options like default value or unique store name
98+ * Here are the available options you can set:
99+ * * defaultValue: the default value
100+ * * storeName: add a custom store key value instead of using the scope variable name
98101 * @returns {* } - returns whatever the stored value is
99102 */
100- bind : function ( $scope , key , def ) {
101- // If no defined value for def we use empty string
102- def = ( angular . isUndefined ( def ) ) ? '' : def ;
103- if ( ! publicMethods . get ( key ) ) {
104- publicMethods . set ( key , def ) ;
103+ bind : function ( $scope , key , opts ) {
104+ var defaultOpts = {
105+ defaultValue : '' ,
106+ storeName : ''
107+ } ;
108+ // Backwards compatibility with old defaultValue string
109+ if ( angular . isString ( opts ) ) {
110+ opts = angular . extend ( { } , defaultOpts , { defaultValue :opts } ) ;
111+ } else {
112+ // If no defined options we use defaults otherwise extend defaults
113+ opts = ( angular . isUndefined ( opts ) ) ? defaultOpts : angular . extend ( defaultOpts , opts ) ;
114+ }
115+
116+ // Set the storeName key for the localStorage entry
117+ // use user defined in specified
118+ var storeName = opts . storeName || key ;
119+
120+ // If a value doesn't already exist store it as is
121+ if ( ! publicMethods . get ( storeName ) ) {
122+ publicMethods . set ( storeName , opts . defaultValue ) ;
105123 }
106- $parse ( key ) . assign ( $scope , publicMethods . get ( key ) ) ;
124+
125+ // If it does exist assign it to the $scope value
126+ $parse ( key ) . assign ( $scope , publicMethods . get ( storeName ) ) ;
127+
128+ // Register a listener for changes on the $scope value
129+ // to update the localStorage value
107130 $scope . $watch ( key , function ( val ) {
108131 if ( angular . isDefined ( val ) ) {
109- publicMethods . set ( key , val ) ;
132+ publicMethods . set ( storeName , val ) ;
110133 }
111134 } , true ) ;
112- return publicMethods . get ( key ) ;
135+
136+ return publicMethods . get ( storeName ) ;
113137 } ,
114138 /**
115139 * Unbind - let's you unbind a variable from localStorage while removing the value from both
116140 * the localStorage and the local variable and sets it to null
117141 * @param $scope - the scope the variable was initially set in
118142 * @param key - the name of the variable you are unbinding
143+ * @param storeName - (optional) if you used a custom storeName you will have to specify it here as well
119144 */
120- unbind : function ( $scope , key ) {
145+ unbind : function ( $scope , key , storeName ) {
146+ storeName = storeName || key ;
121147 $parse ( key ) . assign ( $scope , null ) ;
122148 $scope . $watch ( key , function ( ) { } ) ;
123- publicMethods . remove ( key ) ;
149+ publicMethods . remove ( storeName ) ;
124150 } ,
125151 /**
126152 * Clear All - let's you clear out ALL localStorage variables, use this carefully!
0 commit comments