This small backbone plugin allows you to pass eventData to your actions. This is reached while providing a convenience syntax extension to Backbone.View.prototype.events.
events: {
//in general
"namespace:eventName[eventData] sizzleSelector": "anAction",
//blanks are all right!
//e.g. for menues
"dblclick[new Window] a.menu": "navigateTo",
//e.g. for jquery.hotkeys by jeresig
"keydown[ctrl+a]": "selectAll",
//just one action for that much links
"click[1] .turn_1_page_right": "turnPage",
"click[5] .turn_5_pages_right": "turnPage",
"click[20] .turn_20_pages_right": "turnPage",
"click[-1] .turn_1_page_left": "turnPage",
"click[-5] .turn_5_pages_left": "turnPage",
"click[-20] .turn_20_pages_left": "turnPage",
}
- Include backbone.eventdata.js into your project after Backbone.
- evaluate event.data in your callback
- add eventdata in brackets to your eventbindings (directly after the events name)
This script overloads Backbone.View.prototype.delegateEvents and adds basically just a few statements to it.
view = new Backbone.View({
events: {
//on doubleclick call something like navigateTo({data:"newWindow", ...})
"dblclick[new Window] a.menu": "navigateTo",
},
navigateTo: function(event, ui){
if(event && event.data == "new Window")
//the magic happens!
this.openWindow();
else
this.doOtherMagic();
}
openWindow: function(){
//...
}
}
});
The latest patch will work with backbone 0.9.1. If you are still using backbone 0.5.3, take the patch from 4dfc1c88f1d.