モデルのビュー用に 2 つの異なるテンプレートがあります。モデルがデータベースからフェッチされるたびに、バックエンドからフェッチされた最初の 3 つのモデル (#1、2、3) には最初のテンプレートを使用して作成されたビューがあり、次の 4 つのモデル (#4、5、6、7) にはビューが作成されます。は 2 番目のテンプレートを使用し、次の 3 つのモデル (#8、9、10) は最初のテンプレートを使用するというように続きます。
問題: backbone.js を使用して、この代替テンプレートを導入するにはどうすればよいですか?
JS コード
// Views
PhotoListView = Backbone.View.extend({
el: '#photo_list',
render: function() {
$(this.el).html('');
_.each(this.model.models, function(photo) {
$(this.el).append(new PhotoListItemView({ model: photo }).render().el);
}, this);
return this;
}
});
PhotoListItemView = Backbone.View.extend({
tagNAme: 'div',
className: 'photo_box',
template: _.template( $('#tpl_PhotoListView').html() ),
initialize: function() {
this.model.bind('destroy', this.close, this);
},
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
return this;
},
close: function() {
this.unbind();
this.remove();
}
});