The 2nd answer to this question nicely explains how event declarations in Backbone.js views are scoped to the view's el
element.
It seems like a reasonable use case to want to bind an event to an element outside the scope of el
, e.g. a button on a different part of the page.
What is the best way of achieving this?
Update: This answer is no longer valid/right. Please see other answers below!
Why do you want to do this?
Apart from that, you could always just bind it using regular jQuery handlers. E.g.
IIRC, Backbone.js just uses the regular jQuery handlers, so you're essentially doing the same thing, but breaking the scope :)
there is not really a reason you would want to bind to an element outside the view, there are other methods for that.
that element is most likely in it's own view, (if not, think about giving it a view!) since it is in it's own view, why don't you just do the binding there, and in the callback Function, use
.trigger();
to trigger an event.subscribe to that event in your current view, and fire the right code when the event is triggered.
take a look at this example in JSFiddle, http://jsfiddle.net/xsvUJ/2/
this is the code used: