Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect click on itemview container in Backbone/Marionette

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?

like image 907
Dakine83 Avatar asked Jan 07 '16 01:01

Dakine83


1 Answers

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.

like image 149
T J Avatar answered Oct 24 '22 02:10

T J