modelFor returns the params to model instead of the actual model?


#1

This is some really freaky behavior that I probably should have asked about earlier, as this has been endlessly frustrating.

So, my workflow that I’m trying to implement is just super basic CRUD of my models. ( Each of my objects has a few routes: index, new, show, show/edit, show/index)

My particular problem has to do with creating new objects, and redirection to the show path. the show path loads the show/index route. and the show/index route calls modelFor on the show route to get the model.

here is the code that triggers the redirect (everything is perfect until modelFor is called on the show route) lines 47-50:

console.log(path);
console.log(params);

this.get('router').transitionTo(path, params);

the output of the console.logs is

events.show.levels.show
edit-form.js:50 Object {level_id: "57", event_id: "16"}

which is great, exactly what I expect from nested routing (the level_id being the id of the new object, and event_id being the parent model’s id)

the route that is being transitioned to is here which is built from this mixin. All of that works fine, where things get messed up is here in show/index: where I make the call to modelFor('events.show.levels.show'). The result of modelFor in this route is the params that are sent to the show route.

I’ve been super confused by this behavior, and would be extraordinarily relieved, if someone could shed some light as to what’s happening.


#2

this.get('router').transitionTo(path, params); params is an Object… so from Ember’s perspective you just passed it the model to path so it skips the model hook entirely. Which causes modelFor will return that same param object since it’s the current model backing that route.

What you want to do is pass params as individual arguments to transitionTo instead of the whole params object.

Something like this would be valid and not cause the model hook to be skipped: this.transitionTo(routeName, 'nest', 'employees', { queryParams: { page: 1 } });

Hope that clears that up, it’s a common pitfall.


#3

thanks for the reply!

is nest a keyword?


#4

arbitrary string for a param/segment of the URL.

Router.map(function() {
  this.route('employees', '/:name');
});

this.transitionTo('employees', 'microsoft', { queryParams: { foo: 'bar' } });

#5

So, how does this work with nested paths?

say: events/:event_id/levels/:level_id ?

    this.get('router').transitionTo(path, parentId, {
      queryParams: params
    });

makes both ids 16 in my test, which is just the event_id. The level id should be 34, cause params is

{level_id: "34"}

:-\


#6

I finally fought the example in the docs.

transitionTo(path, eventId, levelId)

much easier than what I was doing. yay