dimanche 17 décembre 2017

ember automatic fetch by name convention

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 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 surprised 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 I want to understand the way it works.

I think it have to be possible to change user_id to ‘useridnumber’. But when I am doing this:

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

my route user.single is even never called and my data listing is not working.

I really can´t figure out why my route user.single is never called.




Aucun commentaire:

Enregistrer un commentaire