正确地实现骨干比较 [英] correctly implement backbone comparators
问题描述
我变得有点坚持实施骨干比较,我基本上要选择基于路线不同的排序方式,并使用比较排序的集合。理想情况下,我想保持集合内封装的排序逻辑,但似乎被卡住。例如:
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/
但有来自外部的集合的方法摆弄这样的味道不好,你不应该这样做的人。相反,你应该问收集来改变其订购的东西是这样的:
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/
或者你可以让收集掉自己的比较
来避免比较
里面的所有条件逻辑:
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屋!