I want to know how to create a resource which has nested path in the rest api.
for example given the end point for employee resource, /companies/{company}/employees/{employee}
How can i create a employee record. i created a employee model, but the request is going to the top level /. sending a post request with employee data to /.
should i override pathForType() on the adapter?
If you are talking about ember router paths then this is how I've done it:
App.Router.map(function () {
this.resource('companies', {path: 'companies/:company_id'}, function () {
this.resource('employees', {path: 'employees/:employee_id'});
});
});
Which creates something like: index.html/#/companies/{company}/employees/{employee}
This will create CompaniesRoute and EmployeesRoute nested in that route. This can be nicely viewed in the ember inspector if you've set that up.
Ember will enter CompaniesRoute first and then EmployeesRoute.
See routing documentation here
If you are talking about sending data to a REST API server then one solution is to override buildURL e.g.:
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.extend({
host: "http://whatever",
buildURL: function (type, id) {
var url = [];
url.push(this.urlPrefix());
url.push("companies/" + App.companyID + "/employee/" + App.employeeID );
url.push(this.pathForType(type));
if (id) {
url.push(id);
}
return url.join('/') + ".json";
}
})
});
also see RESTAdapter doco here
Note App.companyID would have to be set before you called buildURL. I would do this in CompanyRoute and set it like this
model: function (params) {
App.set('companyID', params.company_id);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With