在我的 Ember.js 应用程序中,我有一个名为 mailing-lists 的 Controller ,其中有一个名为 mailing-lists-select 的组件。邮件列表 Controller 将两个操作传递给组件:showSelectedMailingList 和 mailingListDeleted。
我们期望当在组件上调用这些操作时,这些操作应该冒泡到 Controller 。但是,看起来只有 showSelectedMailingList 操作在组件中调用时才会冒泡,而 mailingListDeleted 操作在我们在组件中调用它时永远不会冒泡。
这两个操作之间的唯一区别是在 destroyRecord 调用之后,在组件中调用 mailingListDeleted 的 sendAction。这是已知问题还是我的代码中存在错误?
使用的版本:
Ember: 1.11.2
Ember Data: 1.0.0-beta.16.1
jQuery: 1.11.3
mailing-lists.js Controller
export default Ember.Controller.extend({
selectedId: null,
actions: {
showSelectedMailingList: function(id) {
//action bubbles up, execution reaches here
this.set('selectedId', id);
},
deleteMailingList: function(record) {
//execution never reaches here (???)
//some code
}
}
});
mailing-lists.hbs 模板
<div>
{{#each model as |mailingList|}}
{{mailing-lists-select-option
mailingList=mailingList
showSelectedMailingList='showSelectedMailingList'
deleteMailingList='deleteMailingList'
}}
{{/each}}
</div>
mailing-lists-select-option.js
export default Ember.Component.extend({
mailingList: null,
actions: {
showSelectedMailingList: function(id) {
//execution reaches here
this.sendAction('showSelectedMailingList', id);
},
deleteMailingList: function(mailingList) {
var dat = this;
mailingList.destroyRecord().then(function() {
//execution reaches here
dat.sendAction('deleteMailingList', mailingList);
});
}
}
});
邮件列表-select-option.hbs
<li><a href="#" {{action 'showSelectedMailingList' mailingList.id}}>Show</a></li>
<li><a href="#" {{action 'deleteMailingList' mailingList}}>Delete</a></li>
最佳答案
由于 destroyRecord() 返回 Promise,您可能会遇到一些错误,导致它失败并且永远不会解析,因此永远不会调用 then() 函数,因此不会调用它的 sendAction() 。 如果不是必须调用 sendAction() 的情况,我在我的应用程序中尝试了类似的操作,它可以从 destroyRecord() 中工作。
关于javascript - Ember组件的sendAction在destroyRecord后不会冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37712106/