Rachel
Rachel

Reputation: 103397

How to deal with not a function error in dojo?

I am getting not a function error for my callChart Function in dojo. I am developing a website, which uses excanvas to paint some charts on the browser.

On page load I am having one Ajax call which fetches some database values and passes them to callChart:function(), this callChart function call excanvas for charting. But here I am getting callChart is not a function.

Code snippet is like below:

onload I am having executing

 drawChart : function(){     
     this.chart(true);     
 },

chart: function(flag){
 dojo.xhrPost( {
 url : "/charting.html", 
 load : function(data){
          loadChart(data);
},
 error : function(error){
         console.error(" occured while fetch chart" );
    }
  } );

And in success callback method

loadChart : function(response){
        this.callChart(response);

},

and this is my callChart,

callChart:function(chartData){
----
----
},

When I am calling this.callChart(response), I am getting firebug error "this.callChart(response) is not a function" .

Any help, guidance or suggestions are highly appreciated.

Upvotes: 0

Views: 4649

Answers (1)

mwilcox
mwilcox

Reputation: 4132

Your "this" is the callback mechanism (dojo.Deferred). Use dojo.hitch to fix the context:

chart: function(flag){
 dojo.xhrPost( {
 url : "/charting.html", 
 load : dojo.hitch(this, function(data){ // changed this line
          this.loadChart(data); // added "this"
 }); // close paran
}

Upvotes: 1

Related Questions