Detect click on itemview container in Backbone/Marionette

两盒软妹~` 提交于 2019-12-07 05:18:55

问题


I have the following item view:

return Marionette.ItemView.extend({
        template:tpl,
        tagName: 'div',
        className: 'v_itv_record_type_item',
        events:{
            'click @ui.item':'itemClicked'
        },
        ui:{
            item:'.v_itv_record_type_item'
        },
        itemClicked:function(e){
            console.log('clicked');
        }
    });

that uses the following handlebars template:

<div class="clicktarget">
Stuff Goes Here
</div>

If you click on one of these item views, it does not register the click event. I understand that Backbone restricts access to just the views slice of the DOM, but apparently this does not extend to the containing div itself, even though that containing div is not part of any template, parent view or otherwise.

If we change the ui hash and point item at .clicktarget the click is registered. But this gives me me a <div><div>stuff goes here</div></div> structure for seemingly no reason. Is this the only way to detect a click on the entirety of an item views DOM element?


回答1:


You can register a click event on the view element by omitting the selector:

events:{
  'click' :'itemClicked'
}

Note that if you have an event handler at view level, all the clicks inside the view will bubble up and trigger it's handler, unless it was stopped (event.stopPropagation()) on the way. This is the expected behavior.



来源:https://stackoverflow.com/questions/34645874/detect-click-on-itemview-container-in-backbone-marionette

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!