我这样打电话给帮手:
metric.timestamp已经填好了,我正在记录它。
{{#each..
...
{{#each.. itemController='..'}}
...
{{#each metric in ... itemController='metric'}}
{{log metric.timestamp}} //2014-09-16T14:35:02Z"
{{#ifless12hours this date=metric.timestamp}}
然后,当我尝试从参数中读取它时,它是""。它可能是一个问题 上下文
Ember.Handlebars.registerHelper('ifless12hours', function(date,options) {
Ember.Logger.log(date) // "" ??????
var nowDate = new Date();
var nowDateMil = nowDate.getTime();
var dateAux = new Date(date);
var dateMil = dateAux.getTime();
if (Math.abs(dateMil-nowDateMil)<= 43200000){
return options.fn(this);
}
else{
return options.inverse(this);
}
});
答案 0 :(得分:0)
我不确定你为什么要将this
传递给你的助手。但是命名选项不作为参数传递。它们将在options.hash
如果您像这样创建帮助器:
Ember.Handlebars.registerHelper('ifless12hours', function() {
var options = [].slice.call(arguments, -1)[0];
var context = (options.contexts && options.contexts[0]) || this;
var date = options.hash.date;
function dateCompare(date){
var nowDate = new Date();
var nowDateMil = nowDate.getTime();
var dateAux = new Date(date);
var dateMil = dateAux.getTime();
return Math.abs(dateMil-nowDateMil)<= 43200000;
}
return Ember.Handlebars.bind.call(context, date, options, true, dateCompare);
});
您应该可以在模板中使用它,如:
{{#ifless12hours date=metric.timestamp}}
YUP
{{else}}
NOPE
{{/ifless12hours}}
在此处使用bin:http://emberjs.jsbin.com/siyuvi/2/edit
此帮助程序基于此处的代码:http://discuss.emberjs.com/t/a-way-to-let-users-define-custom-made-bound-if-statements/2583/3