Kis*_*mar 5 javascript aurelia aurelia-event-aggregator
我正在使用app-contacts演示来学习Aurelia,是的我知道,它是不完整的,如@Eisenberg所提到的,但后来当我保存联系人或创建新联系人时,我想使用EventAggregator通知app.js.直到现在一切正常.我能够在app.js中收到联系人对象,但现在我想更新联系人列表,这不起作用,当我保存联系人并更新app.js中的联系人列表时,它将被删除.
在app.js中添加的代码和在构造函数中调用subscribe方法.
subscribe(){
this.ea.subscribe('contact_event', payload => {
console.log(payload);
var instance = JSON.parse(JSON.stringify(payload));
let found = this.contacts.filter(x => x.id == payload.id)[0];
if(found){
let index = this.contacts.indexOf(found);
this.contacts[index] = instance;
}else{
instance.id = this.contacts.length + 1;
this.contacts.push(instance);
}
});
}
Run Code Online (Sandbox Code Playgroud)
没有对app.html进行任何更改
<li repeat.for="contact of contacts" class="list-group-item ${contact.id === $parent.selectedId ? 'active' : ''}">
<a href="#" click.delegate="$parent.select(contact)">
<h4 class="list-group-item-heading">${contact.firstName} ${contact.lastName}</h4>
<p class="list-group-item-text">${contact.email}</p>
</a>
</li>
Run Code Online (Sandbox Code Playgroud)
如何更新列表?
更新 这对我有用,但不确定什么是正确的方法
subscribe(){
this.ea.subscribe('contact_event', payload => {
return this.api.getContactList().then(contacts => {
this.contacts = contacts;
});
});
}
Run Code Online (Sandbox Code Playgroud)
Eric L. Anderson 在他的博客文章: Aurelia 的事件聚合器中很好地解释了其工作原理 。这与您尝试执行的代码类型相同!
注意:现在回答 Kishore 的问题可能已经太晚了,但我正在为其他通过搜索最终到达这里的人提供链接。