monda
monda

Reputation: 3915

Call helpers functions from other template related functions

From Template.myTemplate.rendered function (or from other template functions), I want to call other util function. Not sure how to do it in Meteor way.

I tried

 Template.myTemplate.rendered = function(){
   console.log("chat Interface rendered");
   Template.myTemplate.__helpers.get('someFunction');
 };


 Template.myTemplate.helpers({
   'isEditable': function () {
     return Session.get('editable');
   },
   'someFunction':function () {
     console.log("someFunctionis called");
     //More stuff here

   }
});

This did not work as expected. Is there any standard way of doing it?

Upvotes: 1

Views: 126

Answers (2)

Mozfet
Mozfet

Reputation: 369

You can create a meteor method and use it everywhere in your code. You can also call methods from other methods.

Meteor.methods({
  someMethod: function (param) {
    //do stuff
  }
});

var result = Meteor.call('someMethod', param);

Upvotes: 0

Matthias A. Eckhart
Matthias A. Eckhart

Reputation: 5156

In my opinion, you are misusing template helpers. In general, they are used to get data into templates and not to control UI elements.

As a result, I recommend to create a regular JavaScript function and subsequently call it inside your onRendered callback:

function disableChatBtn() {
  console.log("disableChatBtn is called");
  $('#btn-chat').prop('disabled', true);
}

Please note: Template.myTemplate.rendered is deprecated in Meteor version 1.0.4.2 (and later), use Template.myTemplate.onRendered instead.

For example:

Template.myTemplate.onRendered(function() {
  console.log("chat Interface rendered");
  disableChatBtn();
});

Upvotes: 2

Related Questions