将参数传递到主干视图的主干事件对象中

发布于 2024-12-10 12:52:29 字数 341 浏览 0 评论 0原文

我有以下主干视图事件。它是一个产品视图 - 具有三个选项卡(“全部”、“前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自在安然 2024-12-17 12:52:29

您可以将额外的参数放在可点击项目的数据属性中;像这样:

<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)
    // ...
}

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)
    // ...
}
自由如风 2024-12-17 12:52:29

我通常更喜欢做这样的事情:

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
}

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
}
信愁 2024-12-17 12:52:29

你能做的就是检查参数中作为 currentTarget 接收的元素的 id。

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
}

What you can do, is just check the id of the element which is received as currentTarget in 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
}
时光暖心i 2024-12-17 12:52:29

您可以使用闭包来做到这一点:

EventObject.on(event, (function(){
    var arg = data; // Closure preserves this data
    return function(){
        doThis(arg);
    }   
})());

You can do so using closures:

EventObject.on(event, (function(){
    var arg = data; // Closure preserves this data
    return function(){
        doThis(arg);
    }   
})());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文