【问题标题】:How to get data from extjs 4 store如何从 extjs 4 商店获取数据
【发布时间】:2011-12-05 00:41:23
【问题描述】:

我一开始就与 ext js 4 堆栈。我试图在使用 store 启动应用程序时获取当前用户数据。但我没有从商店获得任何数据,即使 store.count 返回 0。 我发现了很多关于如何创建商店的描述,但没有找到如何访问其中的数据。
我设法使用 Ext ajax 请求获取数据,但我认为使用商店会更好,我无法避免它们..

我的模特:

Ext.define('MyApp.model.User', {
   extend: 'Ext.data.Model',
   fields: [ 
        'id',
        'username',
        'email'
    ]
});

我的商店看起来像:

Ext.define('MyApp.store.User.CurrentUser', {
    extend: 'Ext.data.Store',
    requires: 'MyApp.model.User',
    model: 'MyApp.model.User',
    autoLoad: true,

    proxy: {
        type: 'ajax',
        method: 'POST',
        url: Routing.generate('admin_profile'),
        reader: {
            type: 'json',
            root: 'user'
        }
    }
});

返回的json:

{
    "success":true,
    "user":[{
        "id":1,
        "username":"r00t",
        "email":"root@root.root"
    }]
}

以及应用程序:

Ext.application({
    name: 'MyApp', 
    appFolder: '/bundles/myadmin/js/app',
    models: ['MyApp.model.User'],    
    stores: ['MyApp.store.User.CurrentUser'],
    //autoCreateViewport: true,
    launch: function() {
        var currentUser=Ext.create('MyApp.store.User.CurrentUser',{});

        /*
        Ext.Ajax.request({
            url : Routing.generate('admin_profile'),
            method: 'POST',
            success: function(resp) {
                var options = Ext.decode(resp.responseText).user;

                Ext.each(options, function(op) {
                    var user = Ext.create('MyApp.model.User',{id: op.id,username:op.username,email:op.email});
                    setUser(user);
                }
            )}
        });
        */

        currentUser.load();
        alert(currentUser.count());
    }
});

【问题讨论】:

    标签: javascript extjs extjs4


    【解决方案1】:

    问题本身不在于存储不包含数据,问题在于存储加载是异步的,因此当您计算存储记录时,存储实际上是空的。 要“修复”这个问题,请使用商店加载的回调方法。

    currentUser.load({
        scope   : this,
        callback: function(records, operation, success) {
            //here the store has been loaded so you can use what functions you like
            currentUser.count();
        }
    });
    

    【讨论】:

      【解决方案2】:

      所有的sencha示例在store中都有代理,但是你应该把代理放在模型中,这样你就可以使用model.load方法了。 store 继承了模型的代理,一切都按预期工作。

      看起来 model.load 对 id 进行了硬编码(而不是使用 idProperty),据我所知,它始终必须是一个 int。

      祝你好运!

      【讨论】:

      • 虽然 model.load() 方法似乎使用 'id' 来加载记录,但我相信如果您在模型上定义了 idProperty,它将被使用(未对此进行测试不过)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2013-05-01
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      相关资源
      最近更新 更多