バックボーンで自分の作業を抽象化しようとしている<table>
ので、「ベース ビュー」を拡張する「テーブル ビュー」を作成しました。したがって、新しいテーブルが必要な場合は、「テーブル ビュー」を拡張して、レンダリングしたい DOM 要素に「テーブル ビュー」を向けることができます。そのようです:
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 ビュー」からレンダリングする要素を「テーブル ビュー」に伝えるにはどうすればよいでしょうか。