backbone.jsHow do I create and use stores in a Backbone.js application?
Creating and using stores in a Backbone.js application is done by creating a Model or Collection and then setting the localStorage
property on it.
For example, to create a Model with localStorage and save it:
var Todo = Backbone.Model.extend({
localStorage: new Backbone.LocalStorage("todo-store")
});
var todo = new Todo({name: "My Todo"});
todo.save();
To use the stored values, you can call fetch()
on the Model or Collection:
todo.fetch();
The parts of this code are:
var Todo = Backbone.Model.extend({
- creating a Backbone ModellocalStorage: new Backbone.LocalStorage("todo-store")
- setting the localStorage property on the Modelvar todo = new Todo({name: "My Todo"});
- creating a new instance of the Modeltodo.save();
- saving the Model to localStoragetodo.fetch();
- fetching the Model from localStorage
Helpful links
More of Backbone.js
- How do I use a template engine with Backbone.js?
- How do I use W3Schools to learn Backbone.js?
- How do I create tabs using Backbone.js?
- How do I create a sample application using Backbone.js?
- How do I use Backbone.js to create a YouTube video player?
- How do I use Backbone.js to create a wiki?
- How do Backbone.js and Express differ in their usage for software development?
- How can I migrate from Backbone.js to React?
- How can I use a template in Backbone.js?
- How can I use Backbone.js to render a view?
See more codes...