简体   繁体   English

如何从jQuery.ajax成功声明中推送父对象

[英]How to push parent object from jQuery.ajax success statement

I have one function which contains child functions and objects: 我有一个包含子功能和对象的功能:

//API load function
var apiDataLoader = function ()     {

    // Set default settings
    this.apiSeason = '7924';
    this.apiMatchDay = '30';
    this.apiType = 'gamelist';
    this.apiLink = 'http://example.com/';
    this.apiLinkData = {
        app: this.apiType,
        season_num: this.apiSeason,
        match_day: this.apiMatchDay
    }
    this.apiData = new Object;


    //load API
    apiDataLoader.prototype.load = function()       {
        $.ajax({
            url: this.apiLink,
            data: this.apiLinkData,
            success: function(data) {
                apiDataLoader.apiData = data; //this is not working
                // I need to push somehow responce data to 'apiDataLoader.apiData' object
            }
        });
    }
}

As you can see in my code, I'm trying to push $.ajax response to parent function object element named: apidataLoader.apiData , but i can't achieve this. 如您在我的代码中看到的,我试图将$.ajax响应推送到名为apidataLoader.apiData父函数对象元素,但是我无法实现。

Any ideas? 有任何想法吗?

PS: response in data value is JSON object. PS: data值中的响应是JSON对象。

Try this: 尝试这个:

apiDataLoader.prototype.load = function()       {
        var self = this; //Store the current object to avoid `this` problem in javascript.
        $.ajax({
            url: this.apiLink,
            data: this.apiLinkData,
            success: function(data) {
                self.apiData = data; //assign value
            }
        });
    }

Since load() is defined on the prototype of apiDataLoader , it will receive an instance of apiDataLoader as a context when it is called, in others words, the this keyword inside load() will point to the instance of apiDataLoader on which it is called, so you need to change the code to this one: 由于load()是在apiDataLoader的原型上定义的,因此在apiDataLoader时它将接收apiDataLoader的实例作为上下文,换句话说, load()内部的this关键字将指向在其上调用apiDataLoader的实例,因此您需要将代码更改为以下代码:

apiDataLoader.prototype.load = function()       {

    var that = this; // this is the instance of apiDataLoader on which this method is called
    $.ajax({
        url: this.apiLink,
        data: this.apiLinkData,
        success: function(data) {
            that.apiData = data; //assign value
        }
    });
}

You need to reference the apiData property of the current object using this , however you'll need to cache it outside of the $.ajax success handler as the scope of this in that function will be different. 您需要引用apiData使用当前对象的属性this ,但是你需要缓存它的外面$.ajax成功处理程序的范围this在功能会有所不同。 Try this: 尝试这个:

apiDataLoader.prototype.load = function() {
    var _this = this;
    $.ajax({
        url: this.apiLink,
        data: this.apiLinkData,
        success: function(data) {
            _this.apiData = data;
        }
    });
}

As it is right now you are creating a new property on the constructor apiDataLoader , not the current instance. 现在,您正在构造函数apiDataLoader上创建一个新属性,而不是当前实例。 Here's one way to do it: 这是一种实现方法:

$.ajax({
    url: this.apiLink,
    data: this.apiLinkData,
    dataType: 'json', //will automatically parse the response as JSON
    success: function(data) {
        this.apiData = data;
    },
    context: this //sets the context object to the current instance in the callback
});

Also, please note that your prototype functions should be declared outside the constructor, otherwise there's no advantage in using the prototype since the functions are redeclared for each newly created instances. 另外,请注意,原型函数应在构造函数外部声明,否则使用原型没有优势,因为为每个新创建的实例都重新声明了这些函数。 Additionnaly, constructors starts with an upper-case letter as a convention in JS and you should probably follow it. 此外,构造函数以大写字母开头,这是JS中的约定,您应该遵循它。

function ApiDataLoader() {
    //...
}

ApiDataLoader.prototype = {
    constructor: ApiDataLoader,
    load: function () {
        //...
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM