Reputation: 4161
I have just started integrating backbone.js in my application. I have a question about traversing each model in collection.
I basically want to get urls of each model in the collection and attach those urls to its corresponding model. I'm doing it this way right now, and I just want to know if there is any other way of doing it?
getItemsURLs: function(collection){
var idsInCollection = [];
collection.each(function(model) {
idsInCollection.push(model.get('id'));
});
makeServiceCall({
data: idsInCollection,
success: function(data) {
collection.each(function(model,i) {
model.set({ url: data.urls[i]});
});
}
});
}
Upvotes: 0
Views: 278
Reputation: 25387
Using pluck
is more concise:
var idsInCollection = collection.pluck('id');
(As pointed out by asawyer, pluck
is more concise than map
or each
.)
Upvotes: 2