首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正确实现主干比较器

正确实现主干比较器
EN

Stack Overflow用户
提问于 2012-02-24 20:56:43
回答 2查看 20.8K关注 0票数 22

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

代码语言:javascript
复制
    Requests = Backbone.Collection.extend({
        model : Request,
        comparator : function(ab) {
            return -ab.id;
        },          
        nooffers : function() {
            return this.sortBy(function(ab) {               
                 return ab.get('offers');
            });
        }
    }); 

因此,默认情况下,它基于默认的比较器进行排序-但在我的路由中,我希望能够重新排序,例如,执行以下操作

代码语言:javascript
复制
   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不是一个函数‘),有什么想法吗?

EN

Stack Overflow用户

回答已采纳

发布于 2012-02-25 02:28:00

这里有一些地方是错误的。

这并不是你所想的那样:

代码语言:javascript
复制
if(theorder == 'nooffers') {
    Request.comparator = Request.nooffers();
}

它执行nooffers方法并将其结果赋值给Request.comparator。但是sortBy返回排序后的列表:

代码语言:javascript
复制
nooffers : function() {
    return this.sortBy(function(ab) {               
        return ab.get('offers');
    });
}

并且将该列表设置为比较器函数不会做任何有用的事情。

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

代码语言:javascript
复制
if(theorder == 'nooffers') {
    Request.comparator = Request.nooffers;
}

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

代码语言:javascript
复制
nooffers : function(ab) {
    return ab.get('offers');
}

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

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

代码语言:javascript
复制
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/

或者,您可以让集合交换自己的comparator,以避免comparator中的所有条件逻辑

代码语言:javascript
复制
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/

票数 51
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9431171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档