【发布时间】:2016-01-26 20:46:57
【问题描述】:
我有一个组件,它在滑动时会向上发送一个动作到父路由/控制器来处理一些 ajax 功能。该组件有一些 UI 设置为加载状态,以向用户显示正在发生的事情。当 working 设置回 false 时,UI 中的加载动画会停止,用户交互可能会再次发生。
callAjaxAction() {
this.setProperties({working:true});
Ember.RSVP.cast(this.attrs.action()).finally(() => {
this.$('.draggable').animate({
left: 0
});
this.setProperties({working:false});
});
}
在这种情况下,控制器捕获组件定义上指定的操作并调用 ajax 函数来获取一些数据以显示在页面中
// in the controller action
return Ember.RSVP.Promise((resolve,reject) => {
Ember.$.ajax({
type: 'get',
dataType: 'json',
url: `http://***/api/paysources/user/697?key=${ENV.APP.api_key}`
}).then((response)=>{
this.setProperties({
'cards':response.user_paysources,
'showCards': true
});
},(reason)=>{
reject();
this.get('devlog').whisper(reason);
})
})
这将显示一种新的弹出式组件类型,允许用户选择一张卡进行支付。如果用户点击离开,或者如果他们点击一张卡片并且 ajax 操作完成,我不仅需要在此页面上重置 UI(更改它以显示购物车已付款),还需要发送滑动组件(一个现在有一个加载动画)告诉它加载完成的东西。
基本上,按照我的想法,有没有办法从父控制器/路由对组件触发操作?
【问题讨论】:
-
Nitpick,你在
new Ember.RSVP.Promise(…中缺少new
标签: javascript ajax ember.js