【问题标题】:Unable to call method in ExtJS class from inside the class无法从类内部调用 ExtJS 类中的方法
【发布时间】:2013-07-16 19:19:05
【问题描述】:

当我尝试执行下面的代码时,我收到两条 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 

如果在store 上方定义_createStore,错误仍然会发生。我正在使用 ExtJS 4.2.1.883。

【问题讨论】:

    标签: extjs


    【解决方案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'
    

    【讨论】:

    • 太棒了,我需要从initComponent 调用它。我在谷歌上搜索了一下this SO post,这解释了它为什么起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多