What's the difference between Handlebars helpers and Ember Handlebars helpers?

后端 未结 2 597
面向向阳花
面向向阳花 2021-01-02 07:01

I can\'t catch up with all those changes done to plain Handlebars and modified Ember Handlebars helpers. If I remember correctly you can register a helper with the following

2条回答
  •  离开以前
    2021-01-02 07:24

    Ember.Handlebars.registerHelper is a basic helper that does not bind the argument string to a property. For instance, consider a hello helper created with registerHelper that just returns a greeting message.

    Ember.Handlebars.registerHelper('hello', function(name) {
      return 'Hello ' + name;
    });
    

    When you use it in a template,

    {{hello name}}
    

    You will get the display text as, Hello name. The value of the name property is not looked up.

    To get the value of the name property into the helper you need, registerBoundHelper. As the name suggests it creates a binding between to the name property. Anytime the name changes the helper is called again to rerender. The implementation is similar,

    Ember.Handlebars.registerBoundHelper('hello', function(name) {
      return 'Hello ' + name;
    });
    

    The Ember.Handlebars.helper is same as registerBoundHelper with some additional checks to autodetect what kind of helper you want.

    The vanilla Handlebars.registerHelper isn't used within Ember. It would create similar helpers for projects not using Ember.

提交回复
热议问题