正确实现主干比较器 [英] correctly implement backbone comparators

查看:21
本文介绍了正确实现主干比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现主干比较器时有点卡住了,我基本上想根据路由选择不同的排序方法并使用比较器对集合进行排序.理想情况下,我希望将排序逻辑封装在集合中,但似乎卡住了.例如

I'm getting a bit stuck implemented a backbone comparator, I basically want to select different sorting methods based on the route and use the comparator to sort the collection. Ideally I want to keep the sorting logic encapsulated within the collection but seem to be getting stuck. For example

    Requests = Backbone.Collection.extend({
        model : Request,
        comparator : function(ab) {
            return -ab.id;
        },          
        nooffers : function() {
            return this.sortBy(function(ab) {               
                 return ab.get('offers');
            });
        }
    }); 

因此,默认情况下,它根据默认比较器进行排序 - 但在我的路由中,我希望能够使用例如做类似的事情

So by default it sorts based on the default comparator - but in my routing I wish to be able to resort e.g. do something like

   routes : {
        "" : "index",
        '/ordering/:order' : 'ordering'
    },
    ordering : function(theorder) {
        ordering = theorder;
        if(theorder == 'nooffers') {
            Request.comparator = Request.nooffers();
        }
        Request.sort();
        listView.render();  
        howitworksView.render();
    }

但是在那种情况下我得到一个错误('c.call 不是一个函数')有什么想法吗?

However in that case I get an error ('c.call is not a function') any ideas?

推荐答案

你在这里有一些错误.

这并不像你认为的那样:

This doesn't do what you think it does:

if(theorder == 'nooffers') {
    Request.comparator = Request.nooffers();
}

执行 nooffers 方法并将其结果分配给 Request.comparator.但是 sortBy 返回排序列表:

That executes the nooffers method and assigns its result to Request.comparator. But sortBy returns the sorted list:

nooffers : function() {
    return this.sortBy(function(ab) {               
        return ab.get('offers');
    });
}

并将该列表设置为比较器函数没有任何用处.

and setting that list as the comparator function doesn't do anything useful.

您想更改赋值以使用函数而不是其返回值:

You want to change the assignment to use the function rather that its return value:

if(theorder == 'nooffers') {
    Request.comparator = Request.nooffers;
}

并将函数更改为有效的比较器函数:

and change the function to be a valid comparator function:

nooffers : function(ab) {
    return ab.get('offers');
}

演示(在您的控制台打开时运行):http://jsfiddle.net/ambiguous/AAZCa/

Demo (run with your console open): http://jsfiddle.net/ambiguous/AAZCa/

但是让外面的人像那样摆弄该系列的方法很糟糕,你不应该这样做.相反,您应该要求集合更改其顺序,如下所示:

But having someone from the outside fiddling with the collection's methods like that smells bad and you shouldn't do it. Instead, you should ask the collection to change its ordering with something like this:

var Requests = Backbone.Collection.extend({
    model: Request,
    comparator: function(ab) {
        if(this._order_by == 'offers')
            return ab.get('offers');
        else if(this._order_by == 'id')
            return -ab.id;
        //...
    },
    order_by_offers: function() {
        this._order_by = 'offers';
        this.sort();
    },
    order_by_default: function() {
        this._order_by = 'id';
        this.sort();
    },
    _order_by: 'id'
});
//...
rs.order_by_offers();

演示:http://jsfiddle.net/ambiguous/uM9av/

或者你可以让集合交换它自己的 comarator 以避免 comarator 中的所有条件逻辑:

Or you could let the collection swap its own comparator to avoid all the conditional logic inside the comparator:

var Requests = Backbone.Collection.extend({
    model: Request,
    initialize: function() {
        this._order_by_id = this.comparator;
    },
    comparator: function(ab) {
        return -ab.id;
    },
    order_by_offers: function() {
        this.comparator = this._order_by_offers;
        this.sort();
    },
    order_by_default: function() {
        this.comparator = this._order_by_id;
        this.sort();
    },
    _order_by_offers: function(ab) {
        return ab.get('offers');
    }
});

演示:http://jsfiddle.net/ambiguous/Pjfq2/

这篇关于正确实现主干比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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