ember-prefetch icon indicating copy to clipboard operation
ember-prefetch copied to clipboard

In prefetch, transition.params is not fully populated when a parent model was provided to the transition

Open joshwiseman opened this issue 8 years ago • 4 comments

Let's say you have a router setup like the following...

Router.map(function() {
  this.resource('students', function() {
    // This middle part of the URL is an ID that represents a student.
    this.resource('student', { path: ':student_id' }, function() {
      this.route('milestones');
    });
  });
});

Now, in your milestones route, you use prefetch as follows:

  prefetch(params, transition) {
    let studentId = transition.params.student.student_id;

    // return some data fetch that depends on studentId
  }

When the page loads from scratch, eg. at a URL like mysite.com/students/1234/milestones, then everything works fine. It also works fine when doing a manual transition and passing in the student ID, like this:

this.transitionTo('student.milestones', studentId);

But when we initiate the transition and pass the student model (in certain cases we already have it):

this.transitionTo('student.milestones', student);

... then we run into a problem in the milestones prefetch hook. Specifically, transition.params.student isn't populated, so our data fetch doesn't work. We've had to work around this by doing the following in prefetch:

let studentId = transition.params.student.student_id || transition.intent.contexts[0].id;

... which obviously is very fragile and relies on internal Ember structures.

So seems like a bug that we can't always get the student ID from transition.params.

joshwiseman avatar Jun 17 '16 19:06 joshwiseman

Have you tried using this.paramsFor('student').student_id to retrieve the student_id parameter? I'm not familiar with accessing other routes' dynamic segments via the transition object, but I have a feeling it's not public API.

nickiaconis avatar Sep 26 '16 22:09 nickiaconis

Good idea, but unfortunately that doesn't work either. this.paramsFor('student') is empty in prefetch() when the transition was passed a student object.

joshwiseman avatar Sep 26 '16 23:09 joshwiseman

I wonder if the serialize method is run before or after the prefetch hook. This is definitely something I haven't looking into before, so might need some change to support.

nickiaconis avatar Sep 26 '16 23:09 nickiaconis

I believe (in my example) the student route's serialize is run before the prefetch hook. But somehow the URL parameter returned in that serialize call is not making it to the prefetch arguments.

joshwiseman avatar Sep 27 '16 00:09 joshwiseman