I am trying to use a custom helper in my Ember project. So I have done
bower install --save handlebars
and then under helpers\\compare.js, I have
import Handlebars from 'handlebars';
Handlebars.registerHelper('compare', function (lvalue, rvalue, options) {
}
Is this the correct way to import Handlebars js and use it inside helper ?
I am now getting:
Assertion Failed: Helpers may not be used in the block form, for example {{#my-helper}}{{/my-helper}}. Please use a component, or alternatively use the helper in combination with a built-in Ember helper, for example {{#if (my-helper)}}{{/if}}.
I want to use the following
{{#compare model.someCount 0 operator = ">"}}
UPDATE Here's a JSBin showing how you would use a component to accomplish this instead of a helper, since block helpers aren't really supported in generated helpers:http://emberjs.jsbin.com/puminocuva/edit?html,js,console,output . Keep in mind components need to be named with a -
in them, which is why I named it my-compare
and not just compare
The docs are pretty straightforward on creating helpers: http://guides.emberjs.com/v1.13.0/templates/writing-helpers/
If you're using Ember CLI 1.13.x with Ember 1.13, I'm not sure how that file above was generated. You should use ember g helper compare
in your Ember CLI console to create files.
If you find yourself needing to make comparisons in your view, then I would think you'd want your logic to be in the controller and just generate a computed property such as:
// some/controller.js
import Ember from 'ember';
const { computed } = Ember;
export default Ember.Controller.extend({
// (...)
countGreaterThanTen: computed('model.count', ()=> {
return this.get('model.count') > 10;
}),
// (...)
});
and use {{#if countGreaterThanTen}} stuff here {{/if}}
in your template rather than perform complex logic in there via helpers.
Update Also, your error is because you're trying to use a helper with an opening block (that's what the #
is). When using your own helper in a template, you would just use {{compare}}
and not {{#compare}}
.
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.