javascript Backbone.js 中的“选项”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8997714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is "options" in Backbone.js?
提问by Jan Carlo Viray
What is "options" in Backbone.js that I see all over the official source codeand also used in a tutorial blog by Thomas Daviswith sample code here:
我在官方源代码中看到的 Backbone.js 中的“选项”是什么,并且还在Thomas Davis的教程博客中使用了示例代码:
Friends = Backbone.Collection.extend({
initialize: function (models, options) {
this.bind("add", options.view.addFriendLi);
}
});
I don't see any other tutorials using it, and even the doc mentioning it. It does, but in a context kind of format ([options]), not in a hard-coded "options": options.view.addFriendLi
我没有看到任何其他教程使用它,甚至文档也没有提到它。它确实如此,但在一种上下文格式([options])中,而不是在硬编码的“选项”中:options.view.addFriendLi
回答by Alex Wayne
options
, conventionally, is a javascript object of key/value pairs that provide data/context/arguments/configuration to a method call. Think of it like named arguments, as opposed to ordered arguments.
options
,通常是一个键/值对的 javascript 对象,它为方法调用提供数据/上下文/参数/配置。把它想象成命名参数,而不是有序参数。
For example:
例如:
var makePerson = function(name, options) {
var person = {};
person.name = name;
person.age = options.age;
return person;
};
var me = makePerson('Alex', {age:30}); // In 3 days... scary :(
How the function being called uses this object, is up to that function.
被调用的函数如何使用这个对象,取决于那个函数。
The documentation for backbone for Collection.initialize()
does not seem to list what keys on the options object are used or expected, which is unfortunate. So without looking at the source, there is no way to tell. But your example does seem to indicate a view
key is expected. So you might call it like this:
主干的文档Collection.initialize()
似乎没有列出选项对象上使用或预期的键,这是不幸的。所以不看源码,就无从判断了。但是您的示例似乎确实表明需要一个view
键。所以你可以这样称呼它:
var friendsCollection = new Friends(userModels, {view: someBackboneView});
This is simply a convention many libraries tend to use, and there is nothing special about it. But usually many keys in an object that is passed to a function call is better than a funciton that you call with many arguments, because each value has a label which makes it clear what each value is for.
这只是许多库倾向于使用的约定,并没有什么特别之处。但是通常传递给函数调用的对象中的多个键比使用多个参数调用的函数要好,因为每个值都有一个标签,可以清楚地说明每个值的用途。
Looking a bit further, now here: http://documentcloud.github.com/backbone/docs/backbone.html#section-53
进一步看,现在在这里:http: //documentcloud.github.com/backbone/docs/backbone.html#section-53
It looks like Collection.initialize()
only accepts a single key in it's options: comparator
. Here you can define a function used to sort the models in the collection:
http://documentcloud.github.com/backbone/#Collection-comparator
看起来Collection.initialize()
它的选项中只接受一个键:comparator
。在这里你可以定义一个用于对集合中的模型进行排序的函数:http:
//documentcloud.github.com/backbone/#Collection-comparator
Working that into your example, you would call that like this:
将其应用到您的示例中,您可以这样称呼它:
var friendsCollection = new Friends(userModels, {
view: someBackboneView,
comparator: function(friend) {
return friend.get('age');
}
});
回答by railsbug
Well, you can see the tutorial blog by Thomas Davis with sample code, the Backbone.View.extend
will answer your question:
好吧,您可以查看 Thomas Davis 的教程博客和示例代码,Backbone.View.extend
它将回答您的问题:
....
AppView=Backbone.View.extend({
el:$("body"),
initialize:function(){
this.friends=new Friends(null, {view:this});
//Create a friends collection when the view is initialized,
//Pass it a reference to this view to create a connection between the two
}
....
The key is this.friends=new Friends(null, {view:this});
关键是 this.friends=new Friends(null, {view:this});
From the above code initialize:function(models, options)
从上面的代码 initialize:function(models, options)
So you can know,the "options" == "{view:this}"
所以你可以知道,“选项”==“{view:this}”
It creates a new friends and passes in a parameter ({view:this}), then it passes itself to the above function.
它创建一个新朋友并传入一个参数({view:this}),然后将自身传递给上述函数。
Combine the code options.view.addFriendLi
,we can know why it can call the method .addFriendLi
.
结合代码options.view.addFriendLi
,我们可以知道为什么它可以调用该方法.addFriendLi
。