38

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

Toran Billups
  • 27,111
  • 40
  • 155
  • 268

2 Answers2

43

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?

Community
  • 1
  • 1
Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
3

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}}
Emad
  • 4,110
  • 5
  • 30
  • 35
  • where are you storing your data (if you are not using the ember-data store)? do you have a home grown identity map or something equivalent ? – Toran Billups Sep 09 '14 at 15:56
  • I created my own store using the technique expalined here http://eviltrout.com/2013/03/23/ember-without-data.html – Emad Sep 09 '14 at 19:28