mercredi 13 janvier 2016

Passing unsaved record to Ember.js route

Inside an application we allow users to create new records, related to an existing record. To achieve this, we use actions something like this:

createUser() {
  var route = this;

  var model = this.store.createRecord('user', {
    client: route.modelFor('client'),
  });

  route.transitionTo('user.update', model);
 },

The user.update route renders a user-form component, using the model that was passed in the transition. The same route is also used to update existing users.

The issue with this approach is as follows; when refreshing the page, the page errors because the route fails to find the respective record when querying the store (at this point, the URL is /users/null/update). Ideally I'd pass the client (or client.id) argument in the URL so that:

  1. The page can be reloaded without issue.
  2. The client associated with the user is set correctly.

How can I achieve this in Ember.js? I know that this can easily be done using nested routes (by nesting the user.update route inside a client route), but this doesn't make sense visually.




Aucun commentaire:

Enregistrer un commentaire