Meteor: How to use object names including dashes in Spacebars

半世苍凉 提交于 2019-12-12 22:08:33

问题


Didn't find the answer anywhere, but maybe one of you knows it.

I'm getting back data from http.call('GET'), I can use the data correctly in Spacebars like

{{anydata.specificdata}}  

but have no chance to use data w/ object names containing dashes like

{{anydata.specific-data}}

I tried

{{anydata.'specific-data'}}

, but this does not work either. As I'm retrieving a lot of different data I would like to avoid to create helpers for every field containing dashes.

Does anyone know how I could handle something like

{{anydata.specific-data}?

Thanks for any answer that helps.

Have Fun!


回答1:


I found the answer on a meteor form: https://forums.meteor.com/t/dash-character-in-spacebars/2885

I ran into a similar problem. I had a json object with a dash in a the variable name. In order to call an object with a dash, the following is done:

{{some.json.object.[with-a-dash]}}

Note the . before the open bracket and no quotes (single or double) around the dash named item.




回答2:


on the HTTP success callback map the data object properties from dash to camelcase, then use your new object.




回答3:


Some days offline behind...

I did not find a nice solution (due to the unexpectable combination of arrays and objects in the htpp result) but a working one:

I use EJSON.stringify() to stringify the http-result and replace the dashes in the object keys using a regular expression, then use EJSON.parse() to make it an object again. Done.

Not very elegant but working fine and fast.

Have Fun!



来源:https://stackoverflow.com/questions/29524753/meteor-how-to-use-object-names-including-dashes-in-spacebars

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