我的控制器上有一个方法,当我的应用程序中的网格被 itemclicked 时触发。
单击网格行时,我创建一个详细信息窗口小部件的新实例,获取它的视图模型,并创建一个链接。创建此链接后,我将显示窗口:
...
itemClicked: function (item, record, index, event, eOpts){
var detailsWindow = Ext.widget('sessiondetails'),
viewmodel = detailsWindow.getViewModel();
viewmodel.linkTo('sessiondetails', {
reference: 'Registration.model.SessionDetails',
id: record.id
});
detailsWindow.show();
}
...
在 linkTo 方法调用中引用的模型类有一个 rest 代理配置,所以当 linkTo 被触发时,会对数据发出 GET 请求:
Ext.define('Registration.model.SessionDetails', {
extend: 'Ext.data.Model',
fields: [
...
],
proxy:{
type: 'rest',
url: 'sessions',
reader: {
type: 'json',
rootProperty: 'record'
}
}
});
这一切都很好。我想弄清楚的是如何隐藏或至少屏蔽窗口小部件,直到记录实际加载。
现在,窗口出现,延迟一两秒,然后在返回 GET 请求的结果后出现数据。
我希望能够显示被屏蔽的窗口,然后在数据加载后取消屏蔽它。我知道我可以.mask()
在显示窗口后使用该方法:
...
detailsWindow.show();
detailsWindow.mask('Loading...');
但我不知道如何在 ViewModel 完成加载记录后删除掩码。
我该怎么做?我接近它错了吗?
更新:修复
根据 Robert Watkins 回答中的第二个选项,我将代码重构为:
- 立即显示和屏蔽窗口
- 直接从模型加载记录
- 在加载方法的成功回调中
- 将返回的数据设置到 ViewModel 中
- 取消屏蔽窗口
这是更新的方法:
itemClicked: function (item, record, index, event, eOpts){
// create the window
var detailsWindow = Ext.widget('sessiondetails');
// Get an instance of the model class
var model = Registration.model.SessionDetails;
// manually load the record
// Note that this would be the same as firing
// Registraton.model.SessionDetails.load(...),
// I just think it reads better this way
model.load(record.id, {
// Make sure you include the `scope: this` config,
// otherwise you won't have access to the
// previously defined `detailswindow` variable
scope: this,
success: function (session){
var viewmodel = detailsWindow.getViewModel();
viewmodel.setData(session.getData());
// Since this callback fires after the data is returned,
// placing the unmask here makes sure the data is loaded first.
detailsWindow.unmask();
}
});
// Show and mask the window
detailsWindow.show();
detailsWindow.mask('Loading...');
}