0

According to backbone.js doc for View:

There are several special options that, if passed, will be attached directly to the view: model, collection, el, id, className, tagName and attributes.

I understand the el,id & className are used for wrapping whatever's in render(), but

How special are model and collection in a View object? Are they used by View methods at all?

Thank you.

Henry
  • 32,689
  • 19
  • 120
  • 221

1 Answers1

2

No, View methods don't use this options. model and collection will just become properties of the View object. Quotting from source:

// 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;
  },
  ...

};

theotheo
  • 2,664
  • 1
  • 23
  • 21
  • If that's the case, then they're really not that special other then they're the restrict list of properties allowed to be added to View? Hmm, that's sad. I thought there are more magic to them. – Henry May 30 '12 at 23:46
  • 1
    @Henry: Backbone itself doesn't do much with the view's `model` and `collection` but extensions (such as [Marionette](https://github.com/derickbailey/backbone.marionette)) can take advantage of the convention of having per-model and per-collection views with consistent interfaces. – mu is too short May 31 '12 at 01:28