2
votes

I'm trying to get used to Ember.js and Handlebars, but one problem is puzzling me. I'm probably just missing something, but has been on it for quite a while and could not find anything wrong.

I have the simple template bellow:

<header>

    <h2><a href="#" class="link-box-title">{{project.pid}}-{{projectWindowTitle project}}</a></h2>

</header>

the first {{project.pid}} correctly outputs the project.pid value, and I wanted to pass the project object to the helper function bellow:

Handlebars.registerHelper('projectWindowTitle', function(proj) {

    var info = proj.pid;
    return info;

});

I'm overly simplifying the helper, but the result is always the same, the helper does't return anything:

<a href="#" class="link-box-title"><script id="metamorph-9-start" type="text/x-placeholder"></script>S2S<script id="metamorph-9-end" type="text/x-placeholder"></script>-</a>

What am I doing wrong?

2

2 Answers

5
votes

when using handlebars in ember.js, the helper signature is a little bit different than with "plain" handlebars. the main difference is that the argument is not "resolved" before the helper is called.

for your example, proj is "project", so you have to get the value of "project" from the view:

Handlebars.registerHelper('projectWindowTitle', function(property, options) {
    var project = Ember.getPath(this, property);
    var info = project.get("pid");
    return info;
});
1
votes

I know that question already has accepted answer but .. that's not the correct approach :)

As described on ember docs, the proper way of creating handlebars helpers for ember is a bit different than what you would do in handlebars.

Something along the lines of:

Ember.Handlebars.helper('projectWindowTitle', function(value) {
  var escaped = Handlebars.Utils.escapeExpression(value.pid);
  return new Handlebars.SafeString(escaped);
});