我有一组帖子作为数据:
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/用于工作示例。
关于javascript - 更新集合中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26694939/