无法从类内部调用ExtJS类中的方法

时间:2013-07-16 19:19:05

标签: 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 个答案:

答案 0 :(得分: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'