I’m new to Ember, and especially Ember Data, so forgive me if this is obvious.
I would like to display some basic live stats in my app such as activeUsers, usersOnline, etc. What’s the best way to represent these stats on the API and fetch them with Ember data?
I’d prefer to represent them all within a single object accessible via GET /statistics
, then just grab that object as the model like:
model: function(){
return this.store.find('statistics');
}
with this to tell ember that statistics is uncountable:
var inflector = Ember.Inflector.inflector;
inflector.uncountable('statistics');
But, Ember now tells me I need to use findAll
(because there’s no unique identifier for the stats data object).
So I represent the data on the server like this:
{
"statistics": [
{
"activeManagers": 4207,
"activeTeams": 4840
}
]
}
And change the model hook to this:
model: function(){
return this.store.findAll('statistics');
}
Ember complains that there needs to be an ID on each item, so I add an arbitrary id: 1
on the object:
{
"statistics": [
{
"id": 1,
"activeManagers": 4207,
"activeTeams": 4840
}
]
}
Now I get back a list of items (albeit only 1 item long), but I can’t work out how to get hold of the first item either in the model / ember data promise hook, or in the template.
I could always change the API to respond from GET /statistics/1
, and just use find
instead of findAll
. But that seems weird since there’s no other records with any other ids, so the whole /1
thing doesn’t feel right. Now I’m just starting to doubt my entire existence and feel like there’s got to be a more elegant way to do something that should be so simple.
So here I am, asking/begging for help from the Ember gurus out there. Am I doing this all wrong?