如何从Backbone.listenTo查找触发事件? [英] How To Find Triggered Event From Backbone.listenTo?

查看:68
本文介绍了如何从Backbone.listenTo查找触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Backbone中,我正在使用新的 Backbone.listenTo 事件.我的一个实例将侦听器附加到三个不同的事件,例如:

In Backbone, I'm using the new Backbone.listenTo event. One of my instances has the listener attached to three different events e.g.:

this.listenTo(this._Collection, 'reset add change', this._setCollection);

应在适当的时候调用它,并且那里没有问题.我不知道如何找到触发了哪个事件.我可以使用以下命令访问 e 参数:

It is called appropriately when it's supposed to and there are no issues there. What I don't know is how to find out which event was triggered. I have access to the e argument using:

_setCollection: function(e) {
    // Do fun stuff
}

问题在于, e 参数仅发送集合的副本,而没有提及实际触发了什么事件.我试过 e.type e.target ,但是那些对象不存在.这是Chrome开发工具中 e 对象的副本:

The problem is that the e argument only sends a copy of the collection and doesn't mention what event is actually triggered. I've tried e.type and e.target but those objects don't exist. Here's a copy of the e object from Chrome Dev tools:

_byCid: Object
_byId: Object
_events: Object
    add: Array[1]
    change: Array[1]
    remove: Array[1]
    reset: Array[1]
__proto__: Object
_listenerId: "l16"
length: 3
models: Array[3]

我如何找到触发了什么事件?

How can I find what event was triggered?

答案澄清:尽管从技术上来说标记的答案是正确的,但正如mu_is_too_short指出的那样,正确的答案是使用多个处理程序,而不是执行这种类型的骗子"

Answer Clarification: Although the marked answer is technically correct, as pointed out by mu_is_too_short the correct answer is using multiple handlers and not performing this type of "chicanery"

推荐答案

您无法直接检测事件类型,但在某些情况下,可以从事件目录的意思是:

You can't detect the event type directly but, in some cases, you can infer it from arguments. The events catalog has this to say:

  • 添加" (模型,集合,选项)-将模型添加到集合中.
  • 重置" (集合,选项)-替换集合的全部内容后.
  • 更改" (模型,选项)-模型的属性已更改.
  • "add" (model, collection, options) — when a model is added to a collection.
  • "reset" (collection, options) — when the collection's entire contents have been replaced.
  • "change" (model, options) — when a model's attributes have changed.

幸运的是,所有这三个事件都有不同的参数,因此 arguments 的内容将唯一地(在这种情况下)确定触发事件:

Lucky for you, all three of those events have different parameters so the contents of arguments will uniquely (in this case) determine the triggering event:

  1. 如果 arguments [0] 是一个模型,而 arguments [1] 是一个集合,则您有一个"add" 事件./li>
  2. 如果 arguments [0] 是一个集合,则您有一个"reset" 事件.
  3. 如果 arguments [0] 是模型,而 arguments.length 是2,则您有一个"change" 事件.
  1. If arguments[0] is a model and arguments[1] is a collection then you have an "add" event.
  2. If arguments[0] is a collection then you have a "reset" event.
  3. If arguments[0] is a model and arguments.length is 2 then you have a "change" event.

因此,您可以在 _setCollection 中进行类似这样的令人不快和脆弱的操作:

So you can do something unpleasant and fragile like this in your _setCollection:

    // M is your model, C is your collection.
    if(arguments[0] instanceof M
    && arguments[1] instanceof C) {
        // An "add" event...
    }
    else if(arguments[0] instanceof C) {
        // A "reset" event...
    }
    else if(arguments[0] instanceof M
         && arguments.length == 2) {
        // A "change" event...
    }
    else {
        console.log('confusion!');
    }

演示: http://jsfiddle.net/ambiguous/Y9UUX/

我不建议您使用这种装饰,因为它笨拙,脆弱,如果您将更多事件类型添加到列表中,则可能会损坏.如果您的事件处理程序需要知道触发了哪种事件,那么最好为每种事件类型使用单独的处理程序:三个函数和三个 listenTo 调用比一小堆丑陋的黑客更好

I wouldn't recommend this sort of chicanery though, it is kludgey, fragile, and can break if you add more event types to the list. If your event handler needs to know what sort of event triggered it then you'd be better off using separate handlers for each event type: three functions and three listenTo calls are better than a small pile of ugly hackery.

这篇关于如何从Backbone.listenTo查找触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆