You will need to work around the presentation framework. when you do something like this:
events: 'click .back': 'showListing' 'keyup': 'goBack'
you bind your goBack
function to the goBack
event raised in the container element of your view. (default is the div in which the view is displayed)
instead, if you want to link something outside of your view (which doesn't have its own view! (*) )
Raffler.Views.EntryShow = Backbone.View.extend({ template: JST['entries/show'], events: { 'click .back': 'showListing' }, initialize: function () { $('body').keyup(this.goBack); }, showListing: function () { Backbone.history.navigate("/", trigger: true); }, goBack: function (e) { console.log e.type, e.keyCode; }, render: function () { $(this.el).html(this.template(entry: @model)); return this; } });
(*) note , as mentioned above, it is best to do this only if the element you want to anchor does not have its own representation, if you have a representation for the full page (application or something like that), you can link with it the keyboard and just raise the App.trigger('keypressed', e);
event App.trigger('keypressed', e);
, eg.
you can bind this App keypressed
event in your EntryShow keypressed
.
App.bind('keypressed', goBack);
Keep in mind that you have to do something as a pending event or group keystrokes in some situations, since turning on every keystroke that happens in the body can be a big hit on performance. especially in older browsers.
source share