访问sencha上的父对象

时间:2012-05-28 06:46:53

标签: ajax templates mobile extjs

当ajax调用成功时,我需要更新sencha模板。示例代码如下所示

Ext.define('casta.view.Intro', {
    extend: 'Ext.tab.Panel',
    tpl: '<p> {username} </p>',
    initComponent: function(){
        //Be sure to use the var keyword for planet earth, otherwise its declared as a global variable
        //var planetEarth = { name: "Earth", mass: 1.00 };
       //this.setHtml(this.getTpl().apply(planetEarth));
         Ext.Ajax.request( 
        {    
            waitMsg: 'Bitte warten...', 
            url: 'http://localhost:8000/api/casta/user/?format=json', 

            success:function(response,options){ 
                //var responseData = Ext.util.JSON.decode(response.responseText); 
                  this.setHtml(this.getTpl().apply(response.responseText));
            }                       
        } 
        ); 
    }
});

错误控制台

this.getTpl is not a function
[Break On This Error]   

this.setHtml(this.getTpl().apply(response.responseText));

success我需要更新模板,但问题是内联函数success:function中未定义this.getTpl。它是在initcomponent之后定义的。我需要在success内调用它。任何想法??

1 个答案:

答案 0 :(得分:1)

它不是真正的父母。你无法前往dom去获得它,但是......

Ext.define('casta.view.Intro', {
    extend: 'Ext.tab.Panel',
    tpl: '<p> {username} </p>',
    initComponent: function(){
        //Be sure to use the var keyword for planet earth, otherwise its declared as a global variable
        //var planetEarth = { name: "Earth", mass: 1.00 };
       //this.setHtml(this.getTpl().apply(planetEarth));
       var me = this;
         Ext.Ajax.request( 
        {    
            waitMsg: 'Bitte warten...', 
            url: 'http://localhost:8000/api/casta/user/?format=json', 

            success:function(response,options){ 
                //var responseData = Ext.util.JSON.decode(response.responseText); 
                  me.setHtml(me.getTpl().apply(response.responseText));
            }                       
        } 
        ); 
    }
});    

阅读闭包。