Detect click on itemview container in Backbone/Marionette
Asked Answered
S

1

6

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?

Somatology answered 7/1, 2016 at 1:25 Comment(0)
Y
6

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.

Yeanling answered 7/1, 2016 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.