sooon
sooon

Reputation: 4878

meteor template events functions errors

I have a html with this template:

<template name="entryfield">
    <input type="text" id="name" placeholder="Name" /> <input type="text" id="message" placeholder="Your Message" /><br>
    <input type="button" class="plusbutt" value="+"/><input type="button" class="minusbutt" value="-"/>
</template>

and I want to declare the 2 buttons event:

Template.entryfield.events = {
'click .plusbutt': function() {
      // Submit the form
      var name = document.getElementById('name');
      var mood = document.getElementById('mood');
      var message = document.getElementById('message');

      if(name.value != '' && message.value != ''){

        Messages.insert({
          name: name.value,
          mood: '+',
          message: message.value,
          time: Date.now()
        });

        name.value = '';
        message.value = '';
    }
  }
}

'click .minusbutt': function() {
      // Submit the form
      var name = document.getElementById('name');
      var mood = document.getElementById('mood');
      var message = document.getElementById('message');

      if(name.value != '' && message.value != ''){

        Messages.insert({
          name: name.value,
          mood: '-',
          message: message.value,
          time: Date.now()
        });

        name.value = '';
        message.value = '';
    }
  }

But I have error:

chat.js:58:19: Unexpected token :

Which line 58 is the beginning of the second event declaration. But when I totally take out second event, everything just work. My question is how do people declare more than 1 function in the same template events? I seen it everywhere but I just cannot get this to work. Thanks!

Where can I find reference on the template event? The problem on learning Meteor is their functions is from all over, some from MongoDB, Some from Handlebar, and Some from JavaScripts, which make it rather hard to pick up for beginner.

Upvotes: 0

Views: 90

Answers (1)

saimeunt
saimeunt

Reputation: 22696

Your events declaration syntax is wrong, be careful !

Be sure to check the Meteor docs, everything is detailed : http://docs.meteor.com/#template_events

It should be like this :

Template.entryfield.events({
    "click .plusbutt":function(){
        /* ... */
    },
    "click .minusbutt":function(){
        /* ... */
    }
});

Template.myTemplate.events is a function which takes an object as parameter. Each members of the object should be a key/value pair, the key being the event declaration and the value the corresponding handler.

Upvotes: 1

Related Questions