2011-11-14 84 views
0

我的理解範圍的問題javascript.Let的假設我有以下代碼:如何在extjs中調用一個類的祖父函數?

Ext.define('MA.controller.user',{ 
    extend : 'Ext.app.Controller', 
    editUser:function(){}, 
    updateUser : function() { 
    Ext.Ajax.request({ 
     url : './editall', 
     callback : function(options, success, response) { 
     this.editUser(); 
     } 
    }) 
    }//eof init 
})//eof class 

正如你所看到的,this.editUser()嵌套到Ext.Ajax.request和UpdateUser兩個

這.editUser()將返回undefined。如何在callback中調用editUser?

回答

1

這只是一個範圍問題。在updateUser方法中,範圍是控制器,因此要在回調中調用editUser,只需將範圍添加到ajax請求

Ext.define('MA.controller.user',{ 
extend : 'Ext.app.Controller', 
editUser:function(){}, 
updateUser : function() { 
    //here this refers to the controller 
    Ext.Ajax.request({ 
    url : './editall', 
    scope: this, // add the scope as the controller 
    callback : function(options, success, response) { 
     this.editUser(); 
    } 
    }) 
}//eof init 
})//eof class 
相關問題