0

I'm trying to create a widget, and let it take care of its own ajax call, and do stuff with the data coming back after, however, i can't seems to be able to set the options internally?

$.widget("test" , {
    options: {
        url : null,
        data : null
    },
    _create: function() {
        var that = this;


        $.ajax({
            url : this.options.url,
            dataType : 'json',
            cache : false
        }).done(function(resp) {
            that._option("data", resp.stuff);

        });

        this._blah();
    },
    _option: function(key, val) {
        this.options[key] = val;
    },
    _blah: function() {
        console.log(this.options.data);
    }
}

the variable url is being set when the widget is triggered. I can't use this inside ajax, so i created that instead. But the data won't stick....

Also the weird thing is that, if i put a breakpoint inside the widget, the breakpoint would get trigger for the first time, but not the second... is there a better way to debug the jQuery widget?

codenamezero
  • 2,093
  • 23
  • 47
  • 1
    Ajax is asynchronous. you're executing _blah before the request has finished, which is also before the done callback gets executed. – Kevin B Jan 17 '14 at 15:57
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Kevin B Jan 17 '14 at 15:58
  • oh shoot, thank! bookmarked the other link. -_-" – codenamezero Jan 17 '14 at 16:03

0 Answers0