JSON doesn't fill the backbone collection directly
I have JSON that is returned from the server, and I'm populating a
Backbone collection with it.
Models
Person = Backbone.Model.extend({
});
PersonCollection = Backbone.Collection.extend({
model: Person
});
The JSON for example-
var jsonString = "[
{ \"name\": \"Anna\", \"id\": 5 },
{ \"name\": \"Lina\", \"id\": 26 },
{ \"name\": \"Melissa\", \"id\": 55 }
]"
var people = JSON.parse(jsonString); //people is now an array of 3 persons
Works:
var personCollection = new PersonCollection();
for (var i = 0; i < people.length; i++) {
personCollection.add(people[i]);
}
var personCollectionView = new PersonCollectionView({ collection:
personCollection});
Doesn't work :(
var personCollectionView = new PersonCollectionView({ collection: people});
My JSON is returning an array of models, I'm not sure why I can't pass
this array directly to the PersonCollectionView? Is there something I need
to do in the parse method of PersonCollection, such that when given an
array of models, it will convert it into a collection of models?
I'm sure I'm missing something. Can someone point out what that is?
No comments:
Post a Comment