一个排序集合骨干经过初始化 [英] Sorting a Backbone Collection After initialization
问题描述
我使用Backbone.js的渲染项目例如书籍的列表。列表被呈现之后,存在用于用户选择对它们进行排序。因此,如果用户点击排序方式标题,或排序按作者姓名列表将在客户端自行解决。
I am using Backbone.js to render a list of items e.g Books. After the list is rendered, there are options for user to sort them. So if user clicks on Sort By Title, or Sort By Author Name the list will sort itself on the client side.
window.Book = Backbone.Model.extend({
defaults: {
title: "This is the title",
author: "Name here"
},
这是什么来完成这种使用一个骨干申请的上下文中的最佳方式。我用一个jQuery DOM分拣机在APPVIEW?
What is the best way to accomplish this sort using in the context of a Backbone application. Do I use a jQuery dom sorter in the AppView?
推荐答案
有关于这个,你可能想看看很有话题的讨论:<一href=\"https://github.com/documentcloud/backbone/issues/41\">https://github.com/documentcloud/backbone/issues/41.
There's a discussion on this very topic that you might want to look at: https://github.com/documentcloud/backbone/issues/41.
短的那就是当用户选择用X排序,您可以:
The short of it is that when a user selects 'sort by X', you can:
- 设置上收集的
比较
功能 - 调用集合的
排序
功能(这将引发排序
事件) - 监听视图中的
排序
事件,(清晰)重绘项目
- Set the
comparator
function on the Collection - Call the Collection's
sort
function (which will trigger asort
event) - Listen for the
sort
event in your View, and (clear and) redraw the items
另一种方式来处理步骤1安培;二是有自己的方法调用集合的 sortBy
方法,然后触发您的视图可以收听到自定义事件。
Another way to handle steps 1 & 2 is to have your own method that calls the Collection's sortBy
method and then triggers a custom event that your View can listen to.
但它似乎是清算和重绘是最简单的(甚至最快)的方式进行排序您查看的,并让他们与你收集的排序顺序同步的情况。
But it seems to be the case that clearing and redrawing is the easiest (and maybe even the fastest) way to sort your View's and keep them in sync with your Collection's sort order.
这篇关于一个排序集合骨干经过初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!