2012-05-30 69 views
0

backbone.js docViewBackbone.js View的特殊選項有什麼特別之處?

有跡象表明,如果獲得通過,將附着 直接到視圖幾個特殊選項:modelcollectionelidclassNametagNameattributes

我明白elid & className用於包裝無論在render(),但

如何特殊處於View對象modelcollection?它們是否被View方法使用?

謝謝。

回答

2

不,查看方法不使用此選項。 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; 
    }, 
    ... 

};

+0

如果是這樣,那麼他們真的不是那麼特別,那麼他們是允許添加到視圖的屬性的限制列表?嗯,那很傷心。我認爲他們有更多的魔力。 – Henry

+1

@Henry:骨幹本身對視圖的「模型」和「集合」做的不多,但擴展(如[Marionette](https://github.com/derickbailey/backbone.marionette))可以利用具有一致接口的每個模型和每個集合視圖的約定。 –

相關問題