如何在Handlebars中将消息记录到控制台?

时间:2013-06-10 15:49:16

标签: ember.js handlebars.js

我正在使用Ember.js并想知道Handlebars中是否有内置函数允许我像{{ log "my message" }}一样打印到控制台,类似于我们目前对日志对象的处理方式:{ {1}}?


或者我必须define my helper function every time

但这甚至对我不起作用(click for jsbin):

我有HTML Handlebars:

{{ log this }}

然后在app.js中我有:

{{ debug "this is my string" }}

但是app.js没有收到Ember.Handlebars.helper('debug', function(the_string){ console.log(the_string); }); ,所以the_string在那里未定义,发生了什么?

3 个答案:

答案 0 :(得分:9)

我不确定为什么Ember.Handlebars.helper不起作用......截至目前你可以试试

  Ember.Handlebars.registerHelper('debug', function(the_string){
    Ember.Logger.log(the_string);
    // or simply
    console.log(the_string);
  });

答案 1 :(得分:3)

只为未来发现此问题的人发布新答案。现在有一种更简单的方法。

您的{{debug}}帮助程序与本机{{log}}帮助程序有效内置。您还可以使用{{debugger}}帮助程序添加断点。

有关详细信息,请参阅the guides

答案 2 :(得分:0)

在您的应用中的某处声明此内容:

hbs.registerHelper("log", function(data){
  console.log(data)
});

用法:     {{log data}}

相关问题