/backbone-eventdata

This small backbone plugin allows you to pass eventData to your actions

Primary LanguageJavaScript

Backbone Eventdata

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",

  }

how do I use it?

  1. Include backbone.eventdata.js into your project after Backbone.
  2. evaluate event.data in your callback
  3. add eventdata in brackets to your eventbindings (directly after the events name)

how does it work?

This script overloads Backbone.View.prototype.delegateEvents and adds basically just a few statements to it.

example

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(){
    //...
    }
  }
});

what backbone version do I need?

The latest patch will work with backbone 0.9.1. If you are still using backbone 0.5.3, take the patch from 4dfc1c88f1d.