2013-02-27 44 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

相关问题