[英]Update value in collection
我收集了一些帖子作為數據:
var ractive = new Ractive({
el: '#templateContainer',
template: '#template',
data: {
Posts: [{"Id":"posts/97", Content="Blog post test", Date="Something"}, ...];
}
});
在某個時候,我收到有關博客文章內容已更改的通知:
funcion onBlogPostContentChanged(postId, newContent) {
ractive.set(..., newContent);
}
問題是我不知道如何指定ractive.set
以便更改具有特定ID的博客文章的內容。
您可以通過數組索引和屬性設置鍵路徑:
function onItemChanged(id, newContet) {
var posts = r.get('Posts'), index = -1
for(var i = 0; i < posts.length; i++){
if(post[i].Id === id){
index = i
break
}
}
if(index !== -1){
r.set('Posts.' + index + '.Content', newContent)
}
// or if using "magic: true"**
Posts[index].Content = newContent
}
有關工作示例,請參見http://jsfiddle.net/pj6myzch/ 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.