grevory/angular-local-storage — pull request #124 by yihangho (2014-08-23 14:39:44+00:00)
Title: Improve binding

This PR addresses two annoyances (that I face personally when using this module):

1. The key of the scope must match the key of local storage. For e.g., if I want to bind `$scope.property`, the corresponding key used in local storage must also be `property`. I added the forth and optional argument to the `bind` method to specify the key to be used in local storage. So if I do `.bind($scope, 'property', null, 'another')`, `$scope.property` will bind to `another` in local storage.

2. Inconsistency when `key` contains `.`. For example, suppose we run `.clearAll` beforehand, then run `.bind($scope, 'obj.property', 'default')`, `$scope['obj.property']` (instead of `$scope.obj.property`) is set to `default`, i.e., `$scope['obj.property'] == 'default'`. And then, `obj.property` is then immediately overwritten with `null` as it is actually binding with `$scope.obj.property`, which is `null` (for the sake of giving an example, assume that `$scope` is untouched). What I did for this problem is to correctly bind `$scope.obj.property` instead of `$scope['obj.property']`.

To summarize:

Before my changes:

```javascript
var $store = localStorageService;
$store.bind($scope, 'obj.property', 'val');
$scope['obj.property'] == 'val';
$scope.obj.property == null;
// A tiny little while later
$store.get('obj.property') == null; // It is binding to $scope.obj.property
```

After my changes:

```javascript
var $store = localStorageService;
$store.bind($scope, 'obj.property', 'val');
$scope.obj.property == 'val';
$store.get('obj.property') == 'val';
$scope.obj.property = 'newVal';
$store.get('obj.property') == 'newVal';

// The forth optional key
$store.bind($scope, 'property', 'bla', 'another');
$scope.property = 'hello';
$store.get('another') == 'hello';
```