jeromegn/Backbone.localStorage

Unable to set initial collection data

skotchio opened this issue · 2 comments

I have the following Collection:

var PlayersCollection = Backbone.Collection.extend({
   model: Player,
   localStorage: new Backbone.LocalStorage("players-backbone"),
});

and trying to set initial data by using:

var col = new PlayersCollection([
  {
    "model": "myapp.person",
    "pk": 1
  },
  {
    "model": "myapp.person",
    "pk": 2
  }
]);

But unfortunately I have no the items above in my collection:

col.localStorage.records // [] nothing

When you set models on a collection when instantiating it, you're not "saving" the collection with the models.

To make that work, you'll have to manually col.create(your_model) for every model.

col.localStorage.create(col); worked for me!