将参数传递到主干视图的主干事件对象中
我有以下主干视图事件。它是一个产品视图 - 具有三个选项卡(“全部”、“前 3 个”、“前 5 个”)
我能否以某种方式将参数传递到方法声明中,以便它相当于以下内容(这不起作用)?
events : {
"click #top-all": "topProducts(1)"
"click #top-three": "topProducts(2)"
"click #top-ten": "topProducts(3)"
},
topProducts(obj){
// Do stuff based on obj value
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将额外的参数放在可点击项目的数据属性中;像这样:
然后
topProducts
可以自己计算出来:You could put the extra argument in a data attribute on the clickable item instead; something like this:
And then
topProducts
can figure it out itself:我通常更喜欢做这样的事情:
I generally prefer to do something like this:
你能做的就是检查参数中作为 currentTarget 接收的元素的 id。
What you can do, is just check the id of the element which is received as currentTarget in arguments.
您可以使用闭包来做到这一点:
You can do so using closures: