使用 Backbone.js 反向排序 [英] Reverse sort order with Backbone.js

查看:21
本文介绍了使用 Backbone.js 反向排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Backbone.js 我已经设置了一个带有比较器功能的集合.它很好地对模型进行了排序,但我想颠倒顺序.

With Backbone.js I've got a collection set up with a comparator function. It's nicely sorting the models, but I'd like to reverse the order.

如何按降序而不是升序对模型进行排序?

How can I sort the models in descending order rather than ascending?

推荐答案

好吧,您可以从比较器返回负值.例如,如果我们采用 Backbone 网站上的示例并希望颠倒顺序,它将如下所示:

Well, you can return negative values from comparator. If we take, for example, the example from Backbone's site and want to reverse the order, it will look like this:

var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
  return -chapter.get("page"); // Note the minus!
};

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'));

这篇关于使用 Backbone.js 反向排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆