I’ve been playing with Ember, Ember Data, and Handlebars for a few days now, and it has been extremely frustrating. It seems there’s a lot of magic going on, and not a lot of info or examples that I can wrap my head around, so I’m turning to these forums.
The first question I have is in regards to understanding how data is loaded. I have a route like this:
Neutrinoapp.Router.map(function() {
this.resource('sensorgroups', { path: '/' });
});
The documentation led me to believe that this means loading the web root will trigger the handlebars template named ‘sensorgroups’:
<script type="text/x-handlebars" data-template-name="sensorgroups">
{{#each sensorgroup in model}}
...
{{/each}}
{{#each sensor in model}}
...
{{/each}}
</script>
This is true, however, if this data-template-name doesn’t also match the pluralized name of a model, no REST api call is made to fetch data. In the above example, the ‘each’ on sensorgroup works, but no data is in the second ‘each’. I’ve extended routes for all of my models, and if I change data-template-name to ‘sensors’, it loads those and the second ‘each’ works but not the first. It seems there is some magic going on here with naming, and after playing awhile I can’t figure out how to get it to request anything from my API other than the one matching the data-template-name, so I stick to the hb template name of ‘sensorgroups’, even though I’d prefer to name it ‘home’ or something similar.
The documentation also indiciates that it will auto-load any necessary model data upon request, but this doesn’t seem to be happening when I call ‘#each sensor in model’, for example.
This has led me down a path where I’m sideloading all of my API data with the /api/sensorgroup resource, essentially making my API a single URL. Related to this, the ‘sensorgroup’ model refers to multiple other models via a hasMany relationship, and the dependencies aren’t resolved unless I sideload them, again making my API return everything from just one URL. I would hope Ember would be smart enough to realize that this model requires other models and fetch that data if necessary, but I’m not seeing it.
I really kind of detest the sideloading paradigm, I understand that it is a bit more efficient, but I feel like it clutters the API by returning things that weren’t asked for, so if I can get away from it I’d prefer that.
A second question, it seems like you should be able to do an arbitrary each loop with handlebars, something like:
{{#each thing in ["a","b","c"]}}
However, I’ve tried the above, I tried formally defining a javascript variable and using that, and can’t get anything of the sort to work. I keep seeing references to ‘using this context’ in the documentation, with no examples about how it applies the context to the helper. It appears with Ember that the context is coming from model, meaning that I have to go to the trouble of storing something in the model in order to do a simple loop like this, even though this array is not really api data but layout-specific details that are easily handled in a loop. I did get it to work finally, but I had to write a transform for ‘array of strings’, and then put that in a model. That’s a little heavy for what should be something simple. I looked at writing my own helper, but I’d still have to have context to work on.
Thanks in advance for any tips.