hofan41
hofan41

Reputation: 1468

Update Variable in Mustache Ractive Template

I am trying to get Ractive templates to go through a loop and compare the last accessed value to the current value.

My attempt at this was to create a helper function that updates a "lastValue" variable with the value the template loop has encountered.

You can see my jsfiddle here:

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;
        }
    }
});

Upvotes: 0

Views: 569

Answers (1)

martypdx
martypdx

Reputation: 3712

What about:

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

Upvotes: 2

Related Questions