在我的Ember控制器中,每当配置发生变化时,我都会使用延迟加载来获取其他数据。因此,我想延迟绑定的执行,直到加载所有依赖数据。我认为返回一个假值,因为setter结果将阻止绑定执行但仍然执行绑定。如何延迟绑定执行直到加载完成?
App.ConfigController = Ember.ArrayController.extend({
currentConfiguration: null,
configuration: function(key, value){
if(arguments.length === 1){
return this.get('currentConfiguration');
}
var self = this;
// load additional data
this.loadData(id, function(data){
self.set('currentConfiguration', value);
return value;
})
}
return false;
}.property('currentConfiguration'),
loadData: function(id, resultHandler, faultHandler){
var self = this;
var uri = 'http://foo.com/+id;
$.getJSON(uri)
.success(function(data, status, jqXHR){
resultHandler(data);
})
.error(faultHandler);
},
});
答案 0 :(得分:1)
无法根据需要阻止绑定执行。我接近这个的方法是有两个独立的属性;设置一个,并观察它以更新第二个,并绑定到第二个。像这样:
App.ConfigController = Ember.ArrayController.extend({
configuration: null, // <- set this property
configurationDidChange: function(){
var self = this;
// load additional data
this.loadData(id, function(data){
self.set('configurationData', value);
});
}.observes('configuration'),
configurationData: null <- bind to this property
loadData: function(id, resultHandler, faultHandler){
// ... as you have it
}
});