Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a parent model within a nested index route using ember.js?

Tags:

ember.js

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

like image 880
Toran Billups Avatar asked Feb 05 '13 13:02

Toran Billups


2 Answers

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?

like image 180
Mike Grassotti Avatar answered Sep 17 '22 22:09

Mike Grassotti


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}} 
like image 43
Emad Avatar answered Sep 18 '22 22:09

Emad