Reputation: 41
I have the code below:
define(["dojo/_base/declare"],function (declare) {
return declare("tamoio.Map", null, {
methodA: function(param){
console.log(param);
this.methodB('xxx',function(){
this.methodC(); //it didn't call!
});
},
methodB: function(text, callback){
alert('do nothing: ' + text);
callback();
},
methodC: function(){
alert('hello');
}
});
});
When I run my application I received the messege:
Uncaught TypeError: Object [object global] has no method 'methodC'
How to do I invoke a internal method in my module?
I'm using Dojo 1.9.1
Best regards,
Renan
Upvotes: 2
Views: 2908
Reputation: 16518
You are getting this error because your callback function is being executed in the global scope (window), and there is no function called methodC
defined. You need to get methodC
to execute in the scope of your widget, and there are two ways of doing this:
1.) Take advantage of JavaScript closures:
methodA: function(param){
console.log(param);
var self = this; // Create a reference to the widget's context.
this.methodB('xxx',function(){
self.methodC(); // Use the widget scope in your anonymous function.
});
}
2.) Take advantage of the dojo/_base/lang module's hitch
method:
methodA: function(param){
console.log(param);
this.methodB('xxx', lang.hitch(this, function(){
this.methodC();
}));
}
The hitch
method returns a function that will be executed in the context provided, in this case it is this
(the widget).
Upvotes: 3