I have the following route structure
App.Router.map(function(match) { this.route("days", { path: "/" }); this.resource("day", { path: "/:day_id" }, function() { this.resource("appointment", { path: "/appointment" }, function() { this.route("edit", { path: "/edit" }); }); }); });
When I'm inside the AppointmentIndexRoute I'm looking for a way to create a new model using some meta day from the day (parent) model but because the day model does not yet know about this appointment I'm unsure how to associate them until the appointment is created / and the commit is fired off.
Any help would be much appreciated
From within the AppointmentIndexRoute
's model hook you can use modelFor('day') to access the parent model. For example:
App.AppointmentIndexRoute = Ember.Route.extend({ model: function(params) { day = this.modelFor("day"); ... } });
Another example is here: emberjs 1.0.0pre4 how do you pass a context object to a resource "...Index" route?
What if I am not using ember data? How do I get the parent id in a route like
this.resource('workspace',function () { this.resource('workflow', {path: '/:workspace_id/workflow'}, function () { this.route('show', {path: '/:workflow_id'}); }); });
This code will not work:
App.WorkflowShowRoute = Em.Route.extend({ model: function(params) { var ws = this.modelFor('workspace'); //ws is undefined return this.store.find('workflow', params.id, ws.id); } });
EDIT: I found a workaround, it's not ideal but works exactly the way I want it.
this.resource('workspace',function () { this.route('new'); this.route('show', {path: '/:workspace_id'}); //workflow routes this.resource('workflow', {path: '/'}, function () { this.route('new', {path:'/:workspace_id/workflow/new'}); this.route('show', {path: '/:workspace_id/workflow/:workflow_id'}); }); });
And in my workflow route, I can access the workspace_id jus as I expect from the params property:
App.WorkflowShowRoute = Em.Route.extend({ model: function(params) { return this.store.find('workflow', params.workflow_id, params.workspace_id); } });
Finally, here is my link-to inside the workspace.show route helper:
{{#each workflow in workflows}} <li> {{#link-to 'workflow.show' this.id workflow.id}}{{workflow.name}}{{/link-to}} </li> {{/each}}
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