Writing a helper that produces bound results?

六眼飞鱼酱① 提交于 2019-11-28 09:10:53
Yehuda Katz

It is now possible to create bound Handlebars helpers using a public Ember API.

Handlebars.registerBoundHelper('timestamp', function(date, options) {
  var formatter        = options.hash['format'] ? options.hash['format'] : 'hh:mm a MM-DD-YYYY';
  var parsed_date      = moment(date);
  var formatted_date   = parsed_date.format(formatter);

  return new Handlebars.SafeString("<time datetime=" + date +">" + formatted_date + "</time>");
});

The parameter passed to the helper will have already been resolved, and the helper will be called again whenever the path changes.

It is unfortunately more complex than I'd like to create a custom helper with bound content. Here's an example that Peter Wagenet wrote: https://gist.github.com/1563710

I'll be lobbying for this to become easier.

Not sure if this applies to this particular question, but I also created helpers in views and wanted the values to update when data in the Ember.js view changed. The way I solved this problem was to write an observer on the values I wanted changed and used jQuery to update the specific value.

For example (in Coffeescript):

...

attrObserver: Ember.observer(() ->
  $("#attrId").text(this.get("attr"))

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