Is it possible to bind event handlers (for example, on mouseup) to the document from within a view?
I want to do something like this:
var someView = Backbone.View.extend({
el: "#someDIV",
initialize: function (options) { /* ... */ },
events: {
"mousedown" : "mousedownHandler", // on #someDIV
"mouseup" : "mouseupHandler" // this should be on document (global mouseup)
},
mousedownHandler: function (e) { /* ... */ },
mouseupHandler: function (e) { /* ... */ }
});
Is this possible, or do I have to resort to separate jQuery event binding on initialize? Or if there is a better way to do this, that would work too!
Thanks!
If the listener you want to add belongs, logically, to this view, than you have to do a normal jQuery binding.
Your other option would be to have a separate view, with el: ‘body’, on which to define a mouseup handler, and that would work with normal events declaration. But in your case I think it’s best to keep the mouseup and mousedown handlers in the same view, as they are closely related.