如何从 extjs 4 商店获取数据 [英] How to get data from extjs 4 store

查看:23
本文介绍了如何从 extjs 4 商店获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的模型:

Ext.define('MyApp.model.User', {扩展:'Ext.data.Model',字段: ['ID','用户名','电子邮件']});

我的商店看起来像:

Ext.define('MyApp.store.User.CurrentUser', {扩展:'Ext.data.Store',需要:'MyApp.model.User',模型:'MyApp.model.User',自动加载:真,代理人: {类型:'阿贾克斯',方法:'POST',url: Routing.generate('admin_profile'),读者:{类型:'json',根:'用户'}}});

返回的json:

<代码>{成功":真,用户":[{身份证":1,"用户名":"r00t","email":"root@root.root"}]}

和应用程序:

Ext.application({name: 'MyApp',appFolder: '/bundles/myadmin/js/app',模型:['MyApp.model.User'],商店:['MyApp.store.User.CurrentUser'],//自动创建视口:真,启动:函数(){var currentUser=Ext.create('MyApp.store.User.CurrentUser',{});/*Ext.Ajax.request({url : Routing.generate('admin_profile'),方法:'POST',成功:功能(响应){var options = Ext.decode(resp.responseText).user;Ext.each(选项,功能(操作){var user = Ext.create('MyApp.model.User',{id: op.id,username:op.username,email:op.email});设置用户(用户);})}});*/currentUser.load();警报(currentUser.count());}});

解决方案

问题本身不在于 store 不包含数据,问题在于 store 负载是异步的,因此当你统计 store 记录时,store实际上是空的.要修复"此问题,请使用商店加载的回调方法.

currentUser.load({范围:这个,回调:函数(记录,操作,成功){//这里已经加载了商店,所以你可以使用你喜欢的功能currentUser.count();}});

Im stack with ext js 4 at the very beginning. Im trying to get the current user data when starting the application using store. But Im not getting any data from the store, even the store.count return 0. I found many description how to create store, but not how to access the data in it.
I managed to get the data using Ext ajax request, but i think would be better using store and i cant avoid them..

My model:

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

My store looks like:

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'
        }
    }
});

The returned json:

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

And the application:

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());
    }
});

解决方案

The problem itself isn't that the store does not contain data, the problem is that the store load is asyncronous therefore when you count the store records, the store is actualy empty. To 'fix' this, use the callback method of the store load.

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();
    }
});

这篇关于如何从 extjs 4 商店获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆