Javascript 使用EventAggregator更新Aurelia中的列表
我正在使用app contacts demo学习Aurelia,是的,我知道,@Eisenberg提到它不完整,但后来我想在保存联系人或创建新联系人时,使用EventAggregator通知app.js。到目前为止,一切都很顺利。我可以在app.js中接收联系人对象,但现在我想更新联系人列表,它不起作用,当我在app.js中保存联系人并更新联系人列表时,它会被删除 在app.js中添加的代码和subscribe方法在构造函数中调用Javascript 使用EventAggregator更新Aurelia中的列表,javascript,aurelia,Javascript,Aurelia,我正在使用app contacts demo学习Aurelia,是的,我知道,@Eisenberg提到它不完整,但后来我想在保存联系人或创建新联系人时,使用EventAggregator通知app.js。到目前为止,一切都很顺利。我可以在app.js中接收联系人对象,但现在我想更新联系人列表,它不起作用,当我在app.js中保存联系人并更新联系人列表时,它会被删除 在app.js中添加的代码和subscribe方法在构造函数中调用 subscribe(){ this.ea.subscri
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);
}
});
}
未对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>
Eric L.Anderson在他的博客文章中很好地解释了这一点: . 这与您尝试执行的代码类型相同
注意:现在回答Kishore的问题可能已经太晚了,但我正在为其他搜索到此的用户添加链接。您应该回答自己的问题,并将其标记为“已添加”。)祝您好运!
subscribe(){
this.ea.subscribe('contact_event', payload => {
return this.api.getContactList().then(contacts => {
this.contacts = contacts;
});
});
}