The collection in question grabs a bunch of Friends, and then it uses reset
to save all the new Friends. The problem I am having, is that it only adds 1 friend to the collection.
I did a console.log
on cModels
and it was showing over 300 objects in the Array, so I know there isn't only 1. Any reason why the reset would only be adding 1?
Here is my collection:
FriendsCollection = Backbone.Collection.extend({
model: UserModel,
getFriends: function () {
$.post(baseUrl + dataFriendsUrl, $.proxy(function(data) {
var friends = $.parseJSON(data);
var cModels = [];
for(var key in friends.data) {
var user = new UserModel();
user.set({
fbid: friends.data[key].id,
username: friends.data[key].name
});
cModels.push(user);
}
this.reset(cModels);
console.log(this);
}, this));
}
});
And the models look fine, after inspecting them with the developer tools.
js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)
js respectively. The rest of your application code should be divided into modules that can live under their own modules directory. A module is an encapsulated group of structures (for the purposes of our post, Backbone structures) that work cohesively to provide a subset of functionality in your application.
Backbone was created by Jeremy Ashkenas, who is also known for CoffeeScript and Underscore.js.
It's probably because the models have the same id.
A Backbone collection does not allow models with duplicate id values. (see this commit)
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