There are a few things you need to figure out.
First of all, if your insideMenuHandler
returns false
or calls e.stopPropogation()
, then your outsideMenuHandler
will not be called for clicks on #menu
. For instance:
http://jsfiddle.net/ambiguous/8GjdS/
But this is not your whole problem. Your outsideMenuHandler
will only be called for clicks on your view; therefore, if someone clicks on a page outside of your view, your outsideMenuHandler
will not be called and your menu will not work. If you want the menu to drop when someone clicks somewhere outside of #menu
, you will have to manually bind to the body
and manually unbind when destroying your view. Something like that:
var myView = Backbone.View.extend({ events: { 'click #menu': 'insideMenuHandler' }, initialize: function() { _.bindAll(this, 'insideMenuHandler', 'outsideMenuHandler'); }, render: function() { // Both <body> and <html> for paranoia. $('body, html').on('click', this.outsideMenuHandler); // ... return this; }, remove: function() { // Clean up after ourselves. $('body, html').off('click', this.outsideMenuHandler); // ... }, // ... outsideMenuHandler: function(e) { // ... return false; } });
and then be sure to delete your view. For instance:
http://jsfiddle.net/ambiguous/MgkWG/1/
source share