我正在尝试以相反的顺序对Backbone.js集合进行排序.以前有关于如何使用整数执行此操作的回复,但没有带有字符串的回复.
var Chapter = Backbone.Model;
var chapters = new Backbone.Collection;
chapters.comparator = function(chapter) {
return chapter.get("title");
};
chapters.add(new Chapter({page: 9, title: "The End"}));
chapters.add(new Chapter({page: 5, title: "The Middle"}));
chapters.add(new Chapter({page: 1, title: "The Beginning"}));
alert(chapters.pluck('title'));
Run Code Online (Sandbox Code Playgroud)
上面的代码对A - > Z的章节进行了排序,但是如何编写一个比较器,从Z - > A中对它进行排序?
我有一个简单的backbone.js twitter应用程序,需要以相反的顺序排序推文.我目前按日期实施了一个比较器排序.单击"反向"按钮时(如视图中所示)如何反转排序所有推文而不返回比较器?我的印象是,当我调用sort时,它将尝试重新呈现列表(这意味着比较器将再次对数据进行排序,这是不合需要的).我该如何覆盖它?
Tweet = Backbone.Model.extend();
// Define the collection
Tweets = Backbone.Collection.extend(
{
model: Tweet,
// Url to request when fetch() is called
url: 'http://search.twitter.com/search.json?q=codinghorror',
parse: function(response) {
//modify dates to be more readable
$.each(response.results, function(i,val) {
val.created_at = val.created_at.slice(0, val.created_at.length - 6);
});
return response.results;
},
// Overwrite the sync method to pass over the Same Origin Policy
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: true …Run Code Online (Sandbox Code Playgroud)