主干路由 - 排序和过滤

发布于 2025-01-08 14:01:34 字数 1054 浏览 1 评论 0原文

在我的 Backbone 应用程序中,在我的集合上,我有多种排序方法,当基于集合渲染视图时,我当前正在通过路由使用全局变量集(当其他操作添加到集合中时,我使用全局变量来执行此操作,并且我想要最后使用的顺序)。例如

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,这将不起作用。有人能提供解决方案吗?

In my Backbone app, on my collection I have numerous sorting methods, when rendering the views based on the collection I am currently using a global var set via the route (I do it with a global as other actions add to the collection and I want the last ordering to be used). For example

routes : {
    "" : "index",           
    '/ordering/:order' : 'ordering'
},
ordering : function(theorder) {
    ordering = theorder;
    listView.render();  
},

then in my view

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

Where ordered and owners are the 2 ordering methods.

So my first question is, based on routes could someone advice a better way of implementing above? This view gets rendered in multiple places hence me using a global rather than passing a ordered var to the method?

Second question is - I would like to also add some filtering, so lets say I want to sort by 'price' but also do some filtering (lets say by multiple categories id). How could I add a flexible 'route' to deal with filtering.

I guess I could do

routes : {
    "" : "index",
    '/ordering/:order/:filter1/:filter2' : 'ordering'
},

So the filter1 and filter2 would be the subsequent filtering, but if the filters could be 0 or 100 this will not work. Could anyone offer a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2025-01-15 14:01:34

好吧,首先您应该使用 Backbone 的内置功能来自动对集合进行排序。您可以通过定义 comparator 函数来利用这一点在你的收藏上。这为您提供了各种开箱即用的优势 - 例如,每次您添加或删除某些内容时,集合都会根据您的比较器重新排序。如果您想定义多个排序函数,只需将它们全部定义为函数,然后在需要时更新 comparator 即可。然后你就可以抛弃那个丑陋的全局var

对于你的第二个问题,我不完全确定你所说的“如果过滤器可以是 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
    });
  });
}

Well, first you should be using Backbone's built-in ability to auto-sort collections. You can take advantage of this by defining a comparator function on your collection. This gives you all kinds of wins right out of the box — for example, the collection will re-sort itself every time you add or remove something from it, based on your comparator. If you want to define multiple sort functions, just define them all as functions and then update comparator when you need to. Then you can ditch that ugly global var.

For your second question, I'm not totally sure what you mean by "if the filters could be 0 or 100 this will not work." If you mean that you'll run into trouble if you don't specifiy all of the filters, then that's true. But you can use a wildcard to fix that. Here's what that might look like:

// 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
    });
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文