Backbone.js 主干路由-排序和筛选
在我的主干应用程序中,在我的集合上,我有许多排序方法,当根据集合呈现视图时,我当前通过路由使用一个全局变量集(当其他操作添加到集合中时,我使用一个全局变量集,我希望使用最后一个排序)。比如说Backbone.js 主干路由-排序和筛选,backbone.js,routes,Backbone.js,Routes,在我的主干应用程序中,在我的集合上,我有许多排序方法,当根据集合呈现视图时,我当前通过路由使用一个全局变量集(当其他操作添加到集合中时,我使用一个全局变量集,我希望使用最后一个排序)。比如说 routes : { "" : "index", '/ordering/:order' : 'ordering' }, ordering : function(theorder) { ordering = theorder; listView.rende
routes : {
"" : "index",
'/ordering/:order' : 'ordering'
},
ordering : function(theorder) {
ordering = theorder;
listView.render();
},
那么在我看来,
if (typeof ordering === 'undefined') {
d = this.collection.ordered();
}
else if(ordering == 'owners') {
d = this.collection.owners();
}
_.each(d, function(model){
model.set({request : self.model.toJSON()});
var view = new TB_BB.OfferItemView({model : model});
els.push(view.render().el);
});
其中订购和所有者是两种订购方法
所以我的第一个问题是,根据路线,有人能建议一种更好的方法来实现上述内容吗?该视图在多个位置渲染,因此我使用全局变量,而不是将有序变量传递给该方法
第二个问题是-我还想添加一些过滤,所以假设我想按“价格”排序,但也要进行一些过滤(比如按多个类别id)。如何添加灵活的“路线”来处理过滤
我想我能做到
routes : {
"" : "index",
'/ordering/:order/:filter1/:filter2' : 'ordering'
},
所以filter1和filter2将是后续的过滤,但是如果过滤器可以是0或100,那么这将不起作用。有人能提供解决方案吗?首先,您应该使用主干网的内置功能来自动排序集合。您可以通过在集合上定义函数来利用这一点。这为您提供了开箱即用的各种胜利-例如,每次您根据您的
比较器
添加或删除某个内容时,集合都会自动重新排序。如果要定义多个排序函数,只需将它们全部定义为函数,然后在需要时更新comparator
。然后你就可以抛弃这个丑陋的全局变量了
对于你的第二个问题,我不完全确定你所说的“如果过滤器可能是0或100,那么这将不起作用”是什么意思。如果你的意思是如果你没有指定所有的过滤器,你会遇到麻烦,那么这是真的。但您可以使用通配符来修复此问题。下面是可能的情况:
// your routes look like this:
routes : {
'/ordering/:order/filters/*filters' : 'ordering' // your routes will look like: /ordering/price/filters/filter_one/filter_two/filter_three
},
ordering: function (order, filters) {
filters = filters.split('/'); // creates an array of filters: ['filter_one', 'filter_two', 'filter_three']
listView.render(filters); // pass your filters to the view
}
// listView.render() looks like this:
render: function(filters) {
collection = this.collection;
_.each(filters, function (filter) {
collection = collection.filter(function () {
// your actual filtering code based on what the filter is
});
});
}
首先,您应该使用主干网的内置功能对集合进行自动排序。您可以通过在集合上定义函数来利用这一点。这为您提供了开箱即用的各种胜利-例如,每次您根据您的
比较器
添加或删除某个内容时,集合都会自动重新排序。如果要定义多个排序函数,只需将它们全部定义为函数,然后在需要时更新comparator
。然后你就可以抛弃这个丑陋的全局变量了
对于你的第二个问题,我不完全确定你所说的“如果过滤器可能是0或100,那么这将不起作用”是什么意思。如果你的意思是如果你没有指定所有的过滤器,你会遇到麻烦,那么这是真的。但您可以使用通配符来修复此问题。下面是可能的情况:
// your routes look like this:
routes : {
'/ordering/:order/filters/*filters' : 'ordering' // your routes will look like: /ordering/price/filters/filter_one/filter_two/filter_three
},
ordering: function (order, filters) {
filters = filters.split('/'); // creates an array of filters: ['filter_one', 'filter_two', 'filter_three']
listView.render(filters); // pass your filters to the view
}
// listView.render() looks like this:
render: function(filters) {
collection = this.collection;
_.each(filters, function (filter) {
collection = collection.filter(function () {
// your actual filtering code based on what the filter is
});
});
}
非常感谢Josh,比较器似乎正是我所需要的!过滤也是如此!非常感谢Josh,比较器似乎正是我所需要的!过滤也是如此!