【发布时间】:2016-01-15 23:33:09
【问题描述】:
由于某种原因,我在操作中创建的 RSVP 承诺不再为我工作,我不知道什么时候出错了,在哪里让它停止工作。
我有一个发送动作并等待响应的组件
// COMPONENT IN ITEM ROUTE
callAjaxAction() {
this.setProperties({working:true});
Ember.RSVP.cast(this.attrs.action()).finally(() => {
Ember.$('.draggable').animate({
left: 0
});
this.setProperties({working:false});
});
}
组件的这个特定实例在控制器中调用这个动作
// IN ITEM CONTROLLER
placeBid() {
return new Ember.RSVP.Promise((resolve,reject) => {
if(this.get('winning')) {
this.get('notification')._confirm({message: "You are currently the highest bidder, would you like to continue with your bid?",isConfirm: true}).then(()=>{
console.log('confirmed');
this.send('placeBidActual').then(()=>{
resolve();
}).catch(()=>{
reject();
});
return true;
}).catch(()=>{
resolve();
console.log('denied');
return false;
});
} else {
setTimeout(()=>{
this.send('placeBidActual').then(()=>{
resolve();
}).catch(()=>{
reject();
});
},500);
}
});
}
如果用户已经赢得该项目,此操作正在调用服务上的确认方法并等待用户点击“是”。否则我只是立即调用实际的 ajax 操作,该操作看起来像这样
// IN ITEM CONTROLLER
placeBidActual() {
return new Ember.RSVP.Promise((resolve,reject) => {
Ember.$.ajax({
...
}).then((response)=>{
(do some stuff with the response)
resolve();
}, (reason)=>{
(do something with rejection reason)
reject(reason);
});
});
}
在控制台中出现错误
Uncaught TypeError: Cannot read property 'then' of undefined
在声明this.send('placeBidActual')的那一行
更新:
这可能是对预期流程的更好解释。
用户尝试出价,用户将一个组件滑过以表明他们希望出价。此时的 UI 会显示一个加载指示器,并在重置 UI 之前等待 ajax 操作完成。 如果用户还不是该项目的最高出价者,它将直接进入 ajax 操作,并在完成后指示组件重置 UI。 但是,如果用户是该项目的最高出价者,它应该显示确认消息(使用我设置的通知服务)并等待用户确认或拒绝。如果他们否认它只是取消并表示组件重置 UI。如果用户确认,则调用 ajax 操作并在完成后指示组件重置 UI。
【问题讨论】: