需要幫助,無法理解如何將模型的每個視圖附加到DOM中的每個現有DIV(具有div.container和div.widget數組)。將模型的視圖附加到骨架中的現有DIV
// Model
V.Models.Shortcode = Backbone.Model.extend({});
// Shortcodes Collection Init
V.Collections.Shortcodes = Backbone.Collection.extend({
model: V.Models.Shortcode,
});
當的iframe裝載,推存儲從服務器到集合:
$('#preview').on('load', function() {
var ShortcodesCollection = new V.Collections.Shortcodes(Vision.ShortcodeStorage);
var Preview = new V.Views.Preview({
collection: ShortcodesCollection,
el: $('.container')
});
Preview.render();
});
渲染採集預覽:
// Collection View in iframe
V.Views.Preview = Backbone.View.extend({
initialize: function() {
this.collection.on('add', this.addOne, this);
},
render: function() {
this.collection.each(this.addOne, this);
return this;
},
addOne: function(ad) {
var shortcodeView = new V.Views.Shortcode({ model: ad });
shortcodeView.render();
}
});
查看每個型號:
// Shortcode View
V.Views.Shortcode = Backbone.View.extend({
events: {
'click .widget' : 'SomeActionOnView'
},
render: function(){
//console.log(this.el);
//console.log(this.model.toJSON());
},
SomeActionOnView: function(){
console.log(this);
}
});
問題是,如何將V.Views.Shortcode附加到每個具有「widget」類的div來綁定事件。謝謝!
您是否想在div下添加您的所有視圖?class「widget」? – Nitesh
@Nitesh希望將每個視圖添加到每個視圖.widget – 20yco
您是否需要爲每個視圖創建一個新的div.widget? – Nitesh