If you then want to call newItem
with the correct scope, you could use the dojo/_base/lang
, hitch()
function.
define(["dojo/_base/declare", "dojo/_base/lang", ....], function(declare, lang, ....){
return declare('my.widget', null ,function(..){
startup: function() {
new Button({onClick: lang.hitch(this, "newItem"}, newButtonNode)
},
newItem: function() {
this.openDialog({}, this.saveItemCallback)
},
openDialog: function(item,callback){...},
saveItemCallback: function(item){....}
})
});
The hitch()
function will make sure that the this
reference is correct. This sounds like real magic, but it uses some kind of reflection behind the screens to execute the function.
What it really does is using the Function.prototype.apply
function to get the magic started. As you can read in this article it accepts a context and the parameters. So it will in fact change the this
reference when newItem
is executed.