V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Backbone supplies structure to JavaScript-heavy applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing application over a RESTful JSON interface.
ljbha007
V2EX  ›  Backbone.js

backbone.js的例子问题

  •  
  •   ljbha007 · 2012-12-28 16:40:23 +08:00 · 6518 次点击
    这是一个创建于 4108 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近在学习backbone.js
    看到第一个例子就有看不懂的地方
    http://arturadib.com/hello-backbonejs/docs/1.html

    (function($){
    var ListView = Backbone.View.extend({
    ...
    initialize: function(){
    _.bindAll(this, 'render'); // fixes loss of context for 'this' within methods

    this.render(); // not all views are self-rendering. This one is.
    },
    ...
    });
    ...
    })(jQuery);

    请问:
    Q1: 为什么用 (function($){})(jQuery);而不用 (function(){})();?

    Q2: _.bindAll(this, 'render') 是干啥的? 为什么可以“fixes loss of context for 'this' within methods”?

    麻烦懂的解答一下 感激不尽
    10 条回复    1970-01-01 08:00:00 +08:00
    zythum
        1
    zythum  
       2012-12-28 16:55:49 +08:00   ❤️ 1
    Q1.
    比如
    (function($){
    setTimeout(function(){
    alert($);
    },200)
    })(JQuery);
    JQuery = null;


    (function(){
    setTimeout(function(){
    alert(jQuery);
    },200)
    })();
    jQuery = null;

    这样就应该能理解了。

    2. 就是把这些自定义事件绑到this上
    ljbha007
        2
    ljbha007  
    OP
       2012-12-28 17:00:35 +08:00
    @zythum
    谢谢
    第一个我明白了
    第二个“把这些自定义事件绑到this上”是什么意思?这样的绑定又是怎么防止"loss of context for 'this' within methods"的呢?
    zythum
        3
    zythum  
       2012-12-28 17:03:17 +08:00
    Q2. 其实解释改下this的上下文。
    比如
    var a = {
    click: function(){alert(this)}
    }
    a.click(); => alert window

    _.bindAll(a,'click');
    DOM.bind('click', a.click)
    点击 => alert DOM
    zythum
        4
    zythum  
       2012-12-28 17:05:22 +08:00
    @ljbha007 我 backbone不怎么用。 应该是这样。如果不对请指正
    FuryBean
        5
    FuryBean  
       2012-12-28 17:07:33 +08:00
    Q1: 提供使用其他选择器的可能,比如zepto.js

    Q2:_.bindAll的文档看这里:http://underscorejs.org/#bindAll
    如果了解bind,看_.bind的说明:
    Bind a function to an object, meaning that whenever the function is called, the value of this will be the object. Optionally, bind arguments to the function to pre-fill them, also known as partial application.
    ljbha007
        6
    ljbha007  
    OP
       2012-12-28 17:10:59 +08:00
    @zythum
    第一段代码运行结果不是window是a本身[Object object]
    第二个运行不了
    zythum
        7
    zythum  
       2012-12-28 17:35:16 +08:00
    @ljbha007
    恩。第一个应该是a 我错了。
    DOM是一个dom么。比如 $(document.body)之类的。
    ljbha007
        8
    ljbha007  
    OP
       2012-12-28 17:56:44 +08:00
    @zythum
    不过我最后搞懂那个_.bindAll的意思和原理了 貌似是利用.apply或者.call方法来注入一个this对象
    zythum
        9
    zythum  
       2012-12-28 18:37:13 +08:00
    @ljbha007 恩。就是改变了this的上下文环境
    jinwyp
        10
    jinwyp  
       2012-12-28 22:50:48 +08:00
    _.bindall 现在不用了都用 model.on 了 网站的教程比较老 看这个吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1343 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:37 · PVG 07:37 · LAX 16:37 · JFK 19:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.