我想用_.without
方法过滤Backbone collection
。为什么'不'方法根据上下文返回不同的结果?
它返回正确的结果(仅完成托多斯)以这种形式调用:
return this.without.apply(this, this.active());
但不是在这一个:
return _.without(this.models, this.active());
在后者这一说法它返回一个包含ALL车型阵列从收集。
我不能直接使用Underscore
方法,只能通过Backbone的this
上下文吗?
如何使第二条语句正常工作?
todos.js
var app = app || {};
var Todos = Backbone.Collection.extend({
model: app.Todo,
active: function() {
return this.filter(function(todo) {
return todo.get('completed') === false;
});
},
completed: function() {
return this.without.apply(this, this.active());
// return _.without(this.models, this.active()); <--- Problem is here
}
});
app.Todos = new Todos();
以后添加:
由于_ .without
方法不接受阵列作为第二个参数_.difference
更适合我的任务。
return _.difference(this.models, this.active());
但是如果_a没有在第一种形式的调用中工作,如果它期望一个标量值? this.without。apply(this,this.active()) – yaru 2014-10-05 13:17:58
>这是一个不好的习惯为什么这样?这是一个代码重用。 DRY原则。 – yaru 2014-10-05 13:19:03
_为什么这样?这是一个代码重用_不适合你的情况 – hindmost 2014-10-05 13:19:50