What is best practice for creating new records between transitions?


#1

The scenario is a user clicks a button on a template which takes them to another route/template.

For clarity, let’s call the page with the button the index route/template, and let’s call the page that the user is sent to after clicking the button the edit route/template.

Before presenting the edit route/template, there should be a new record created that will be the model of the edit route/template.

Would it best practice to:

  1. implement the logic of creating a new data record and redirect in the index controller; or
  2. implement all the logic in the edit route handler?

#2

Since all the logic is specific to the Route you are redirecting the user to the most appropriate place to handle it would be in the route itself. You could even just return this.store.createRecord('model') as the model.

I generally just assume a user can pop into any page (once logged in) using a bookmark. So if its not generated at runtime or can be revisited by refreshing the page then all information required to work that page should be gathered in the route itself.


#3

I do it in the parent route (index, in this case). My edit routes have the same buttonology as modals. In fact, we can flop back and forth in UX style by wrapping the template in a modal. In the edit’s cancel button, we dispose of a new record.


#4

Thanks! The idea is to create it only once if the model doesn’t exist. If the model does exist, then load it.

This said, I’m thinking now it might make more sense to check if the model exists first. If it does, then use a link-to that has the model’s ID in the URL.

If the model doesn’t exist, then the link-to would create the record onClick, and then redirect to the URL with the model’s ID in it.

Does that sound like a sensible approach?


#5

Thanks! I feel like this might be the right approach as well, and I think I outlined it my reply to @James-Byrne above. Take a look if you have the time. I’d appreciate the feedback. :slight_smile: