I have a button on top of a view. Both have a click event. When i click on the button, the view also receive a click event. That s not normal! how can i prevent that behavior?
var view = Ti.UI.createView(); view.addEventListener('click', function(){Ti.API.info('click1');}); var button = Ti.UI.createButton({width:50, height:50}); button.addEventListener('click', function(){Ti.API.info('click2');}); view.add(button);if i click on button, i get click2 and click1
2 Answers
Accepted Answer
Try this:
var view = Ti.UI.createView({ _type: 'view' }); view.addEventListener('click', function(e) { if ( e.source._type != 'view' ) { return; } Ti.API.info('click1'); });
That's normal and it is called event bubbling. Currently, Titanium has no means to prevent event bubbling, but you can check if view is the source of the click:
view.addEventListener('click', function(e){ //this is view inside event listener if (this === e.source) { //view is the source } });
Your Answer
Think you can help? Login to answer this question!