2013-07-16 43 views
1

内调用ExtJS的类方法,当我尝试下面执行的代码,我得到两个Uncaught ReferenceError消息无法从类

Ext.define('Example.foo', { 
    store: _createStore(), 
    _createStore: function() { 
    return new Ext.data.JsonStore({...}); 
    } 
}); 
var example = new Example.foo(); 

错误消息:

Uncaught ReferenceError: _createStore is not defined 
Uncaught ReferenceError: Example is not defined 

仍然出现错误如果_createStore被定义为高于store。我正在使用ExtJS 4.2.1.883。

回答

1

如果foo是一些UI组件,你必须使用一个函数来获取到您的商店的参考,你可以这样做:

Ext.define('Example.Foo', { 

    // set other Foo properties... 

    initComponent: function() { 
     this.store = this.createStore(); 
     this.callParent(arguments); 
    }, 

    createStore: function() { 
     return Ext.create("Ext.data.JsonStore", { 
      // store setup 
     }); 
    } 

}); 

如果你有一个类,它表示你的店,不过,你可以只需使用商店类的字符串名称,Ext框架将为您创建一个实例:

store: 'Example.stores.FooStore' 
+0

真棒,从'initComponent'调用它就是我需要的。一些谷歌搜索引导我[这个SO帖子](http://stackoverflow.com/questions/14492179/to-initcomponent-or-not-to-initcomponent),解释了为什么它的工作。 – anjunatl