我做了我的第一个骨干应用程序,并收集排序问题。 使用该骨干收集sortBy
var SortedFriends = MyFriends.sortBy(function(friend) {
return friend.get("uid");
});
的console.log后(SortedFriends)显示,SortedFriends包含排序的模型,但是当我尝试使用收藏功能,如“SortedFriends.each”或“SortedFriends.at”它使错误:
TypeError:SortedFriends.each不是函数。
代码:
var Friend = Backbone.Model.extend({});
var Friends = Backbone.Collection.extend({
model: Friend,
});
var MyFriends = new Friends();
MyFriends.reset(<?=$friends?>);
var FriendView = Backbone.View.extend({
initialize: function(){
model:Friend
},
tagName: "tr",
template: _.template($('#item-template').html()),
className: "document-row",
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var SortedFriends = MyFriends.sortBy(function(friend) {
return friend.get("uid");
});
var addOne = function(element){
var view = new FriendView({model: element});
$("#friends").append(view.render().el);
}
console.log(JSON.stringify(SortedFriends));
SortedFriends.each(function(friend){
var view = new FriendView({model: friend});
$("#friends").append(view.render().el);
});
此帮助 VAR SortedFriends = _(MyFriends.sortBy(函数(朋友){ 回报friend.get( “UID”); })); 谢谢! – fyodor