最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript: Setting class property from ajax success - Stack Overflow

programmeradmin1浏览0评论

I have a "class"/function called Spotlight. I'm trying to retrieve some information through ajax and assign it to a property of Spotlight. Here is my Spotlight class:

function Spotlight (mId,mName) {
    this.area = new Array();

    /**
     * Get all area information
     */
    this.getArea = function () {

        $.ajax({
            url: base_url +'spotlight/aGetArea',
            type: 'POST',
            success: function (data) {
                this.area = data;
            }
        });
    }
}

I have assigned the object to an array, and it would be difficult to get to it from within Spotlight, so I'm hoping to access everything using 'this.' It appears though that the success function is outside of the class, and I don't know how to make it inside the class.

Is there a way to get the data into the class property using this.area rather than Spotlight.area?

I have a "class"/function called Spotlight. I'm trying to retrieve some information through ajax and assign it to a property of Spotlight. Here is my Spotlight class:

function Spotlight (mId,mName) {
    this.area = new Array();

    /**
     * Get all area information
     */
    this.getArea = function () {

        $.ajax({
            url: base_url +'spotlight/aGetArea',
            type: 'POST',
            success: function (data) {
                this.area = data;
            }
        });
    }
}

I have assigned the object to an array, and it would be difficult to get to it from within Spotlight, so I'm hoping to access everything using 'this.' It appears though that the success function is outside of the class, and I don't know how to make it inside the class.

Is there a way to get the data into the class property using this.area rather than Spotlight.area?

Share Improve this question asked Jan 14, 2013 at 18:49 TylerTyler 3,8138 gold badges40 silver badges68 bronze badges 2
  • 1 I think you want to get the data from your ajax request and use it elsewhere in your class? Defined a new function, lets say newFun(newData) and then in your success function call, newFun(data). Is that what you want to do? – Leeish Commented Jan 14, 2013 at 18:51
  • That may also work. I ended up using the context option in $.ajax. – Tyler Commented Jan 14, 2013 at 19:03
Add a comment  | 

3 Answers 3

Reset to default 14

The value of this is dependent on how each function is called. I see 3 ways around this problem:

1. Creating an alias to this

var that = this;
this.getArea = function () {
    $.ajax({
        url: base_url +'spotlight/aGetArea',
        type: 'POST',
        success: function (data) {
            that.area = data;
        }
    });
};

2. Using jQuery .ajax context option

this.getArea = function () {
    $.ajax({
        url: base_url +'spotlight/aGetArea',
        type: 'POST',
        context : this,
        success: function (data) {
            this.area = data;
        }
    });
};

3. Using a bound function as the callback

this.getAreaSuccess = function (data) {
    this.area = data;
};
this.getArea = function () {
    $.ajax({
        url: base_url +'spotlight/aGetArea',
        type: 'POST',
        success: this.getAreaSuccess.bind(this)
    });
};

Well Spotlight.area wouldn't work anyway. You just need to preserve the outer this:

this.area = [];
var theSpotlight = this;

and then in the callback:

  theSpotlight.area = data;

When inside the success function, this corresponds to the scope of the success function.

function Spotlight (mId,mName) {
    this.area = [];
    var scope = this;

    /**
     * Get all area information
     */
    this.getArea = function () {

        $.ajax({
            url: base_url +'spotlight/aGetArea',
            type: 'POST',
            success: function (data) {
                scope.area = data;
            }
        });
    }
}
发布评论

评论列表(0)

  1. 暂无评论