本文介绍了将参数传递给主干视图的主干事件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下主干视图事件.它是一个产品视图 - 带有三个选项卡(全部"、前 3 名"、前 5 名")
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
}
推荐答案
您可以将额外的参数放在可点击项的数据属性中;像这样:
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">
然后 topProducts
可以自己解决:
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)
// ...
}
这篇关于将参数传递给主干视图的主干事件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!