更改组件的属性不会触发其他组件上的“更改”
问题描述:
我在同一层次结构(同级)中有两个横向组件。我将模型传递给他们两个。我正在更改组件中的模型数据,并希望捕获来自另一个组件的所有更改,但组件的“更改”事件仅触发用于更改值的组件。它在其他组件中不起作用。我怎样才能做到这一点?我只是想通过这两个组件进行沟通。更改组件的属性不会触发其他组件上的“更改”
应用程序/路由/ applications.js
应用程序/模板/ applications.hbs:
{{applications-list rejectFormData=model.rejectFormData}}
{{reject-dialog rejectFormData=model.rejectFormData}}
应用/组件/拒绝-dialog.js
export default Ember.Component.extend({
change() {
// this fires only when changed through this component
// I want this to fire even when changed from other components
alert('changed!');
},
actions: {
rejectApplication(formData) {
this.set('formData.application_id', 123);
}
}
});
应用/components/applications-list.js
export default Ember.Component.extend({
actions: {
rejectApplication (application) {
this.set('rejectFormData.application_id', application.Application.id);
}
}
});
答
你不能做你想做的。行动不会以兄弟姐妹的方式冒泡。您可以在一个中更改数据并查看其他数据中的数据更改。
如果您想在数据更改时执行逻辑操作,则需要使用观察器。
app/components/reject-dialog.js
export default Ember.Component.extend({
changeObserver: Ember.observer('rejectFormData.application_id', function(){
//do logic
}),
change() {
// this fires only when changed through this component
// I want this to fire even when changed from other components
alert('changed!');
},
actions: {
rejectApplication(formData) {
this.set('formData.application_id', 123);
}
}
});
尝试做'Ember.set(formData,'application_id',123)',而不是'应用程序列表'。另外,我不知道组件中有一个'change()'钩子,你在哪里读过它? – locks