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

丶灬走出姿态 提交于 2019-12-03 19:56:25

问题


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 methods

  • Ember.Handlebars.helper
  • Ember.Handlebars.registerHelper
  • Ember.Handlebars.registerBoundHelper
  • Handlebars.registerHelper

That's too much for me. Could you explain it down to earth what's the difference?


回答1:


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.




回答2:


  • Ember.Handlebars.helper should be used by to register helpers, other methods are just limited versions of it. Ember.Handlebars.helper renders HTML with bindings that keep views and models in sync. Functions, Views and Components can be supplied as helper definitions.

  • Ember.Handlebars.registerBoundHelper is just like Ember.Handlebars.helper, but you can supply only a Function as a helper definition.

  • Ember.Handlebars.registerHelper previously delegated to Handlebars.registerHelper. Ember.Handlebars.registerHelper no longer exists, but Handlebars.registerHelper is still used internally by Ember to create all the helpers after the bindings are set up.



来源:https://stackoverflow.com/questions/18004510/whats-the-difference-between-handlebars-helpers-and-ember-handlebars-helpers

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