views:

96

answers:

2

Hi there,

can anyone help, i have an issue with the keyword this.. before entering the ajax call its available but when entering Success. my "this" is available but doesn't contain the same info i.e. a method i wish to call.. This example shows what i mean..

I would appreciate any help, this.isoDateReviver is available before doing ajax.. and then when success arives .. this.isoDateReiver is UNDEFINED

   var data = new Object();
data.year = this.today = new Date().getFullYear();

this.isoDateReviver("yes","yes");                //// THIS WORKS HERE

$.ajax({
    type: "POST",
    url: "MyService.aspx/GetHolidays",
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        var holidays = JSON.parse(msg.d,
       this.isoDateReviver);             // THIS DOES NOT WORK its undefined

        Calendar.initalizeHolidays(holidays);
    },
    error: function(msg) {
        alert(error);
    }
});
+5  A: 

its because the success function is executed in a different scope.

you need to do:

var me = this;

$.ajax({
    // etc
success: function(msg) {
    var holidays = JSON.parse(msg.d, me.isoDateReviver);
Andrew Bullock
Be careful with "self" as you overwriting the "self" reference to the current frame with this, might break other code.
fforw
edited accordingly
Andrew Bullock
A: 

see: http://www.alistapart.com/articles/getoutbindingsituations

the function called on success is not working in the same context as the previous call to this.isoDateReceiver.

Jonathan Fingland