繁体   English   中英

更新集合中的值

[英]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/

** http://docs.ractivejs.org/latest/magic-mode

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM