这就是我的集合视图的样子,我将如何在集合中排列我的模型,以便最近添加的模型呈现在顶部?
PostsApp.Views.Posts = Backbone.View.extend({
initialize: function(){
this.listenTo(this.collection, 'add', this.addOne);
},
render: function(){
this.collection.forEach(this.addOne, this);
},
addOne: function(post){
var postView = new PostsApp.Views.Post({model:post, collection :this.collection});
postView.render();
this.$el.append(postView.el);
}
});
编辑: Prepend 方法似乎有效,但我也尝试过这样的比较器,它似乎不起作用,我的问题是什么?
PostsApp.Models.Post = Backbone.Model.extend({
urlRoot : '/tweet',
idAttribute: '_id',
defaults:{
name: '',
adress: '',
pictureUrl: '',
postListing: '',
comments: '',
date_created: new Date()
}
}
});
PostsApp.Collections.Posts = Backbone.Collection.extend({
model: PostsApp.Models.Post,
url: '/tweet',
comparator: function(post){
return post.get("date_created");
}
});