Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement MVVM with offline storage and Knockout.js?

I can implement Mvvm with Knockout.js. But I want to use it with cross browser(FF and Chrome) supported Html 5 offline storage.

I want to bind html objects to offline storage.

like image 470
Oguz Karadenizli Avatar asked Jul 22 '12 15:07

Oguz Karadenizli


People also ask

How do I get Knockout JS data?

prototype. loadNote = function (id) { var self = this; $. getJSON(uri + '/' + id, function (data) { self. note(data); }); }; // Apply bindings ko.

What is knockout js used for?

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.

What is Ko applyBindings?

The ko. applyBindings method activates Knockout and wires up the view-model to the view. Now that we have a view-model, we can create the bindings. In Knockout.js, you do this by adding data-bind attributes to HTML elements.


2 Answers

I haven't tried it, but there is a knockout.localStorage project on GitHub, that seems to be what you are looking for.

With that plugin, you should be able to pass an object as a second argument, when you create your observable, which saves the observable into localStorage.

From the documentation:

var viewModel = {
  name: ko.observable('James', {persist: 'name'})
}

ko.applyBindings(viewModel);
like image 177
Christofer Eliasson Avatar answered Sep 27 '22 16:09

Christofer Eliasson


You can use a library such as amplify.js which can serialize objects to localStorage (cross browser). It falls back to older storage tools for older browsers too. First, unwrap the observables to a JSON object, then use amplify.store to serialize the object and store it. Then you can pull it back out and map it back to an observable object when you want to fetch it.

http://amplifyjs.com/api/store/

like image 28
John Papa Avatar answered Sep 27 '22 18:09

John Papa