dalgard / meteor-viewmodel

Minimalist VM for Meteor
24 stars 2 forks source link

Shooting params into viewmodel functions #18

Closed wvanooijen92 closed 8 years ago

wvanooijen92 commented 8 years ago

Hi,

First of all, great project!

I have a template named a with the following html:

<div class="col s2 center" {{bind "click: add -1"}}><i class="material-icons">remove</i></div>

In my JS i have:

    Template.a.viewmodel({
        add : function(e,quantity){
            console.log(quantity, parseFloat(quantity[1]));
        }
    }

Why is quantity an array? I expected it to be -1

Thank you!

dalgard commented 8 years ago

Thanks.

The second parameter in your method will be an array of the string arguments appended in the binding expression – and the third parameter will be an object with the keyword arguments passed to the {{bind}} helper.

You can write it like this:

Template.a.viewmodel({
  add(e, args) {
    let quantity = args[0];
    console.log(quantity, parseFloat(quantity));
  }
}

But better yet, pass the number using a keyword argument (either hardcoded or dynamically through a variable):

<div class="col s2 center" {{bind "click: add" quantity=-1}}><i class="material-icons">remove</i></div>
add(e, a, kwargs) {
  console.log(kwargs.quantity);
}
dalgard commented 8 years ago

Please close the issue if your problem has been solved.

wvanooijen92 commented 8 years ago

Yes, ofc. Thanks for answering so quickly!