Hi there,
I am having a very hard time working with some ember data models. I am working with ember app kit as well as ember-data-shim 1.0.0-beta.3. Here are my models:
var Route = DS.Model.extend({
name: DS.attr(),
description: DS.attr(),
creation: DS.attr(),
modified: DS.attr(),
active: DS.attr(),
adhoc: DS.attr(),
routeUsers: DS.hasMany('user'),
userapprovals: DS.hasMany('userapproval')
});
export default Route;
var UserApproval = DS.Model.extend({
route: DS.belongsTo('route'),
user: DS.belongsTo('user'),
amount: DS.attr(),
approval: DS.attr(),
comment: DS.attr(),
notification: DS.hasMany('notification', {embedded: 'always'}),
denyRedirect: DS.belongsTo('user')
});
export default UserApproval;
var User = DS.Model.extend({
firstname: DS.attr('string'),
lastname: DS.attr('string'),
username: DS.attr('string'),
email: DS.attr('string'),
password: DS.attr('string'),
fullName: function(){
return this.get('firstname') + ' ' + this.get('lastname');
}.property('firstname', 'lastname')
});
export default User;
Here are my serializers:
var ApplicationSerializer = DS.ActiveModelSerializer.extend({
normalize: function (type, hash, property) {
// normalize the `_id`
var json = {
id: hash._id
};
delete hash._id;
// normalize the underscored properties
for (var prop in hash) {
json[prop.camelize()] = hash[prop];
}
// delegate to any type-specific normalizations
return this._super(type, json, property);
},
});
export default ApplicationSerializer;
import ApplicationSerializer from 'appkit/serializers/application';
var RouteSerializer = ApplicationSerializer.extend({
attrs: {
routeUsers: {
embedded: 'always'
},
userapprovals: {
embedded: 'always',
ids: true
}
}
});
export default RouteSerializer;
import ApplicationSerializer from 'appkit/serializers/application';
var UserApprovalSerializer = ApplicationSerializer.extend({
attrs:{
notification: {
embedded: 'always'
}
}
});
export default UserApprovalSerializer;
I can save models just fine to mongodb, but when the model is sent back my embedded records disappear. Here is my JSON response:
{
"route": {
"name": "4",
"description": "123",
"adhoc": true,
"_id": "527822dee56a06dc2a000002",
"__v": 0,
"userapprovals": [
{
"amount": "22",
"route_id": null,
"user_id": "5277ca37b900b14c0e000003",
"deny_redirect_id": "5277ca25b900b14c0e000002",
"id": "f0ced142-aee1-4abb-f36a-56ae0088141b"
}
],
"route_users": [
{
"firstname": "first",
"lastname": "last",
"username": "flast",
"email": "flast@xyz.com",
"password": null,
"id": "5277ca37b900b14c0e000003"
}
]
}
}
I guess what I am trying to figure out is what is wrong with my JSON response to cause the embedding relationships to disappear? I have also tried overriding the extractSingle in my RoutesSerializer with the following:
extractSingle: function(store, type, payload, id, requestType) {
var userApprovals = payload.route.userapprovals,
approvalIds = userApprovals.mapProperty('id');
payload.userapprovals = userApprovals;
payload.route.userapprovals = approvalIds;
return this._super.apply(this, arguments);
}
But when I override that method I get a error stating You must include an id
in a hash passed to push
. When I remove the extractSingle override I get no error’s but my model does not contain the embedded records. I’ve been fiddling with it all day and I think it might be time to get another set of eyes to look at it. Please let me know if I need to provide anything else. Thanks for the help in advance.