Automatic fetching data with name-convention


#1

I am building a smarthone app to learn ember. A user got households which got rooms and so on. When I click on a user I want to show his households and after that I want to click on a household to show all rooms in that household. My Router.js looks like this:

Router.map(function() {
  this.route('about');

  this.route('users', function() { //Lists all the users, URL looks like /users
    this.route('single', { path: '/:user_id' }, function() { //Actually i did not use this route
      this.route('households', function() { ;// Shows all households a user with user_id has, URL looks like /users/123/households
        this.route('single', { path: '/:household_id' }, function() { // Shows a single household, URL looks like /users/123/households/456
          this.route('rooms', function() { // Shows all rooms a household with household_id has, URL looks like /users/123/households/456/rooms
            this.route('single', { path: '/:room_id' }, function() {
              this.route('devices', function() {});
            });
          });
        });
      });
    });
  });
});

If I call {{#link-to ‘users.single.households’ user.id}} the route ‘households’ is called. This is fine. In this route i need access to the user.id. This is working with the following statement in the household-route model hook.

var parentModel = this.modelFor(‘users.single’);

complete route:

import Route from '@ember/routing/route';

export default Route.extend({
    model(){
        console.log('household route');
        var parentModel = this.modelFor('users.single');
   
   return Ember.RSVP.hash({
    
            user: parentModel,
    
            household: this.get('store').findAll('household').then(results => results.filter((site) => {
            return site.get('member').filter(x => x == parentModel.id).length > 0;
       })),
          });
    }
});

Now I am stuck with adding new objects to the store cause of the name-convention or automatic data fetch. My child-route users.single is never called. In my understanding this is because of the naming convention user_id. For listing my data from the mirage server this woks fine. But if I want to push a new object to the store, the automatic request for a user_id did not find my new object which is only stored in the store and not stored in the database on my mirage-server. Is there any option to override the default automatic fetch? I want to try to use peekRecord instead of findRecord.

OR

Is it possible to use an other param name like ‘useridnumber’? But when i am doing this:

this.route(‘single’, { path: ‘/:useridnumber’ }, function() {

my route user.single is even never called.


#2

So the param that you set in ‘path’ is arbitrary, has nothing to do with your data model. You could write:

this.route('single', { path: '/:weird_dynamic_segment_param_name' }, function() { 

And it would work fine. The only thing that affects is the param name in the Route, which you use typically in the model hook.

One thing that I will note is that unless you want all of your UI to be nested you probably don’t want to nest all your routes. For example if you are going through this view all the way down to devices and you still want the list of users and then the user and then the rooms and then the room and then the devices shown, nested routes are the way to go. Otherwise if you’re displaying only one list/single at a time I’d recommend flattening your router.

Another thing I notice is that you have two routes named “single”. I’d still expect this to work but it may not. You could try renaming them to ‘user’ and ‘room’ respectively or something like that.

Where are you pushing new users to the store? And do you not want them stored on the backend? If you want to stop async requests on a given relationship you can always do something like user: DS.belongsTo('user', { async: false }), in the model that you don’t want to fetch async relationships.


#3

The only thing that affects is the param name in the Route, which you use typically in the model hook.

This is exactly my point. In my case the param name affects. My model hook in users.single route is clear. There is no findRecord for the given param but ember did a request on his own. When I am searching my hole projekt for “findRecord(‘user’, params.user_id)” there is no single entry.

Furthermore if i will declare a model hook in users.single and i use param user_id it won´t go into it. So how could ember know the specific user if i do this.modelFor(‘users.single’) when it not use the model hook