Reputation: 11368
I'm trying to initialize a collection in backbone by using fetch, I have this in my view:
initialize: function()
{
_.bindAll(this, 'render', 'addItem', 'appendItem');
this.collection = new MembersCollection();
this.collection.bind('add', this.appendItem);
this.collection.fetch({success: _.bind(function(blah)
{
console.log(blah);
this.render();
}, this)});
}
It seems to be working, I get a successful response, with the correct number of models, it renders the correct number of views for each model, however, if I inspect the model.attribute value it only contains my default values and no values from the server?
If i check my JSON response I do see the proper data there, is there anything I might have missed that is required to get the data to set?
Upvotes: 1
Views: 724
Reputation: 3246
I had the same issue -- mine was caused by model's parse method.
Turns out the model's parse method is called when a model is created for each item in the array returned by the collection's fetch call.
The json/object returned from a direct model fetch was different to the json/object in the collection's returned array -- this caused my model's parse method to return undefined when created from an array item.
Upvotes: 0
Reputation: 5707
I think I had a similar issue recently.. Removing my defaults from the model definition solved the problem and I didn't need it so left it at that. I think, maybe, that if you define defaults, then you need to include all the attributes you are going to set.. but don't quote me on that.. Try it without defaults and see how it goes. If you still have issues you'll probably want to give us your model definition and a sample of the json returned in the response.
Upvotes: 2