次のコードがあります:
var Contents = Backbone.View.extend({
events: {
"click #addAll": "addContent"
},
initialize: function() {
_.bindAll(this);
this.model = new ContentCollection();
this.model.on("add", this.contentAdded);
},
addContent: function(event) {
this.model.add({ modelName: "all"});
//Plugin event
$('select').switchify().data('switch').bind('switch:slide', function(e,type){
//Do something
});
},
contentAdded: function(content) {
if (content.view == null) {
var template_name;
switch(content.get("modelName")){
case 'all': template_name = 'home'; break;
}
content.view = new ContentView({
model: content, template: //template
});
$(".box").empty();
content.functionModel();
//render template
}
},
});
ボタン(#addAll
)をクリックすると、モデルがコレクションに追加され、ビューが作成され、functionModel
呼び出され、テンプレートがレンダリングされます。funtionModel()
プラグインイベントが完了したとき に実行したいのですcontent
が、関数を呼び出すために作成された特定のモデルが必要です。また、プラグインはでは機能しませんcontentAdded
。