Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters into the Backbone events object of a backbone view

I have the following events for a Backbone View. Its a product view - with three tabs ("All", "Top 3", "Top 5")

Can I somehow pass a parameter into the method declaration so that it is equivalent to the following (this doesn't work)?

events : {     "click #top-all":          "topProducts(1)"     "click #top-three":      "topProducts(2)"     "click #top-ten":         "topProducts(3)" }, topProducts(obj){     // Do stuff based on obj value } 
like image 614
paddle42380 Avatar asked Oct 19 '11 15:10

paddle42380


2 Answers

You could put the extra argument in a data attribute on the clickable item instead; something like this:

<a id="top-all" data-pancakes="1"> 

And then topProducts can figure it out itself:

topProducts: function(ev) {     var pancakes = $(ev.currentTarget).data('pancakes');     // And continue on as though we were called as topProducts(pancakes)     // ... } 
like image 76
mu is too short Avatar answered Sep 22 '22 21:09

mu is too short


I generally prefer to do something like this:

events : {    "click #top-all":    function(){this.topProducts(1);}    "click #top-three":  function(){this.topProducts(2);}    "click #top-ten":    function(){this.topProducts(3);} }, topProducts(obj){    // Do stuff based on obj value } 
like image 39
Dre Avatar answered Sep 22 '22 21:09

Dre