can't iterate over backbone collection, could be a fetch/bind issue
-
13-06-2021 - |
Question
I'm grabbing a list of a users friends, into a collection, and I want to iterate over those friends so that the user can select a friend and do a bunch of stuff later.
in my initalize I've got
friends = new MyApp.Collections.UserFriends(); friends.fetch({ data: $.param({ search: this.model.attributes.user_id}), success: function(){ }, error: function(){ alert('error getting friends'); } }); friends.bind('fetched', this.render(),this);
Then in my render I have
console.log(friends); console.log(HandlebarsTemplates['friends/friendsResults'](friends); $(this.el).html(HandlebarsTemplates['friends/friendsResults'](friends);
my handlebars is
ok, I have the right handlebar template {{#each models}} <li> {{attributes.username}}</li> {{/each}}
the friends collection in the console is, so I can see that the models are there.
byCid: Object _byId: Object _callbacks: Object length: 39 models: Array[39] __proto__: q
my concern was that the problem was that render was being triggered before the collection was made, but that is what I thought the friends.bind('fetched',this.render(), this)
was supposed to resolve.
Solution
There is no 'fetched' event in Backbone, try to use 'reset', http://backbonejs.org/#FAQ-events.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow