骨干JS .listenTo VS。对 [英] Backbone js .listenTo vs .on

查看:126
本文介绍了骨干JS .listenTo VS。对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是以下2行code的优点和缺点?我不明白为什么有两种不同的方式做同样的事情。

What are the advantages and disadvantages of the following 2 lines of code? I don't understand why there are 2 different ways to do the same thing.

this.listenTo(app.Todos, 'change:completed', this.filterOne);
app.Todos.on('change:completed', this.filterOne);

使用的时候。对

另外,我怎么确定是默认的情况下?

Also when using .on, how do I determine is the default context?

推荐答案

listenTo 是在更新更好的选择,因为这些听众将自动为您删除的stopListening 当一个视图被删除,这被称为(通过删除())。此前 listenTo 有带幻象意见挂撒手人寰(内存泄漏而造成的不当行为),因为视图方法是在模型作为事件监听器引用的真正阴险的问题,即使视图实例自己在DOM远去,不再赘述。

listenTo is the newer and better option because these listeners will be automatically removed for you during stopListening which is called when a view gets removed (via remove()). Prior to listenTo there was a really insidious problem with phantom views hanging around forever (leaking memory and causing misbehavior) because view methods were referenced as event listeners on models even though the view instances themselves were long gone and no longer in the DOM.

如果你想读 listenTo 后面的故事中,搜索 listenTo 骨干GitHub的仓库,通读一些较长的问题进行讨论的。

If you want to read the back story for listenTo, search the backbone github repository for listenTo and read through some of the longer issue discussions.

至于默认的情况下,有几种情况可以结束了绑定到这个

As to the default context, several things can end up bound to this:


  • 如果您通过做 this.listenTo 的结合,它将始终是视图实例(由维姆斜睨着在评论中指出)

  • 没有 this.listenTo ,故事变得复杂

    • 对于其它事件,这将是全局对象(最好避开这个)

    • 对于DOM事件,这将是源元素,就像在普通的DOM事件绑定

    • 如果您提供一个明确的上下文(第三参数 foo.on ),骨干网将使用(因此这是一个更强大的方法)

    • 如果您使用的ECMA标准函数(){//事件处理} .bind(本),也可以手动控制上下文(也建议)

    • 作为@mu指出, _。结合 $。代理是ECMA可用的替代 function.bind

    • 对于骨干网的意见,做 this.bindAll('的​​onClick',...)将确保视图实例是这个当任何视图方法作为事件处理程序的上下文

    • if you do the binding via this.listenTo, it will always be the view instance (pointed out by Wim Leers in the comments)
    • without this.listenTo, the story gets complicated
      • For misc events, it will be the global object (best to avoid this)
      • for DOM events, it will be the source element just like in regular DOM event binding
      • If you provide an explicit context (the 3rd argument to foo.on), backbone will use that (thus this is a more robust approach)
      • If you use the ECMA standard function () {//your event handler}.bind(this), you can also manually control the context (also recommended)
      • As @mu pointed out, _.bind or $.proxy are available alternatives to ECMA function.bind
      • For backbone views, doing this.bindAll('onClick', ...) will ensure the view instance is the this context when any view methods are used as event handlers

      所以总结到一些准则:


      • 使用事件只要有可能,因为它是简洁正确属性

      • 使用 this.listenTo 所有绑定模型和集合

      • 任何附加绑定记得上下文绑定可靠使用preferred方法。我通常使用ECMA Function.bind ,因为哎,标准,但也有几个很好的选择这里。

      • use the events property whenever possible as it is concise and correct
      • use this.listenTo for all bindings to models and collections
      • any additional bindings remember to bind the context reliably using your preferred method. I usually use ECMA Function.bind because hey, standards, but there are several good options here.

      这篇关于骨干JS .listenTo VS。对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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