0
votes

In every example I see, we always use data like if we knew what was the next ID. I'm building a web application that multiple users will be using at the same time. When a user click on the button "Create a category", I create a new record, send it to my API that saves it in the DB. Unfortunately Ember has no idea of my new Category ID. Let's say he made a typo. He click on "Modify". The app has no idea of the ID and therefor, cannot complete the route :product_id/edit.

I see two solution :

  1. One thing I tried, that would potentially fix my problem is returning the ID in a the header. So I would send my data, return Status 201 (Created) if everything went well, then get the content of the Header and assign the created ID. Looks good on paper, but I have no idea how to get the content of my header with Ember.

    App.CategoriesAddRoute = Ember.Route.extend({
        setupController: function(controller) {
            controller.newRecord(); 
        },
        actions: {
           save: function() {
                controller = this.controllerFor("category"); 
                // Here I'd like to use something like .then( getHeaderContent('id') ) 
                // and assign that value to the category                
                controller.get('content').save() 
                this.transitionTo('categories.index');
            }
        },
    });
    
  2. Reload my categories data with a new query. I would like to avoid that but if I have no other choice I'll go for this.

1

1 Answers

1
votes

It's standard practice on save of a new model (POST) to return the model back from the server with the new id. After the server returns the model Ember Data will update the model so the model client side should have the id.