extjs store.load 未填充,可能是根对象问题?
extjs store.load not populating, possibly root object issue?
我在加载我的第一个 ExtJS 商店时遇到问题。
var testStore = Ext.data.StoreManager.lookup('myUserStoreID');
testStore.load({
callback: function (records, operation, success) {
testStore.each(function (record) {
debugger;
var task = record.data.description;
console.log(task);
});
//debugger;
//var x2 = operation._response;
//var x3 = x2.responseText;
//var x4 = Ext.decode(x3);
////var x3 = Ext.JSON.decode(operation);
////var x2 = Ext.decode(operation.response);
//console.log(testStore);
}
});
在调试器中,如果我向下钻取 operation._response.responseText,我可以看到正确的数据,但记录是空白的。所以我知道这只与我的代码有关。如果我使用 Ext.decode 它会 return 一个对象。 return 数据自动填充我的商店,我做错了什么。
这是fiddler中对象的图片。
这是我正在尝试使用的模型...我知道它还没有所有字段。
Ext.define('ExtApplication1.model.UserModel', {
extend: 'ExtApplication1.model.Base',
requires: ['ExtApplication1.model.Base'],
fields: [
/*
The fields for this model. This is an Array of Ext.data.field.Field definition objects or simply the field name.
If just a name is given, the field type defaults to auto. For example:
*/
{ name: 'UserID', type: 'int' },
{ name: 'UserName', type: 'string' },
{ name: 'password', type: 'string' },
{ name: 'Email', type: 'string' },
{ name: 'GroupID' }
],
proxy: {
type: 'ajax',
url: 'http://localhost:49537/api/user/gettargetuser/xxx/xxx',
reader: {
type: 'json',
rootProperty: 'JSON'
}
}
这是我在 webapi 中的用户 class
这里是 MainModel.js 我创建商店的地方
Ext.define('ExtApplication1.view.main.MainModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.main',
data: {
name: 'ExtApplication1',
appName: xxx,
appHeaderIcon: '<span class="fa fa-desktop fa-lg app-header-logo">',
footer: 'Copyright - xxx- x
},
stores: {
sessionListByInterest: {
model: 'MainMenuListModel',
autoLoad: false //was true
},
myUserStore: {
model: 'UserModel',
storeId: 'myUserStoreID',
autoLoad: false
},
问题是商店希望 rootProperty 包含一组记录,而您只提供一条记录。
如果您只想加载不在数组中的单个记录,则必须使用静态 model.load()
函数。
或者您可以将 API 端点从 User JSON
更改为 List<User> JSON
(或 IEnumerable<User>
或 ICollection<User>
或 User[]
... ),即使您打算只 return 一个用户。 JSON 数组中有多少条记录并不重要,但 ExtJS 存储需要该数组。
我在加载我的第一个 ExtJS 商店时遇到问题。
var testStore = Ext.data.StoreManager.lookup('myUserStoreID');
testStore.load({
callback: function (records, operation, success) {
testStore.each(function (record) {
debugger;
var task = record.data.description;
console.log(task);
});
//debugger;
//var x2 = operation._response;
//var x3 = x2.responseText;
//var x4 = Ext.decode(x3);
////var x3 = Ext.JSON.decode(operation);
////var x2 = Ext.decode(operation.response);
//console.log(testStore);
}
});
在调试器中,如果我向下钻取 operation._response.responseText,我可以看到正确的数据,但记录是空白的。所以我知道这只与我的代码有关。如果我使用 Ext.decode 它会 return 一个对象。 return 数据自动填充我的商店,我做错了什么。
这是fiddler中对象的图片。
这是我正在尝试使用的模型...我知道它还没有所有字段。
Ext.define('ExtApplication1.model.UserModel', {
extend: 'ExtApplication1.model.Base',
requires: ['ExtApplication1.model.Base'],
fields: [
/*
The fields for this model. This is an Array of Ext.data.field.Field definition objects or simply the field name.
If just a name is given, the field type defaults to auto. For example:
*/
{ name: 'UserID', type: 'int' },
{ name: 'UserName', type: 'string' },
{ name: 'password', type: 'string' },
{ name: 'Email', type: 'string' },
{ name: 'GroupID' }
],
proxy: {
type: 'ajax',
url: 'http://localhost:49537/api/user/gettargetuser/xxx/xxx',
reader: {
type: 'json',
rootProperty: 'JSON'
}
}
这是我在 webapi 中的用户 class
这里是 MainModel.js 我创建商店的地方
Ext.define('ExtApplication1.view.main.MainModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.main',
data: {
name: 'ExtApplication1',
appName: xxx,
appHeaderIcon: '<span class="fa fa-desktop fa-lg app-header-logo">',
footer: 'Copyright - xxx- x
},
stores: {
sessionListByInterest: {
model: 'MainMenuListModel',
autoLoad: false //was true
},
myUserStore: {
model: 'UserModel',
storeId: 'myUserStoreID',
autoLoad: false
},
问题是商店希望 rootProperty 包含一组记录,而您只提供一条记录。
如果您只想加载不在数组中的单个记录,则必须使用静态 model.load()
函数。
或者您可以将 API 端点从 User JSON
更改为 List<User> JSON
(或 IEnumerable<User>
或 ICollection<User>
或 User[]
... ),即使您打算只 return 一个用户。 JSON 数组中有多少条记录并不重要,但 ExtJS 存储需要该数组。