Ember.js helper not properly recognizing a parameter

点点圈 提交于 2019-12-01 09:33:02

问题


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?


回答1:


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;
});



回答2:


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);
});


来源:https://stackoverflow.com/questions/9261976/ember-js-helper-not-properly-recognizing-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!