Backbone.js - 这个参数的值来自哪里?

发布于 2024-12-20 09:10:36 字数 497 浏览 0 评论 0原文

我不确定如何以一种易于理解的方式表达我的问题,所以我创建了下面的图表。该示例特定于 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:

enter image description here

Here is the Task model if that helps:

var Task = Backbone.Model.extend({
  isComplete: function() {
    return this.get('completed_at') !== null;
  }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-12-27 09:10:36

比较器函数由下划线的 _.sortBy 调用,并以数组元素作为参数,进行大量的对象创建和迭代,本质上可以归结为更快的本机排序:

someArrayOfTasks.sort( function( taskA, taskB ) {
return taskA.dueDate < taskB.dueDate ? -1 :
       taskA.dueDate > taskB.dueDate ? 1 : 0;
});

或者一个更简单的示例:

[3,2,5,1,4].sort( function(a,b){
return a-b;
});
//[1, 2, 3, 4, 5]

[3,2,5,1,4].sort( function(a,b){
return b-a;
});
//[5, 4, 3, 2, 1]

这里的比较器函数在尝试解析排序顺序时从排序函数获取其参数,从比较器返回的值确定数组的排序顺序。

我建议在主干排序上进行正常排序,因为在 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:

someArrayOfTasks.sort( function( taskA, taskB ) {
return taskA.dueDate < taskB.dueDate ? -1 :
       taskA.dueDate > taskB.dueDate ? 1 : 0;
});

Or a simpler example:

[3,2,5,1,4].sort( function(a,b){
return a-b;
});
//[1, 2, 3, 4, 5]

[3,2,5,1,4].sort( function(a,b){
return b-a;
});
//[5, 4, 3, 2, 1]

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

眸中客 2024-12-27 09:10:36

集合的比较器函数将模型作为输入。我认为你必须编写如下比较器:

function (task){
  return task.get('dueDate');
}

注意:Backbone 比较器与普通的 JS 数组排序比较器不同!

Comparator function of the Collection takes a Model as input. I think you have to write comparators like:

function (task){
  return task.get('dueDate');
}

Note: Backbone comparators are different from normal JS array sort comparator!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文