0
我有一個應用程序,它的第一個頁面與不同的組件構建。我在頁面加載(main.js)創建一個userStore實例我想訪問userStore在我的標題中獲取用戶的名字並將其顯示在標題部分。如何調用Extjs商店而不創建它的明確實例
Ext.create('store.User', {
storeId : 'storeUser'
});
Ext.define('OnlineApp.view.Main', {
extend : 'Ext.panel.Panel',
xtype : 'app-main',
requires : [
'Ext.plugin.Viewport'
],
controller : 'main',
viewModel : 'main',
layout : 'border',
padding : '0 0 0 0',
bodyStyle : 'border:0',
items : [
{
xtype : 'appHeader',
region : 'north'
},
{
xtype : 'tabpanel',
region : 'center',
bodyPadding : 0,
id : 'contentPanel',
reference : 'contentPanel',
layout : 'card',
border : false,
scrollable : true,
tabBar : {
hidden : true
},
items : [
firstPage,
contentFrame,
],
},
{
xtype : 'footer',
region : 'south'
}
]
});
** This is a Header file where I am trying to use the store below. Not sure what could be the best way to call store without creating a instance.**
Ext.define('OnlineApp.view.Header', {
extend: 'Ext.container.Container',
xtype : 'appHeader',
id : 'main-header',
height : 100,
layout : {
type : 'hbox',
align : 'middle'
},
initComponent: function() {
this.items = [
{
xtype : 'container',
flex : .55,
layout : {
type: 'vbox',
align : 'right'
},
collapsible : false,
padding : 5,
items : [
{
xtype : 'container',
id : 'app-header-user',
ui : 'usp-plain-button',
layout : {
type : 'hbox',
align : 'center'
},
items : [
{
xtype : 'component',
html : 'Welcome, <b>' + Ext.getStore('storeUser').data.firstname + '</b>' **// I am trying to use the store getting fiest name as undefined.**
},
{
xtype : 'gear-menu'
}
]
},
}
];
this.callParent();
}
});
在這種情況下,您的商店肯定有問題。你可以添加關於你的商店的代碼嗎?因爲在示例代碼中您的商店是空的。 – pagep
是的,我有一個正確定義的商店。我嘗試使用Ext.getStore('storeUser')。getAt(0).get('firstname'),我碰到一個錯誤「Can not read property'get'of null」但是創建顯式實例我得到它的工作。我會嘗試模板建議。 Ext.create('store.User')。load({scope:this,callback:function(records,operation,success)){} {var headerObj = this.items.items [1] .items.items [0]。 items.items [0]; headerObj .setHtml('Welcome,'+ records [0] .data.firstname +'');}}); – divein
Ext.define( 'OnlineApp.store.User',{ 延伸\t \t: 'Ext.data.Store', 別名\t \t: 'store.User', 模型\t \t: 'OnlineApp.model.User' , autoLoad \t:true }); – divein