简体   繁体   English

Extjs在视图之前加载存储

[英]Extjs Load store before the view

I have a view which gous like this: 我有这样的看法:

Ext.define('Webedi.view.zones.Menu', {
    store: 'PartnerSettings',
    extend: 'Ext.panel.Panel',
            .
            .
            .
            .
    adminPanelEnabled: false,
    initComponent: function() {
        var me = this;

console.log(Ext.getStore('Userrights').findExact("name","admin_panel_access"));

          if(Ext.getStore('Userrights').findExact("name","admin_panel_access") !== -1) me.adminPanelEnabled = true;
//        if(Ext.getStore('PartnerSettings').getAt(0).get('purchasingOrganisation').enabled ) me.purchasingOrganisationEnabled = true;
        this.items = [
                    .
                    .
                    .
                    .

                    {
                        id: 'adminpanel',
                        itemId: 'adminpanel',
                        xtype: 'button',
                        text: Translation.ZonesMenuAdminPanel,
                        action: 'adminpanel',
                        margin: '3 3 0 3',
                        hidden: !me.adminPanelEnabled
                    }
                ]
            }
        ];

        this.callParent();
    }
});

The problem is that 问题是

 if(Ext.getStore('Userrights').findExact("name","admin_panel_access") !== -1)

Is not yet filled on the time when the part of code runs for: 代码部分运行时间:

 hidden: !me.adminPanelEnabled

initComponent function Kicks in? initComponent函数是否起作用?

You should use callbacks when working with stores (generally when doing ajax calls). 在处理商店时(通常在进行ajax调用时),应使用回调。 In your case your views should either be created when the store finished loading (use the load event and let the callback create your view) or create a adminPanelEnabled() method that get called by the load event of the store. 在您的情况下,应在商店完成加载后创建视图(使用load事件并让回调创建您的视图),或者创建一个由商店的load事件调用的adminPanelEnabled()方法。

Both variants have one thing in common, they get executed when the store finished loading. 两种变体有一个共同点,它们在商店完成加载后执行。

In addition: if you just need this once you can register the event as sort of singleton 此外:如果只需要一次,则可以将事件注册为单例

store.on('load', function(/*see api for ful arg list*/) {}, scope, { single:true })

Ok thank you @seba for the idea i created in the consructor 好的,谢谢@seba我在主顾中创建的想法

    applicationName.getApplication().on('allStoresLoaded', function() {
        console.log(Ext.getStore('Userrights').findExact("name", "admin_panel_access"))

    });

And it works now :D 它现在可以工作了:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM