【问题标题】:Pass config to a custom Ext.tree.Panel and then to a custom Ext.data.Store将配置传递给自定义的 Ext.tree.Panel,然后传递给自定义的 Ext.data.Store
【发布时间】:2013-05-24 22:07:38
【问题描述】:

如何将变量传递给扩展的Ext.tree.Panel,而后者又会将自定义的Ext.data.Store 传递给它。

这是我的代码:

Ext.define('CustomStore', {
    extend: 'Ext.data.TreeStore',
    alias: 'widget.customstore',
    folderSort : true,
    model : 'TreeNode',
    autoLoad: true,
    config: {
        customParam: 'defaultVal'
    },
    ...
    proxy: {
        url: '/some/url?param'+this.customParam,
        ...
    }
});
Ext.define('CustomTree', {
    extend: 'Ext.tree.Panel',
    alias: 'widget.customtree',
    config: {
        customParam2: 'defaultVal'
    },
    store: new CustomStore({customParam: this.customParam2'}),
    ...
});

var tree = Ext.create('CustomTree', {customParam2: 'someVal'});

如您所见,我想将值 someVal 传递给树,它应该将其传递给商店,然后商店的代理需要将其拾取并在其加载 url 中使用。

尝试了很多事情,仅举几例:configinitConfigconstructorinitComponent,但没有好的结果。

【问题讨论】:

    标签: javascript extjs extjs4 extjs4.1


    【解决方案1】:

    你有正确的成分,但你没有按正确的顺序混合它们。

    这里的问题是你的商店创建代码:

    new CustomStore({customParam: this.customParam2'})
    

    CustomTree 的定义之前被调用:

    Ext.define('CustomTree', ...)
    

    这是因为new CustomStore(...) 被用作define 函数的参数。所以,很明显,它也是在设置customParam2值的那一行之前调用的:

    var tree = Ext.create('CustomTree', {customParam2: 'someVal'});
    

    所以为了让它工作,你想在CustomTree的构造函数被调用时创建你的商店。但是在使用组件时,最好重写initComponent 而不是构造函数。所以你应该这样做:

    Ext.define('CustomTree', {
        extend: 'Ext.tree.Panel',
        alias: 'widget.customtree',
        config: {
            customParam2: 'defaultVal'
        },
        // remove that
        // store: new CustomStore({customParam: this.customParam2'});
    
        // ... and put it in there:
        initComponent: function() {
    
            // creates the store after construct
            this.store = new CustomStore({customParam: this.customParam2});
    
            // call the superclass method *after* we created the store
            this.callParent(arguments);
        }
        ...
    });
    

    至于initConfig,您必须在构造函数中调用它才能应用配置参数。但在您的情况下,您是从 Ext.data.StoreExt.tree.Panel 扩展的,并且它们的构造函数已经调用它,因此您不必自己做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2021-12-28
      • 2019-11-07
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多