javascript 如何从 extjs 4 商店获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8367074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from extjs 4 store
提问by user1063963
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..
我一开始就与 ext js 4 堆栈。我试图在使用商店启动应用程序时获取当前用户数据。但是我没有从商店中获取任何数据,甚至 store.count 返回 0。我发现了很多关于如何创建商店的描述,但没有找到如何访问其中的数据。
我设法使用 Ext ajax 请求获取数据,但我认为使用 store 会更好,我无法避免它们..
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:
返回的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());
}
});
回答by nscrob
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();
}
});
回答by Neil McGuigan
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.
所有sencha示例在store中都有代理,但您应该将代理实际放在模型中,以便您可以使用model.load方法。store 继承了模型的代理,一切都按预期工作。
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.
它看起来像model.load虽然对id进行了硬编码(而不是使用idProperty),而且据我所知,它总是必须是一个int。
good luck!
祝你好运!