更新 Mustache Ractive 模板中的变量

Update Variable in Mustache Ractive Template

我正在尝试让 Ractive 模板通过循环并将上次访问的值与当前值进行比较。

我的尝试是创建一个辅助函数,用模板循环遇到的值更新 "lastValue" 变量。

你可以在这里看到我的 jsfiddle:

http://jsfiddle.net/k6hj6q46/3/

<script id='template' type='text/ractive'>
    <ul>
     {{#each names}}
        <li>value: {{lastValue}}</li>
        <li>{{name}}</li>
        {{update(name)}}
     {{/each}}
   </ul>
</script>
<div id='container'></div>



var ractive = new Ractive({
    // The `el` option can be a node, an ID, or a CSS selector.
    el: '#container',

    // We could pass in a string, but for the sake of convenience
    // we're passing the ID of the <script> tag above.
    template: '#template',

    // Here, we're passing in some initial data
    data: {
        lastValue: 'oldValue',
        names: [{
            name: 'value1'
        }, {
            name: 'value2'
        }],
        update: function (newValue) {
            console.log(newValue);
            this.lastValue = newValue;
        }
    }
});

怎么样:

{{#each names:i}}
    <li>last value: {{names[i-1]}}
    <li>current value: {{this}}
{{/each}}