Passing parameters to the baseline event object of the baseline view

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

Can I somehow pass the parameter to the method declaration so that it is equivalent to the following (this does not 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 } 
+49
javascript
Oct 19 '11 at 15:13
source share
4 answers

Instead, you can put an additional argument in the data attribute on an element that you can click; something like that:

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

And then topProducts can understand for itself:

 topProducts: function(ev) { var pancakes = $(ev.currentTarget).data('pancakes'); // And continue on as though we were called as topProducts(pancakes) // ... } 
+74
Oct 19 '11 at 17:54
source share

I usually 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 } 
+33
Oct 21 '14 at 15:33
source share

What you can do is simply check the id of the element that was received as currentTarget in the arguments.

 topProduct: function (e) { var id = e.currentTarget.id; if (id == "top-all") // Do something else if (id == "top-5") // Do something else if (id == "top-3") // Do something } 
+8
Aug 21 2018-12-12T00:
source share

You can do this with closures:

 EventObject.on(event, (function(){ var arg = data; // Closure preserves this data return function(){ doThis(arg); } })()); 
+3
Sep 23 '13 at 1:58
source share



All Articles