Let's suppose I can get from the javascript console the following result:
var ls = new Backbone.LocalStorage("items");
ls; // {"name":"items","records":["1244f588-be3d-c493-5c86-b2abb997af82"]}
how should I get the Backbone.Collection
from the Backbone.LocalStorage
?
P.S.:
the collection looks like
[
{
"title":"test",
"completed":false,
"order":1,
"id":"1244f588-be3d-c493-5c86-b2abb997af82"
},
{
"title":"test2",
"completed":false,
"order":2,
"id":"8a8658b9-b636-eac3-4c54-03c279a73c2d"
}
]
To get items from localStorage, use the getItem() method. getItem() allows you to access the data stored in the browser's localStorage object.
localStorage An implementation of Client-side Storage. JSON (JavaScript Object Notation) a syntax for serializing objects, arrays, numbers, strings, booleans, and null.
js is a JavaScript rich-client web app framework based on the model–view–controller design paradigm, intended to connect to an API over a RESTful JSON interface. Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore.
Either create an empty collection with collection.localStorage
set to your Backbone.LocalStorage
object and fetch it:
var c = new Backbone.Collection();
c.localStorage = new Backbone.LocalStorage("items");
c.fetch();
console.log(c.pluck('id'));
or use findAll
on your Backbone.LocalStorage
object to get an array of models in storage:
var ls = new Backbone.LocalStorage("items");
console.log(ls.findAll());
A Fiddle to play with http://jsfiddle.net/nikoshr/8pHNG/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With