如何从extjs 4商店获取数据 [英] How to get data from extjs 4 store
问题描述
我发现很多描述如何创建存储,但不是如何访问其中的数据。
我设法获取数据使用Ext ajax请求,但我认为会更好的使用商店,我不能避免他们..
我的模型:
Ext.define('MyApp.model.User',{
extends:'Ext.data.Model',
fields:[
'id',
'username',
'email'
]
});
我的商店看起来像:
Ext.define('MyApp.store.User.CurrentUser',{
extends:'Ext.data.Store',
require:'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'],
存储:['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({
范围:这个
回调函数(记录,操作,成功){
//这里的商店已经加载,所以你可以使用你喜欢的功能
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屋!