问题
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