Last active
January 10, 2016 08:44
-
-
Save ismnoiet/591ff134f99e0b2c3861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// first include underscore,backbone and backbone.localStorage | |
//<script src="bower_components/underscore/underscore.js"></script> | |
//<script src="bower_components/backbone/backbone.js"></script> | |
//<script src="bower_components/backbone.localStorage/backbone.localStorage.js"></script> | |
// 1) defining the model | |
var Model = Backbone.Model.extend({ | |
defaults:function(){ | |
return { | |
note:"hello" | |
}; | |
} | |
}); | |
// 2) define the collection | |
var Collection = Backbone.Collection.extend({ | |
model:Model, | |
localStorage: new Backbone.LocalStorage("localStorage_item_name") | |
}); | |
// 3) instantiate the collection and fetch data | |
var myCollection = new Collection(); | |
myCollection.fetch(); | |
// 4) create and add models to collection | |
var model1 = new Model({important:'hello'}); | |
myCollection.add(model1); | |
// 5) now the last thing is to save the model permanently to localStorage | |
// note here that saving model must comes after adding model to collection | |
// otherwise it will raise the exception 'url' must be specified is raised. | |
model1.save(); | |
// in case you already have a localStorage collection and you want to retrieve data from it then : | |
var col = new Collection(); | |
col.fetch(); // fetch the data from the localStorage | |
console.log(col.models); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment