GalleryItem サブビューで構成されるバックボーンにギャラリーがあります。
私が今抱えている問題は、サブビューでイベントが正しく発生していないことです。
これが私のコードですGalleryItemView
:
// Gallery View: Displays a Single Image Element
var GalleryItemView = Backbone.View.extend({
el: '#isotope',
events: {
"click .hoverbox": "onHoverBoxClick"
},
initialize: function () {
this.template = Hogan.compile($("#gallery-item-template").html());
},
render: function () {
this.$el.append(this.template.render(this.model.toJSON()));
return this;
},
onHoverBoxClick: function () {
console.log("clicked: ", this.model.id);
}
});
divonHoverBoxClick
をクリックすると、各要素がイベントを発生させると思っていました。.hoverbox
しかし、それは何もしません。これをビューに追加すると:
el: "#mydiv"
次に、イベントを発生させますが、GalleryItemView ごとに発生します。(したがって、8 つのサブビューの場合、8 つの console.logs も取得し、1 つだけをクリックしました)
サブビューの私のテンプレートは次のようになります。
<script type="text/x-handlebars-template" id="gallery-item-template">
<div class="item {{tags}}" style="width: auto;">
<img class="eveimage" src="{{image_medium_url}}">
<div class="hoverbox down"><span>{{tags}}</span> </div>
</div>
</script>
ギャラリーのコレクション ビューは次のようになります (ここでギャラリー アイテムをインスタンス化します)。
// Gallery View: Displays the main image / project gallery
window.views.GalleryView = Backbone.View.extend({
el: '#isotope_container',
className: 'no-transition',
template: _.template($("#gallery-template").html()),
initialize: function() {
this.collection = new gallery();
this.$el.append(this.template);
this.collection.on('reset', this.render, this);
},
render: function(){
console.log("ich render nei");
this.collection.forEach(this.addOne, this);
},
addOne: function(model){
var itemView = new GalleryItemView({model: model});
var el = itemView.render().el;
this.$el.append(el);
}
});
それはなぜですか?? ありがとう...