范围在$阿贾克斯回调函数 [英] Scope in an $.ajax callback function

查看:279
本文介绍了范围在$阿贾克斯回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用这code为什么范围变更为doStuff警报?有没有一种方法,我可以保证的范围是我的对象,而不是Window对象?

下面是同一code在的jsfiddle

 (功能($){
    变种C $ = {};

    C $ .TestScope =功能(){

        this.doAjax =功能(的onComplete){
            无功自我=这一点;
            $阿贾克斯({
                网址:badurl,
                完成:功能(数据){
                    警报('doAjax2自=== C $ .oTestScope:'+(自=== C $ .oTestScope)的ToString());
                    的onComplete(数据);
                }
            });
            警报('doAjax1自=== C $ .oTestScope:'+(自=== C $ .oTestScope)的ToString());
        };

        this.doStuff =功能(数据){
            无功自我=这一点;
            警报('doStuff自=== C $ .oTestScope:'+(自=== C $ .oTestScope)的ToString());
        }

    };

    C $ .oTestScope =新C $ .TestScope();
    C $ .oTestScope.doAjax(C $ .oTestScope.doStuff);
})(jQuery的);
 

解决方案

您应该可以指定值的背景下,你的 $。阿贾克斯()参数:

 变种C $ = {};

C $ .TestScope =功能(){

    this.doAjax =功能(的onComplete){

        警报('doAjax1这=== C $ .oTestScope:'+(这=== C $ .oTestScope)的ToString());

        $阿贾克斯({
            网址:badurl,
            背景:这一点,
            完成:功能(数据){
                警报('doAjax2这=== C $ .oTestScope:'+(这=== C $ .oTestScope)的ToString());
                onComplete.call(此,数据);
            }
        });
    };

    this.doStuff =功能(数据){
        警报('doStuff这=== C $ .oTestScope:'+(这=== C $ .oTestScope)的ToString());
    }

};

C $ .oTestScope =新C $ .TestScope();
C $ .oTestScope.doAjax(C $ .oTestScope.doStuff);
 

修改我做了一个小提琴,这和验证,它的工作原理正常。有没有一个额外的参数周围搞乱或有淤泥周围的封锁,让您的变量。

什么,你失踪被调用部分 onComplete.call(这个数据)来调用 doStuff()

If I am using this code why has the scope changed for the "doStuff" alert? Is there a way that I can make sure that the scope is my object and not the Window object?

Here is the same code in jsfiddle.

(function ($) {
    var c$ = {};

    c$.TestScope = function () {

        this.doAjax = function (onComplete) {
            var self = this;
            $.ajax({
                url: 'badurl',
                complete: function (data) {
                    alert('doAjax2 self === c$.oTestScope: ' + (self === c$.oTestScope).toString());
                    onComplete(data);
                }
            });
            alert('doAjax1 self === c$.oTestScope: ' + (self === c$.oTestScope).toString());  
        };

        this.doStuff = function (data) {
            var self = this;
            alert('doStuff self === c$.oTestScope: ' + (self === c$.oTestScope).toString());
        }

    };

    c$.oTestScope = new c$.TestScope();
    c$.oTestScope.doAjax(c$.oTestScope.doStuff);
})(jQuery);​

解决方案

You should be able to specify the this value as the context in your $.ajax() parameters:

var c$ = {};

c$.TestScope = function() {

    this.doAjax = function(onComplete) {

        alert('doAjax1 this === c$.oTestScope: ' + (this === c$.oTestScope).toString());

        $.ajax({
            url: 'badurl',
            context: this,
            complete: function(data) {
                alert('doAjax2 this === c$.oTestScope: ' + (this === c$.oTestScope).toString());
                onComplete.call(this, data);
            }
        });
    };

    this.doStuff = function(data) {
        alert('doStuff this === c$.oTestScope: ' + (this === c$.oTestScope).toString());
    }

};

c$.oTestScope = new c$.TestScope();
c$.oTestScope.doAjax(c$.oTestScope.doStuff);

Edit I made a fiddle for this and verified that it works properly. There's no messing around with an extra self parameter or having to muck around with closures to keep your variables.

Part of what you were missing was calling onComplete.call(this, data) to invoke doStuff().

这篇关于范围在$阿贾克斯回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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