本文介绍了编写一个产生绑定结果的助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个日期/时间格式化助手,但是当基础属性发生变化时,它生成的内容不会更新.这并不奇怪,但有谁知道如何在 helper 中生成绑定?
I have a date/time formatting helper but what it produces does not update when the underlying property changes. This is not a surprise, but does anyone know how to produce bindings in helpers?
我像这样调用助手...
I invoke the helper like this...
{{timestamp created_at}}
...这里是助手本身:
...and here is the helper itself:
Handlebars.registerHelper('timestamp', function(context, options) {
var formatter = options.hash['format'] ? options.hash['format'] : 'hh:mm a MM-DD-YYYY';
var original_date = Ember.getPath(this, context); // same as this.get(context) ?
var parsed_date = moment(original_date);
var formatted_date = parsed_date.format(formatter);
return new Handlebars.SafeString("<time datetime=" + original_date +">" + formatted_date + "</time>");
});
推荐答案
现在可以使用公共 Ember API 创建绑定的 Handlebars 助手.
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>");
});
传递给helper的参数已经被解析了,只要路径改变就会再次调用helper.
The parameter passed to the helper will have already been resolved, and the helper will be called again whenever the path changes.
这篇关于编写一个产生绑定结果的助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!