Meteor Cheatsheet Templates Events

meteor-cheat-sheet

// Meteor - Templates - Blaze - Events:

// To register an event, in our JavaScript file:

Template.loginLayout.events({
  "click .something": function(e, tpl) {

  },

  'focusout input.textContent, keydown input.textContent, keyup input.textContent': function(e, tpl) {
    if (e.type === "keydown" && e.which === 27) {
      // Esc was hit
    } else if (e.type === "keyup" && e.which === 13 || e.type === 'focusout') {
      // Enter key was hit or input was unclicked (blur)
    }
  }
});

Template.moduleSubmit.events({
  'submit form': function(e) {
    e.preventDefault();
    var module = {
      title: $(e.target).find('[name=title]').val(),
      body: $(e.target).find('#body').val(),
      tags: $(e.target).find('select.chosen-select').val()
    };

    if (module.tags === null) {
      module.tags = [];
    }

    Meteor.call('moduleInsert', module, function(error, result) {
      if (error) {
        alert(error.reason);
      }
      if (result.moduleExists) {
        alert('This title has already been posted: ' + result._id);
      }

      Router.go('modulesList', {
        username: Meteor.user().username
      });
    });
  }
});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License