[英]Update value in collection
I have a collection of posts as data: 我收集了一些帖子作为数据:
var ractive = new Ractive({
el: '#templateContainer',
template: '#template',
data: {
Posts: [{"Id":"posts/97", Content="Blog post test", Date="Something"}, ...];
}
});
At some point I receive a notification that the blog post content has changed: 在某个时候,我收到有关博客文章内容已更改的通知:
funcion onBlogPostContentChanged(postId, newContent) {
ractive.set(..., newContent);
}
The problem is that I don't know how to specify ractive.set
so that the content is changed for a blog post with a specific Id. 问题是我不知道如何指定
ractive.set
以便更改具有特定ID的博客文章的内容。
You can set the keypath via the array index and the property: 您可以通过数组索引和属性设置键路径:
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
}
See http://jsfiddle.net/pj6myzch/ for working example. 有关工作示例,请参见http://jsfiddle.net/pj6myzch/ 。
** http://docs.ractivejs.org/latest/magic-mode ** http://docs.ractivejs.org/latest/magic-mode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.