user3475602
user3475602

Reputation: 1217

Call Meteor helper function when adding elements to DOM

I want to call a UI.registerHelper('getTitle', function (type){}); function when adding an element with jQuery's .append($('<h3/>', {'class': 'panel', html: "{{getTitle 'game'}}"}). The output is: {{getTitle 'game'}}, so it's not interpreted. How can I do this?

Any help would be greatly appreciated.

Upvotes: 1

Views: 263

Answers (1)

saimeunt
saimeunt

Reputation: 22696

You can't proceed this way, because {{getTitle "game"}} is not HTML, it's actually Spacebars syntax and it cannot be dynamically interpreted like this.

What you could do is declare the getTitle as a separate function :

function getTitle(type){
  //
}
Template.registerHelper("getTitle",getTitle);

And use it in your DOM insertion code :

var html=getTitle("game");
// ... proceed with insertion via jQuery

Most important, adding HTML via jQuery doesn't sound very meteorish so I advise you to reconsider your design and switch to a template based approach :

<template name="whatever">
  {{#id conditionMet}}
    <h3>{{getTitle "game"}}</h3>
  {{/if}}
</template>

Template.whatever.helpers({
  conditionMet:function(){
    // return some value based on a reactive data source
  }
});

Upvotes: 1

Related Questions