1

How can I access object properties from functions which will be called as callback function. Please see code below. How can I access config property from processData function which will called when data is received from the server in ajax call.

MyClass: {
    config: {
        name: "",
        id: ""
    },

    loadData: function() {
        MyApi.getData(
            this.config,
            this.processData,   //sucess
            this.failureHandler //failure
        );
    },

    processData: function() {
        // how to access config object here? 
    }
}

Probably you can create an anonymous handler function and use call or apply to pass the this scope to actual handler function but is there a better solution than that?

bluetech
  • 1,380
  • 3
  • 13
  • 22
  • possible duplicate of [Preserve 'this' reference in javascript prototype event handler](http://stackoverflow.com/questions/8100469/preserve-this-reference-in-javascript-prototype-event-handler) – t.niese Mar 31 '15 at 20:55
  • `this.processData.bind(this)` is you don't want anonymous function. – dfsq Mar 31 '15 at 21:12
  • Where is MyApi declared? –  Apr 01 '15 at 01:20
  • MyApi is another object written in a separate file: MyApp. MyApi with namespace. Think of it as a wrapper around $.ajax – bluetech Apr 01 '15 at 17:38

0 Answers0