How to make a custom GET request with relation
I am wondering how I can use restmod to perform a custom GET request on a relation model that I have defined below:
angular
.factory('AccountModel', function() {
return restmod.model('/account').mix({
audits: {hasMany: 'AuditModel'}
});
})
.factory('AuditModel', function() {
return restmod.model().mix();
})
That allows me to make requests like so:
AccountModel.find(123).audits.$collection(params) // '/accounts/123/audits?params'
But what I am needing to do is a GET request on the following URL:
/accounts/123/audits/templates
I need to be able to get a $collection of templates, too.
I tried nesting relations like so, but it didn't work out, as the template model was undefined
angular
.factory('AccountModel', function() {
return restmod.model('/account').mix({
audits: {hasMany: 'AuditModel'}
});
})
.factory('AuditModel', function() {
return restmod.model().mix({
templates: {hasMany: 'TemplatesModel'}
});
})
.factory('TemplatesModel', function() {
return restmod.model().mix();
})
Have you seen the hasMany options in the documentation ? below I have copied the snippet from readme. Parameter "path" can be used to alter the relationship link.
HasMany Options The has many relation provides the following options for you to customize its behaviour:
path: will change the relative path used to fetch/create the records. Ex: { hasMany: 'Part', path: 'pieces' } inverseOf: adds a property on the child object that points to the parent. Ex: { hasMany: 'Part', inverseOf: 'bike' }. params: optional query string parameters to be used when fetching collection. Ex: { hasMany: 'Part', params: { foo: 'bar' } }. hooks: you can also define hasMany relation hooks. Check the hooks advanced documentation for more information.
You can do like below:
angular
.factory('AccountModel', function() {
return restmod.model('/account').mix({
audits: {hasMany: 'AuditModel', path: 'audits/templates'}
});
})
.factory('AuditModel', function() {
return restmod.model().mix();
})
Thank you @furqanaziz - I have been able to use path to get what I need.