Backbone.js - 这个参数的值来自哪里?
我不确定如何以一种易于理解的方式表达我的问题,所以我创建了下面的图表。该示例特定于 JavaScript 和 Backbone.js,但我假设这也可以被视为只是一个一般的编程问题。
我有一个 Backbone 集合的示例,但我无法理解这些值是如何传递的。我假设“任务”可以很容易地是任何任意值,例如“炸玉米饼”或“马”,并且效果也一样。我只是想知道“任务”在哪里以及如何将其值映射到它。
这是我看待问题的方式,我尝试在下面重新创建我的困惑路径:
如果有帮助的话,这是任务模型:
var Task = Backbone.Model.extend({
isComplete: function() {
return this.get('completed_at') !== null;
}
});
I wasn't sure how to express my question in a way that would make it understandable, so I created a diagram below. The example is specific to JavaScript and Backbone.js, but I'm assuming this may also be regarded as just a general programming question as well.
I have an example of a Backbone collection and I'm having trouble understanding how the values are being passed around. I'm assuming that 'task' could just as easily be any arbitrary value like 'taco' or 'horse' and work just as well. I'm just wondering where and how 'task' is getting its value mapped to it.
Here is how I am looking at the problem and I've tried to recreate the path of my confusion below:
Here is the Task model if that helps:
var Task = Backbone.Model.extend({
isComplete: function() {
return this.get('completed_at') !== null;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
比较器函数由下划线的
_.sortBy
调用,并以数组元素作为参数,进行大量的对象创建和迭代,本质上可以归结为更快的本机排序:或者一个更简单的示例:
这里的比较器函数在尝试解析排序顺序时从排序函数获取其参数,从比较器返回的值确定数组的排序顺序。
我建议在主干排序上进行正常排序,因为在 chrome 中它对我来说快 10 倍并且更容易理解,因为比较器函数实际上......比较: http://jsperf.com/underscore-sort-vs-normal-sort
The comparator function is called by underscore's
_.sortBy
with array element as an argument, there is a ton of object creating and iteration going on for what essentially comes down to much faster native sort:Or a simpler example:
The comparator function here gets its arguments from the sort function when it tries to resolve the sort order, the value that you return from the comparator determines in what order the array is sorted.
I'd suggest normal sort over the backbone sort, as it's 10x faster for me in chrome and easier to understand because the comparator function actually... compares: http://jsperf.com/underscore-sort-vs-normal-sort
集合的比较器函数将模型作为输入。我认为你必须编写如下比较器:
注意:Backbone 比较器与普通的 JS 数组排序比较器不同!
Comparator function of the Collection takes a Model as input. I think you have to write comparators like:
Note: Backbone comparators are different from normal JS array sort comparator!