Encountered Error:
The main key for my user model is username. The primary key for my routes is the routename. When my API returns JSONs, they are nested inside data:{} following jsonapi.org specifications. However, this structure differs from what js-data requires, as it expects the id attribute to be at the top level. To handle this discrepancy, I have been returning data.data in the afterFind function for 'users'. I attempted a similar approach for 'routes' but encountered challenges due to it being an array of routes. The console log output in beforeInject shows:
Configuration Details:
DS.defineResource({
name: 'users',
idAttribute: 'username',
basePath: apiEndpoint,
relations: {
hasMany: {
routes: {
localField: 'routes',
foreignKey: 'username'
}
}
},
// custom setting for this resource
afterFind: function(resource, data, cb) {
// implementing specific logic for "users"
cb(null, data.data);
}
});
DS.defineResource({
name: 'routes',
idAttribute: 'routename',
basePath: apiEndpoint,
cacheResponse: true,
relations: {
belongsTo: {
users: {
parent: true,
localKey: 'username',
localField: 'users'
}
}
},
beforeInject: function(resource, data) {
// executing additional tasks specific to "users"
console.log(data);
return data.data.routes;
}
});
Issue Arises During Attempt to Load Routes:
resolve: {
user: function($route, DS) {
var username = $route.current.params.username;
return DS.find('users', username).then(function(user) {
DS.loadRelations('users', user.username, ['routes']).then(function(user) {
console.log(user);
}, function(err) {
console.log(err);
});
});
}
}