How to get data from extjs 4 store

Im stacking with ext js 4 at the very beginning. Im trying to get current user data at application startup using storage. But I do not receive any data from the store, not even returning store.count 0. I found many descriptions of how to create a repository, but not how to access the data in it.
I managed to get the data using an Ext ajax request, but I think it is better to use the storage and I can not avoid it.

My model:

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

My store looks like this:

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

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()); } }); 
+4
source share
2 answers

The problem itself is not that there is no data in the store, the problem is that the store load is asynchronous, so when you count the store records, the store is practically empty. To "fix" this, use the store load callback method.

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

All sencha examples have proxies in storage, but you really have to put proxies in the model so you can use the model.load method. the repository inherits a model proxy and everything works as expected.

it looks like a model.load hardcodes identifier, though (instead of using idProperty), and it should always be int, as far as I can tell.

Good luck

0
source

Source: https://habr.com/ru/post/1384460/


All Articles