简体   繁体   中英

Ember 2.0 handlebars helper is not a function?

Have been looking for a few hours a fix for this without any solution. I am trying to create a custom Ember handlebar helper using:

Ember.Handlebars.helper('highlight', function(value, options) {
    var escaped = Handlebars.Utils.escapeExpression(value);
    return new Ember.Handlebars.SafeString('<span class="highlight">' + escaped + '</span>');
});

But for some reason I get Uncaught TypeError: Ember.Handlebars.helper is not a function

I read that in Ember 2 they have a new approach:

// app/helpers/full-name.js
import Ember from "ember";

export default Ember.Helper.helper(function(params, hash) {
  return params.join(' ');
});

But this is if you are using ember-cli, how about to register helper without ember-cli?

In version 2 they removed all Ember.Handlebars interface calls.

Use the new helper:

Ember.Helper.helper(function(params) {

});

or extend the Ember.Helper

Ember.Helper.extend({
  // This service name is only an example
  compute(params, hash) {
    return this.get('nameBuilder').build(params, hash.title);
  },
  rebuildName: Ember.observer('nameBuilder.isAnonymized', function() {
    this.recompute();
  })
});

Ideally you should also use ember-cli, it's easier, as i'm not sure how you compile and get everything running without it (it's a really good build system and you can make it work with any backend service you want using

ember serve --proxy

Ember Cli

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM