Backbone.js View的特殊选项有什么特别之处?

时间:2012-05-30 22:36:26

标签: backbone.js backbone-views

根据View的{​​{1}}:

  

如果通过,将附加几个特殊选项   直接访问视图:modelcollectionelidclassNametagName   和attributes

我理解elid& className用于包装render()中的任何内容,但

<{1}}对象中特殊如何modelcollection?它们是否被View方法使用?

谢谢。

1 个答案:

答案 0 :(得分:2)

不,View方法不使用此选项。 modelcollection将成为View对象的属性。 从来源报价:

// List of view options to be merged as properties.
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];

// Set up all inheritable **Backbone.View** properties and methods.
_.extend(View.prototype, Events, {

  ...
  // Performs the initial configuration of a View with a set of options.
  // Keys with special meaning *(model, collection, id, className)*, are
  // attached directly to the view.
  _configure: function(options) {
    if (this.options) options = _.extend({}, this.options, options);
    for (var i = 0, l = viewOptions.length; i < l; i++) {
      var attr = viewOptions[i];
      if (options[attr]) this[attr] = options[attr];
    }
    this.options = options;
  },
  ...

};