admin管理员组文章数量:1326299
I'm generating a handlebars view with hbs for express js framework, and I need to access the variables I pass to the view from inside a separate JavaScript file.
For example:
var foo = {{user.name}}
Obviously this code throws an error, but what is the preferred way of getting hbs variables inside JavaScript?
Currently I'm running another ajax request to get the same data. but I think it is not wise to make another request for the same data...
I'm generating a handlebars view with hbs for express js framework, and I need to access the variables I pass to the view from inside a separate JavaScript file.
For example:
var foo = {{user.name}}
Obviously this code throws an error, but what is the preferred way of getting hbs variables inside JavaScript?
Currently I'm running another ajax request to get the same data. but I think it is not wise to make another request for the same data...
Share Improve this question edited Mar 20, 2015 at 19:31 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Mar 20, 2015 at 19:12 Dima GrossmanDima Grossman 2,8302 gold badges22 silver badges27 bronze badges 1- I've edited your question with updates to the grammar and capitalization of words (especially I, which should be capitalized in the Engliish language). I'm afraid I know very little about handlebars or express, so I can't answer the question. – Heretic Monkey Commented Mar 20, 2015 at 19:31
1 Answer
Reset to default 9Found the solution, seems like i just had to put the handlebars variable inside a string for it to work.
In order to pass and Object to a variable I created hbs helper:
hbs.registerHelper('json', function(context) {
return JSON.stringify(context);
});
and the in my view:
var currentUser = JSON.parse('{{{json user}}}');
and now i can access the data on the client side js.
本文标签: javascriptAccessing handlebars variable inside client js fileStack Overflow
版权声明:本文标题:javascript - Accessing handlebars variable inside client js file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206601a2432931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论