0
所以我想在骨幹抽象我<table>
工作的「厄爾尼諾」,所以我創建了一個「表視圖」延伸我的「基本視圖」。所以當我需要一個新表時,我可以只擴展'表視圖',並希望將'表視圖'指向我想要呈現的DOM元素,即。像這樣:BackboneJS - 設置父/繼承視圖
app.Views.quotesList = app.Views.table.extend({
template: '#tmpl-table',
initialize: function(){
this.collection = new Backbone.Collection([
{name: "Tim", age: 5},
{name: "Ben", age: 26},
{name: "Rob", age: 55}
]);
},
render: function() {
var that = this;
var template = Handlebars.compile($(this.template).html());
$(this.el).html(template({id: 'quotes'}));
this.table = this.$('#table-quotes');
this.thead = this.table.find('thead tr');
this.tbody = this.table.find('tbody');
var template = Handlebars.compile($('#table-heading').html());
that.thead.append(template(that.collection.first().toJSON()));
var template = Handlebars.compile($('#table-row').html());
that.collection.each(function(model){
that.tbody.append(template(model.toJSON()));
});
return this;
},
});
我想這個最普通的代碼移動到表格視圖:
app.Views.table = app.Views.base.extend({
initialize: function(){
}
});
那麼我將如何去告訴「表視圖」渲染成什麼元素從'quotesList視圖'?
謝謝,但我需要做的,是通過「厄爾尼諾」父視圖?或者我是否過度這個? – benhowdle89
我認爲你正在超越它。 –
如果它不適合你,可能是因爲父'initialize()'沒有被調用(因爲你已經覆蓋了它)。嘗試將'options' arg添加到'quotesList'' initialize()'函數並在其開頭調用'app.Views.table.prototype.initialize.call(options)'。 –