这是我的模特:
Ext.define('Email', {
extend: 'Ext.data.Model',
idProperty: 'emailid',
fields: [
{ name: 'emailid', type: 'int' },
{ name: 'emailsubject', type: 'string', useNull: true },
{ name: 'emailbody', type: 'string', useNull: true },
{ name: 'emailto', type: 'string', useNull: true },
{ name: 'emailfrom', type: 'string', useNull: true },
]
});
和DataStore:
var EmailDataStore = {
getDetailStore: function(aid) {
var options = {
autoLoad: true,
autoSync: true,
pageSize: 1,
remoteFilter: true,
remoteGroup: true,
remoteSort: true,
model: 'Email',
proxy: {
type: 'ajax',
url: 'datastores/datastore-email.asp?action=details&auditid=' + aid,
reader: {
type: 'json',
root: 'emailDetails'
}
}
};
if (typeof (options2) != 'undefined') {
options = Ext.merge(options, options2);
}
var detailStore = Ext.create('Ext.data.Store', options);
return detailStore;
}
}
我不会包含ASP,因为JSON正在适当地返回数据。但是在下一段代码中,
...
PortalEmailGrid.prototype.detailStore = null;
PortalEmailGrid.prototype.showEmailDetails = function (id) {
var self = this;
//alert(id);
self.detailStore = EmailDataStore.getDetailStore(id);
//view store at this state - object appears to exist with the returned record in it
console.log(self.detailStore);
var firstItem = self.detailStore.first();
//undefined here
console.log(firstItem);
//count is zero, but i can see the object in the console log above
alert('detailStore count: ' + self.detailStore.count());
var win = Ext.create('Ext.window.Window', {
title: 'Email Details',
store: self.detailStore,
height: 400,
width: 800,
bodyPadding: 4,
items: [
{
xtype: 'textfield',
name: 'tofield',
dataIndex: 'emailto',
fieldLabel: 'To'
},
{
xtype: 'textfield',
name: 'subjectfield',
dataIndex: 'emailsubject',
fieldLabel: 'Subject'
}
]
});
win.show();
}
我无法使用detailStore
数据填充字段。但是当我在Firebug中记录self.detailStore
时,我可以看到对象及其细节。 self.detailStore.count()
显示为零,但根据控制台,数据应该在那里。我怀疑由于这种差异,字段没有填充dataIndex
信息。前面的代码块也被调用:
var selected = self.grid.selModel.selected;
var id = selected.items[0].raw.emailid;
//var status = selected.items[0].raw.status;
PortalEmailGrid.prototype.showEmailDetails(id);
您能看到数据未从数据存储中加载的原因吗?
答案 0 :(得分:1)
存储加载是异步的,当您将计数记录到控制台时,存储尚未加载。 Firebug始终显示对象的“最新”状态,这就是您可以查看数据的原因。您需要监听商店中的加载事件。
因此,如果删除autoLoad配置,则可以执行以下操作:
self.detailStore = EmailDataStore.getDetailStore(id);
self.detailStore.load({
callback: function(){
console.log('loaded, do something');
}
});