什么是"方式];在Backbone.js的? [英] What is "options" in Backbone.js?

查看:110
本文介绍了什么是"方式];在Backbone.js的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Backbone.js的选项我看遍了正式的来源$ C ​​$ C 并通过托马斯教程博客也用戴维斯的样本code位置:

What is "options" in Backbone.js that I see all over the official source code and also used in a tutorial blog by Thomas Davis with sample code here:

Friends = Backbone.Collection.extend({
     initialize: function (models, options) {
                   this.bind("add", options.view.addFriendLi); 
                 }
});

我不使用它看到任何其他的教程,甚至文档提到它。确实如此,但在上下文样的格式([选项]),而不是在硬codeD选项: options.view.addFriendLi

推荐答案

选项,传统上,是键/值对的JavaScript对象提供数据/环境/参数/配置一个方法调用。认为它像命名参数,而不是命令的参数。

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.

例如:

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.

骨干的文档Collection.initialize()似乎并没有列出的选项键对象使用或预期,这是不幸的。所以不看的来源,也没有办法告诉。但是你的例子似乎指示视图键的预期。所以,你可能会这样称呼它:

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:

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.

看远一点,现在在这里:<一href=\"http://documentcloud.github.com/backbone/docs/backbone.html#section-53\">http://documentcloud.github.com/backbone/docs/backbone.html#section-53

Looking a bit further, now here: http://documentcloud.github.com/backbone/docs/backbone.html#section-53

看起来 Col​​lection.initialize()只有在它选择接受单个键:比较。在这里,您可以定义用于集合中的模型进行排序的函数:
<一href=\"http://documentcloud.github.com/backbone/#Collection-comparator\">http://documentcloud.github.com/backbone/#Collection-comparator

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

该工作到你的例子,你会说这是这样的:

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');
  }
});

这篇关于什么是&QUOT;方式];在Backbone.js的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆