简体   繁体   English

骨干定制事件回调参数

[英]Backbone custom event callback arguments

I'm attempting to pass an argument called newIndex in a custom event from one view to another using the trigger() function. 我试图使用trigger()函数将自定义事件中名为newIndex的参数从一个视图传递到另一个视图。 Here's an excerpt from the first view: 这是第一个视图的节选:

var ProductListView = Backbone.View.extend({

    el: '#products',

    events:{
            sortupdate: function(event, ui){
                    ui.item.trigger('moved', ui.item.index());
            }
    }
});

The element on which the 'moved' event is being triggered is another view. 触发“移动”事件的元素是另一个视图。 Here's an excerpt from that view: 这是该观点的摘录:

var ProductItemView = Backbone.View.extend({

    events: {
            moved: 'viewMoved'
    },

    initialize: function(){
            _.bindAll(this, 'render', 'viewMoved');
    },

    viewMoved: function(newIndex){
            anotherFunc(this.model, newIndex);
    },

});

The issue I'm having is that in the anotherFunc() call, the newIndex argument appears to be the event itself rather than the value that I passed in when I triggered the event. 我遇到的问题是,在anotherFunc()调用中,newIndex参数似乎是事件本身,而不是触发事件时传递的值。 If I adjust the viewMoved callback to the following, it works correctly: 如果我将viewMoved回调调整为以下值,它将正常工作:

viewMoved: function(event, newIndex){
    anotherFunc(this.model, newIndex);
},

Is this intended functionality? 这是预期的功能吗? Is the event itself always the first argument passed to a custom event's callback, or is something else going on here? 事件本身是否始终是传递给自定义事件的回调的第一个参数,还是这里发生了其他事情? If it's intended is this documented somewhere? 是否打算在某处记录此内容? I was unable to find any reference to this in the Backbone documentation. 我在主干文档中找不到对此的任何引用。

Backbone uses jQuery heavily under the hood, and your ui.item is actually a jQuery object. Backbone在ui.item大量使用jQuery,而您的ui.item实际上是jQuery对象。 See the documentation for the trigger function . 请参阅有关触发功能的文档。

In summary, you're using jQuery's trigger because ui.item is not a Backbone View. 总之,由于ui.item不是主干视图,因此您正在使用jQuery的trigger Extra parameters are always passed after the event object. 总是在event对象之后传递额外的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM