Search code examples
javascriptractivejs

Ractive.js Calling object method from proxy event


I would like to call a method of the specific object from ractive.

on-click="mylist.page(5)"

In this case mylist object with the next method would be set on the ractive object.

It works when I use next method added directly on the ractive object, but not when nested deeper inside


Solution

  • Ractive currently only supports calling one instance method in event handlers:

    on-click='setPage(5)'
    

    Being able to chain calls is under consideration.

    For now, you'd have to route it through a method:

    new Ractive(){
        mylist: {
            page: function(page){
                // do page work
            }
        },
        setPage: function(page) {
            this.mylist.page(page);
        }
    }
    

    If what you really want is to call a method on a data object, add a prototype method to Ractive:

    Ractive.prototype.call = function( obj, method ) {
        var args = [].prototype.slice.call( arguments, 2 );
        obj[method].apply( obj, args );
    }
    

    Then you can use:

    on-click='call( mylist, "page", 5)'