Reputation: 3989
I've got the following model (simplified for this question):
App.Manager = DS.Model.extend({
name: DS.attr('string'),
teamMembers: DS.hasMany('App.Employee')
});
When this model is loaded (say with App.Manager.find(1)
), it's coming back from my server with a teamMembers array:
[10, 11, 12]
My view needs data from these team members, so Ember automatically loads them with a findMany()
request, as expected. The problem I'm running into is that employee #11 doesn't exist. The server responds to the findMany()
request with just employees 10 & 12:
{
"employees": [
{
"id": 10,
"name": "John Doe"
},
{
"id": 12,
"name": "Jane Doe"
}
}
But, Ember-Data still seems to keep around an empty (and fulfilled) promise for Employee 11 even though the data for that employee was never returned. So now when my view renders, I get a table with 3 rows (one for each employee), and one of those rows is completely blank (because the record is empty).
Checking the state of the record:
{
isLoaded: true,
isDirty: false,
isSaving: false,
isDeleted: false,
isError: false,
isNew: false,
isValid: true
}
So, I'm not sure how to keep this empty record out of my view without checking if every property I need is empty. Is there a way for the server to respond that tells ember not to fulfill this promise? Is there a way to configure ember to recognize when data is not returned?
Edit: I realize that, ideally, the server wouldn't return an ID for an employee that doesn't exist. But the reality is that sometimes data is unreliable, or poorly maintained. If employee 11 were coming back with inaccurate data, then I would agree that the problem is with the data and/or the service and not with Ember. However, in this case, employee 11 is not returning inaccurate data, it's returning NO data at all. In this case, it seems to me like ember should, at minimum, set a flag (i.e. isValid: false) indicating that the record is empty/invalid/not found, if not just flat out destroy the reference to the empty record altogether.
Edit 2: Here's an Issue on Github
Upvotes: 0
Views: 954
Reputation: 475
It's hard to say whether or not Ember-data should try to provide some magic for this scenario. By including the ID in a relationship you've effectively told ember-data that it exists.
The REST adapter (which request data from the server) is separate from the serializer (which converts the response data to your models), so it would be difficult to know whether or not all of the requested data was actually returned. If you don't mind tightly coupling your adapter to your serializer you may be able to verify that the server gave you everything you were expecting.
Workaround:
Add a verification property to your employee model to use as a kind of pseudo-state. This should default to false or null and every record your API returns must overwrite it. Pretty hackish. Luckily this same concept can be accomplished using an existing property such as the employee's name.
if (App.Employee.find(11).get('name') != null) { dance('thriller'); }
If you consider the concept above as a standard validation requirement you may choose to implement it as such in either the model or serializer.
Upvotes: 1