我从一开始就使用 ext js 4。我试图在使用商店启动应用程序时获取当前用户数据。但我没有从商店获取任何数据,即使 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());
}
});
最佳答案
问题本身不是存储不包含数据,问题是存储加载是异步的,因此当您计算存储记录时,存储实际上是空的。 要“修复”此问题,请使用商店加载的回调方法。
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();
}
});
关于javascript - 如何从 extjs 4 存储中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8367074/