1

I have a a angular factory class User which gets instantialted via a dynamic view filling in data from the view to the user variable which is then bound to the view.

I need to update the localstorage when ever there is a change to the userView.Name which is bound to the view input type='text' i.e. any change to the value in the text field directly changes the text in the user instance of the class and then updates the dynamic view which would update the localStorage ?

I have a sample of the code hosted at CodePen

Sample Code illustrating the problem

app.controller('TestController', ['$scope', 'Loki', 'MobileConstants', 'LocalStorageApi', 'User', 
function ($scope, Loki, MobileConstants, LocalStorageApi, User, Book) {
    LocalStorageApi.initialize();
    var userData = LocalStorageApi.UsersView.data()[0];

    var user = new User(userData);

    $scope.userView = user;
}]);

app.factory('User', ['$rootScope', function ($rootScope) {
    function User(userData) {
        User.prototype.Name = userData.nameSample;
    };

    User.prototype = {
        Name: null,
    }

    return User;
}]);

below is how the code would look like illustrating the problem

Angular Code

Html View with data bound to it.

user581157
  • 1,327
  • 4
  • 26
  • 64

1 Answers1

2

LokiJS's default storage adapter in a browser is localstorage, so if you have the user saved in a LokiJS collection all you need to do is call db.saveDatabase() and it will automatically update the localstorage. In other words, you don't need to fiddle with localStorage directly at all, unless you are saving values that are not stored in LokiJS.

Joe Minichino
  • 2,793
  • 20
  • 20