对于 Backbone View ,我有以下事件。其产品 View -具有三个标签(“全部”,“前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
}

最佳答案

您可以将多余的参数放在可点击项目的data属性中;像这样的东西:

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

然后topProducts可以自行解决:
topProducts: function(ev) {
    var pancakes = $(ev.currentTarget).data('pancakes');
    // And continue on as though we were called as topProducts(pancakes)
    // ...
}

10-06 13:09