2013-01-08 100 views
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視圖'?

回答

0

除非我誤解你在這裏後在做什麼,你應該還只是能在el傳遞,當你構建你的孩子看,即使從表視圖繼承:

new app.Views.quotesList({ el: $('#foo') }) 

由於只要你將它傳遞到初始化,它會被你叫render()或其他任何使用它,即使render()由父(表)查看所定義的時間設置。

+0

謝謝,但我需要做的,是通過「厄爾尼諾」父視圖?或者我是否過度這個? – benhowdle89

+0

我認爲你正在超越它。 –

+0

如果它不適合你,可能是因爲父'initialize()'沒有被調用(因爲你已經覆蓋了它)。嘗試將'options' arg添加到'quotesList'' initialize()'函數並在其開頭調用'app.Views.table.prototype.initialize.call(options)'。 –