【发布时间】:2015-03-12 06:23:27
【问题描述】:
如果 promise 发生异常,我想返回默认值。我返回的数组似乎无法正常工作。
我的路线模型挂钩
model: function() {
return {
menuItems : this.store.find('menuItem').catch(function(error) {
Ember.Logger.error('Retrieving Menu Items', error);
return [];
}),
我的控制器模式
selectFirstMenuItem: function() {
var model = this.get('model');
//At this point model.menuItems is some weird object
//with an _id : 26, _label : undefined, _result: Array(0), _state : 1 etc.
//(maybe a Promise?), the .get method throws an undefined exception.
if(model.menuItems.get('length') > 0 ) {
...
}
}.observes('model.menuItems.@each'),
【问题讨论】:
-
我不确定,但在这种情况下可能会触发拒绝 (...then(func(){}, func(){.. denied ...})?
-
@EntspAndi 我不关注。您是否建议在 .catch() 之后使用 .then()?
-
@EntspAndi 我想我现在明白你的意思了。 .then() 接受两个函数,成功和失败。我试过了,但得到了相同的结果。从失败钩子返回时。