Skip to content

Commit 2d34df9

Browse files
committed
added a new paramater options in place of just a defaultValue string. with documentation and tests
1 parent efcae18 commit 2d34df9

3 files changed

Lines changed: 80 additions & 25 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ The simpliest localStorage module you will ever use. Allowing you to set, get, a
1717
``yourApp.controller('yourController', function( $scope, $store){``
1818
3. Using the ``$store`` factory
1919
```
20-
// binding it to a $scope.variable
21-
$store.bind($scope,'varName','someDefaultValue');
22-
// the params are ($scope, varName, defaultValue(optional))
20+
// binding it to a $scope.variable (minimal)
21+
$store.bind($scope,'varName');
22+
// binding full
23+
$store.bind($scope,'varName',{defaultValue: 'randomValue123' ,storeName: 'customStoreKey'});
24+
// the params are ($scope, varName, opts(optional))
2325
// $scope - pass a reference to whatever scope the variable resides in
2426
// varName - the variable name so for $scope.firstName enter 'firstName'
25-
// defaultValue - if you want to set a default value if there is no localStorage value so that the first time you initiate it the value is assigned
27+
// opts - custom options like default value or unique store name
28+
// Here are the available options you can set:
29+
// * defaultValue: the default value
30+
// * storeName: add a custom store key value instead of using the scope variable name
2631
2732
// will constantly be updating $scope.viewType
2833
// to change the variable both locally in your controller and in localStorage just do

src/localStorage.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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!

test/localFactory.spec.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,40 @@ describe('angular-localStorage module', function () {
4949
expect(testValue).toEqual(scope.spec);
5050
});
5151

52-
it('should not store undefined value', function () {
53-
scope.$apply(function () {
54-
scope.spec = undefined;
55-
});
56-
57-
expect(testValue).toEqual(false);
58-
expect(scope.spec).toBeUndefined();
59-
});
52+
it('should not store undefined value', function () {
53+
scope.$apply(function () {
54+
scope.spec = undefined;
55+
});
56+
57+
expect(testValue).toEqual(false);
58+
expect(scope.spec).toBeUndefined();
59+
});
60+
61+
it('should store default value when passed as string', function() {
62+
scope.$apply(function(){
63+
$store.bind(scope,'defaultedSpec','someDefault');
64+
});
65+
expect(scope.defaultedSpec).toEqual('someDefault');
66+
});
67+
68+
it('should store default value when passed as options object', function() {
69+
scope.$apply(function(){
70+
$store.bind(scope,'defaultedSpecObj',{defaultValue: 'someNewDefault'});
71+
});
72+
expect(scope.defaultedSpecObj).toEqual('someNewDefault');
73+
});
74+
75+
it('using a custom storeName to bind variable', function() {
76+
scope.$apply(function(){
77+
$store.bind(scope,'customStored',{defaultValue: 'randomValue123' ,storeName: 'myCustomStore'});
78+
scope.directFromLocal = $store.get('myCustomStore');
79+
});
80+
expect(scope.customStored).toEqual('randomValue123');
81+
expect(scope.directFromLocal).toEqual('randomValue123');
82+
});
6083
});
6184

85+
6286
describe('when unbind() variable that clears localStorage and the variable', function () {
6387
var testLocalStorageValue, testLocalVariableValue;
6488

0 commit comments

Comments
 (0)