什么是“选项"?在 Backbone.js 中? [英] What is "options" in Backbone.js?

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

问题描述

我在官方 源代码,也用于 Thomas Davis 示例代码如下:

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

我没有看到任何其他教程使用它,甚至文档中也没有提到它.它确实如此,但在一种上下文格式([options])中,而不是在硬编码的选项"中: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,通常是一个键/值对的 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() 的主干文档似乎没有列出选项对象上的哪些键被使用或预期,这是不幸的.所以不看源码,就无从判断了.但是您的示例似乎确实表明需要 view 键.所以你可以这样称呼它:

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.

看得更远,现在在这里: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

看起来 Collection.initialize() 在它的选项中只接受一个键: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');
  }
});

这篇关于什么是“选项"?在 Backbone.js 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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