4

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":"[email protected]"
    }]
}

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

2 Answers 2

5

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();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

All the sencha examples have the proxies in the store, but you should actually put the proxy in the model, so that you can use the model.load method. the store inherits the model's proxy, and it all works as expected.

it looks like model.load hardcodes the id though (instead of using idProperty), and it always has to be an int, as far as I can tell.

good luck!

1 Comment

Although the model.load() method does appear to use 'id' to load records, I believe if you've defined an idProperty on your model, that it will be used instead (not tested this though)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.