2013-02-27 25 views
0

我有一個UserStore,我想加載成功登錄的用戶。我無法得到這個工作,即找到一個模式來做到這一點。Sencha - 登錄後在需求加載商店

我現在有這樣的app.js的UserStore:

stores : ['UserStore'] 

商店

Ext.define('MyApp.store.UserStore', { 
extend : 'Ext.data.Store', 
xtype : 'userstore', 
config : { 
    model : 'MyApp.model.User', 
    autoLoad : true, 
    proxy : { 
     type : 'ajax', 
     url : 'php/get_user.php', 
     reader : { 
      type : 'json', 
      rootProperty : 'users' 
     } 
    }, 
    listeners : { 
     beforeload : function() { 
      console.log('Before load'); 
     }, 
     load : function(store) { 
      console.log('load'); 
     } 
    } 
} 
}); 

用戶是基於PHP的$ _SESSION檢索[ '用戶ID']這在用戶登錄之前未設置。

啓動應用程序時,商店被加載,但沒有找到任何數據。我需要回到開始再次登錄,然後當然會話ID在前面的登錄中設置。

我想完成的是要麼延遲加載商店,要麼只在視圖需要時自動加載。

我已經試過這個,但是我不能得到它的工作。

這是我做過什麼:

選項1

我從app.js刪除UserStore並增加了一個需要和的xtype項目的看法,但然後我得到 [WARN] [分機.dataview.DataView#applyStore]指定的商店無法找到

Ext.define('MyApp.view.Profile', { 
extend : 'Ext.Panel', 
xtype : 'profileview', 

requires : ['MyApp.store.UserStore', 'Ext.List', 'Ext.DataView', 'Ext.data.Store'], 

config : { 
    layout : 'fit', 
    title : 'Profiel', 
    iconCls : 'user3', 
    cls : 'home', 
    scrollable : true, 
    styleHtmlContent : true, 
    html : ['<h1>Mijn Profiel</h1>'].join(""), 
    items : [Ext.create('Ext.DataView', { 
     store : 'userstore', 
     itemTpl : '<h2>{USERNAME}</h2><p>{EMAIL}</p>' 
    })] 
} 
}); 

選項2

摸索一下,如果我可以設置自動加載到假,並加載在通過一些聽衆的需求。但我無法確切知道如何。

那麼,怎樣才能做到這一點,以及做到這一點的最佳模式是什麼。

感謝您的幫助! Ext.dataview.DataView#applyStore無法找到指定的商店

回答

3

我實際上從來沒有這樣指定商店:store : 'userstore'。更好的方法是創建商店的一個實例並自己加載它,在我的商店中使用autoLoad:false,我不喜歡它們在應用程序開始時加載。試試這個(我無法測試它,因爲我通常不會編程觸摸應用程序)。

Ext.define('MyApp.view.Profile', { 
    extend: 'Ext.Panel', 
    xtype: 'profileview', 

    requires: ['MyApp.store.UserStore', 'Ext.List', 'Ext.DataView', 'Ext.data.Store'], 

    config: { 
     layout: 'fit', 
     title: 'Profiel', 
     iconCls: 'user3', 
     cls: 'home', 
     scrollable: true, 
     styleHtmlContent: true, 
     html: ['<h1>Mijn Profiel</h1>'].join("") 
    }, 

    initialize: function() { 
     var me = this; 

     //Create the instance of the store and load it 
     var userStore = Ext.create('MyApp.store.UserStore'); 
     userStore.load(); 

     //Create the dataview 
     var view = Ext.create('Ext.DataView', { 
      store: userStore, 
      itemTpl: '<h2>{USERNAME}</h2><p>{EMAIL}</p>' 
     }); 
     //Add the dataview to the panel 
     me.add(view); 
    } 
}); 

我喜歡這種工作方式。

+0

謝謝,那正是我一直在尋找的! – 2013-02-27 10:17:42