vendredi 5 février 2016

Accessing nested route parameter from parent route/template

I have a nested route that behaves similarly to a less-nested version of itself:

/code/class/MyFile.java
/code/coverage/:coverageId/class/MyFile.java

Somewhat self-explanatory, but the coverage route decorates the page with some additional highlighting/details that are not present in the "normal" version.

However, my code template defines a sidebar that includes a navigation tree to access other code files. When viewing a /coverage/ page, I would like for the sidebar to display the links differently, ensuring that the nested route is maintained when clicking around.

My Problem:

I can't seem to find a way to access the :coverageId in the code route or template to modify the way the links are displayed. My code route:

export default Ember.Route.extend({
  model(params) {
    console.log('Coverage ID: ' + params.coverageId);
      ...

Prints undefined. This seems to make sense since it's a child parameter, which is "out of scope"?

My Incorrect Solution:

I tried this in my class route:

export default Ember.Route.extend({
  model(params) {
    var parentModel = this.modelFor('code');
    parentModel.coverageId = params.coverageId;
    ...

Which, in my code.hbs template:

{{#if model.coverageId}}
  {{#link-to 'code.coverage.class' model.coverageId fileName}}{{fileName}}
  {{/link-to}}
{{else}}
  {{#link-to 'code.class' fileName}}{{fileName}}{{/link-to}}
{{/if}}

Actually works! The link is produced properly when viewing the coverage route.

However: While I can right-click any of these links to open them in a separate tab successfully, if I just regularly click them, I receive an error:

Error: Assertion Failed: You must use Ember.set() to set the `coverageId` property (of [object Object]) to `425988`

But of course, I can't use parentModel.set('coverageId', params.coverageId);, because it fails with:

TypeError: parentModel.set is not a function

So! How can I either (1) examine the nested route parameters in the parent (code) route, or (2) properly inject the known value (:coverageId) from a child route into the parent?




Aucun commentaire:

Enregistrer un commentaire