2011-03-03 101 views
0

尊敬的全部, 我已经以编程方式创建了一个新的Dojo按钮。我在我的一个自定义dojo类中这样做。在创建按钮时,我定义了一个onClick方法,该按钮在点击按钮时应该被调用。这个方法是类的一部分。我无法调用该方法,因为单击按钮时,“this”的范围有所不同。有人可以帮我解决这个问题吗?以编程方式创建的Dojo按钮 - 范围问题

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 CustomClass(); 
customObj.createCustomButton(); 

我需要removetrack()当我点击创建的按钮时调用方法。

+0

从你的问题中不清楚你希望调用removetrack()的范围。我将在下面回答假设它是createCustomButton中的范围,但如果这是错误的,请澄清,我会看看是否我可以改变我的答案:) – mrtom 2011-03-03 12:01:10

回答

0

使用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(); 
+0

非常感谢。我认为我们需要使用this.removetrack()。 – Steves 2011-03-03 13:00:07

+0

是的,你会,很好的地方。我会更新我的代码示例 – mrtom 2011-03-03 13:58:30

-1

我无法做到更好的方法,如果你需要紧急修复

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

希望有人可以给你更好的选择。

+0

不,这是行不通的。这看起来像是一个附加到CustomClass函数的removetrack函数。 – mwilcox 2011-03-04 20:10:42

相关问题