Search code examples
slim-langractivejs

How can I use Ractive's on-tap attribute with Slim templates?


In my Slim template, I have:

th class=="sortable {{ sortColumn === 'name' ? 'sorted' : '' }}" on-tap=="sort:name" Name

This renders:

<th class="sortable sorted">Name</th>

The sort method is not being called when the item is clicked.

Should the on-tap attribute work in this case or does it need a different syntax? I have tried Slim's splat attribute:

th class=="sortable {{ sortColumn === 'name' ? 'sorted' : '' }}" *({:on-tap => ["sort:name"]})

but Slim complains when it hits on-tap.

Any help is appreciated.


Solution

  • EDITED: sorry, I misunderstood first time around. Based on this:

    The sort method is not being called when the item is clicked.

    I suspect you're mixing up method calls versus events.

    If you want to call a method on the ractive instance, the syntax is:

    on-tap='sort("name")'
    

    And would be handled via:

    new Ractive({
       sort: function(name){
    
       }
    });
    

    If you want to raise an event, you would use the syntax you have:

    on-tap='sort:name'
    

    And handle it via:

    new Ractive({
       oninit: function(){
           this.on('sort', function(event, name){
    
           });
       }
    });
    

    See http://jsfiddle.net/yccknhh5/.