我有一个“盒子”路线/控制器,如下所示;
export default Ember.Controller.extend({
initialized: false,
type: 'P',
status: 'done',
layouts: null,
toggleFltr: null,
gridVals: Ember.computed.alias('model.gridParas'),
gridParas: Ember.computed('myServerPars', function() {
this.set('gridVals.serverParas', this.get('myServerPars'));
this.filterCols();
if (!this.get('initialized')) {
this.toggleProperty('initialized');
} else {
Ember.run.scheduleOnce('afterRender', this, this.refreshBox);
}
return this.get('gridVals');
}),
filterCols: function()
{
this.set('gridVals.layout', this.get('layouts')[this.get('type')]);
},
myServerPars: function() {
// Code to set serverParas
return serverParas;
}.property('type', 'status', 'toggleFltr'),
refreshBox: function(){
// Code to trigger refresh grid
}
});
我的路线看起来像;
export default Ember.Route.extend({
selectedRows: '',
selectedCount: 0,
rawResponse: {},
model: function() {
var compObj = {};
compObj.gridParas = this.get('gridParas');
return compObj;
},
activate: function() {
var self = this;
self.layouts = {};
var someData = {attr1:"I"};
var promise = this.doPost(someData, '/myService1', false); // Sync request (Is there some way I can make this work using "async")
promise.then(function(response) {
// Code to use response & set self.layouts
self.controllerFor(self.routeName).set('layouts', self.layouts);
});
},
gridParas: function() {
var self = this;
var returnObj = {};
returnObj.url = '/myService2';
returnObj.beforeLoadComplete = function(records) {
// Code to use response & set records
return records;
};
return returnObj;
}.property(),
actions: {
}
});
我的模板看起来像
{{my-grid params=this.gridParas elementId='myGrid'}}
我的 doPost 方法如下所示;
doPost: function(postData, requestUrl, isAsync){
requestUrl = this.getURL(requestUrl);
isAsync = (isAsync == undefined) ? true : isAsync;
var promise = new Ember.RSVP.Promise(function(resolve, reject) {
return $.ajax({
// settings
}).success(resolve).error(reject);
});
return promise;
}
鉴于上述设置,我想了解执行的流程/顺序(即针对不同的钩子)。我试图调试,它不断地从一个类跳到另一个类。另外,2个具体问题;
我原以为最初会触发“激活”钩子,但发现事实并非如此。它首先执行“gridParas”钩子,即在“activate”钩子之前。是因为模板中指定了“gridParas”吗?
当我为 /myService1 执行 this.doPost() 时,它必须是“同步”请求,否则执行流程会发生变化,我会收到错误消息。实际上,我希望仅在收到响应后才执行 filterCols() 控制器中的代码,即 this.set('gridVals.layout', this.get('layouts')[this.get('type')])来自 /myService1。但是,到目前为止,我必须使用“同步”请求来执行此操作,否则使用“异步”,执行将移至 filterCols(),并且由于我还没有响应,因此会引发错误。
补充一下,我使用的是 Ember v 2.0