关于()Backbone.js的,什么是最后这个是指什么? [英] Backbone.js on(), what does the last this refer to?

查看:88
本文介绍了关于()Backbone.js的,什么是最后这个是指什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习骨干,我试图找出库中我得到了'上'的功能出来。我认为这是jQuery的,但即便如此我不理解的API。可能有人请解释一下开的功能或链接我的一些文档。第一个参数是该事件。第二个参数是被调用的函数。什么是最后的'这'是指(我假设调用类),以及为什么需要它?下面是从阿迪·奥斯马尼我的code直,这是APPVIEW:

 初始化:功能(){
        this.input =这个$('#新TODO')。
        this.allCheckbox =这个$('#切换,所有的')[0]。
        这$页脚=这个$('#页脚)。
        这主要$ =这个$('#主')。        window.app.Todos.on(添加,this.addOne,这一点);
        window.app.Todos.on('复位',this.addAll,这一点);
        window.app.Todos.on('的变化:完成,this.filterOne,这一点);
        window.app.Todos.on(过滤,this.filterAll,这一点);        window.app.Todos.on('所有',this.render,这一点);        app.Todos.fetch();
    },


解决方案

上在这种情况下,方法是从骨干网的事件模块到来。它接受三个参数 - 事件名称,功能和context.The背景下决定的本的价值应该是函数里面是什么。

Todos.on(过滤,this.filterAll,这一点); 你只是要求该函数内部filterAll的价值这个应该是你的看法实例

I am learning Backbone and I am trying to figure out what library I am getting the 'on' function out of. I thought it was jQuery, but if so I am not understanding the API. Could someone please explain the 'on' function or link me to some docs. The first parameter is the event. The second parameter is the function being called. What does the last 'this' refer to (I assume the calling class) and why is it needed? Here is my code straight from Addy Osmani, this is the AppView:

    initialize : function() {
        this.input = this.$('#new-todo');
        this.allCheckbox = this.$('#toggle-all')[0];
        this.$footer = this.$('#footer');
        this.$main = this.$('#main');

        window.app.Todos.on('add', this.addOne, this);
        window.app.Todos.on('reset', this.addAll, this);
        window.app.Todos.on('change:completed', this.filterOne, this);
        window.app.Todos.on("filter", this.filterAll, this);

        window.app.Todos.on('all', this.render, this);

        app.Todos.fetch();
    },

解决方案

The on method in this case is coming from Event module of Backbone. It accepts three parameters - the event name, function and the context.The context decides what the value of 'this' should be inside the function.

Todos.on("filter", this.filterAll, this); your just asking that inside the function filterAll the value of 'this' should be your views instance

这篇关于关于()Backbone.js的,什么是最后这个是指什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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