Search code examples
ractivejs

Call one function from another


I have a custom function according to documentation. What documentation doesn't cover (or I'm blind) is how to call one function from another. Here is what I tried:

var myRactive = new Ractive({
    el: '#element',
    template: '#template',
    data: {
        function1: function() {
            return 100;
        },
        function2: function() {
            return function1() / 2;
        }
    }
});

It doesn't work. I tried to change it to myRactive.function1(), myRactive.data.function1(), etc. but neither of them worked out.


Solution

  • You can use this.get('function1') to get it from your data object, then run it as a function like this.

    var myRactive = new Ractive({
    el: 'body',
    template: '#ele',
    data: {
        function1: function() {
            return 100;
        },
        function2: function() {
            return this.get('function1')() / 2;
        }
    }
    });
    

    Working fiddle: http://jsfiddle.net/gs3yhhsz/1/

    In pre 0.7 ractive, you could access the data-object directly (this.data). This got changed, and you should now use this.get() and this.set() instead.