我有一个像这样的 Backbone 集合
var ContactsCollection = Backbone.Collection.extend({
model: Contact,
initialize: function () {
//retrieves contacts from web service
//contactsLoaded: is the callback that gets called after
//the contacts get received
loadContacts(this.contactsLoaded);
},
contactsLoaded: function (contacts) {
for (var i = 0; i < contacts.length; i++) {
//TODO populate the collection [models][1]
}
}
});
换句话说,我想自行填充集合的 models , 我该怎么做?
最佳答案
考虑使用 REST API 作为 Collection#fetch应该完全按照您的意愿行事。
var ContactsCollection = Backbone.Collection.extend({
model: Contact,
url: '', // set the URL
initialize: function () {
this.fetch();
}});
关于javascript - Backbone JS : self populate Collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15907026/