开发者

Dojo button created programmatically-Scope problem

Dear All, I've created a new Dojo button programatically. I'm doing that in one of my custom dojo class. While creating the button, I've defined an onClick method which should be called when the button is clicked. This method is part of the class. I'm not able to invoke that method, since the scope of "this" is different when the button is clicked. Can some one please help me to do fix this?

dojo.declare("CustomClass",null,{
createCustomButton:function(){
var button = new dijit.form.Button({onClick:function(){
                    removetrack();
                    testDataGrid.filter({status:"COMPLETED"});
                }},"testButton1");
},
removetrack:function(){
//some logic
}
});


var customObj=new C开发者_C百科ustomClass();
customObj.createCustomButton();

I need removetrack() method to be called when I click on the Button created.


Use dojo.hitch();

dojo.declare("CustomClass",null,{
    createCustomButton:function(){
        var button = new dijit.form.Button({
            onClick:dojo.hitch(this, function(){
                this.removetrack();
                testDataGrid.filter({status:"COMPLETED"});
            })
        },"testButton1");
    },
    removetrack:function(){
        //some logic
    }
});


var customObj=new CustomClass();
customObj.createCustomButton();


I cannot manage to do better way, in case you need urgent fix

var button = new dijit.form.Button({
                    label: "Custom!",
                    onClick:function(){
                    CustomClass().removetrack();
                }},"result");

Hope someone can give you better option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜