Best practice for a route which creates a new instance of a model?


#1

Let’s say I have a route someEntity.new which has the model hook:

model: function() {
    return this.store.createRecord("someEntity");
},

Now from some action I use this.transitionToRoute("someEntity.new"), which hits the model hook on the route, which creates a new instance of some-entity which I can can then edit on the rendered view.

This works fine the first time I transition to the route, but if I try to transition to the route again it does not perform a full transition, so the model hook is not hit and a new instance of some-entity is not created.

Am I going about this the correct way? And if so, is there any way that I could force a full transition when I redirect to someEntity.new?

I think I read something about all transitions being full transitions by default when 1.12 hits with component routing, or was this only related to query parameters?

edit: I think this can also be boiled down to “which of the following is the ember-way?”:

  • Creating and then saving a model immediately, then sending it to a route responsible for editing (like someEntity.edit).
  • Creating an entity in memory (on someEntity.new) without persisting immediately, and only saving after editing the model.

#2

You should do this inside your setupController hook:

setupController: function (controller) {
	controller.set("model", this.store.createRecord("someEntity"));
}

#3

setupController is not being hit after calling transitionToRoute the second time.

Imagine a page constructed as follows:

  • A someEntity resource, with a someEntity.new child route (which renders into an outlet on the someEntity view).
  • The someEntity view shows the list of someEntity models that exist in the store.
  • The someEntity view has a button “Create” which fires the action createSomeEntity on the someEntity controller:
export default Ember.Controller.extend({
    actions: {
        createSomeEntity: function() {
            this.transitionToRoute("script.new");
        }
    }
});

The first time “Create” is clicked the transition is fine. It hits the model hook, then setupController, and if I redirect away from someEntity.new it hits resetController.

If I do not navigate away from someEntity.new, and click the “Create” button on someEntity (since someEntity.new is rendered into an outlet inside of someEntity), the model hook and setupController are never hit on the someEntity.new route (I assume this is because the URL doesn’t change). So basically I want to force a full transition to the someEntity.new route when the createSomeEntity action is used.

From what I understand, in 1.12, routable components will always perform full transitions. So I guess I could wait for the 1.12 beta.

Unlike today’s model hook, attributes gets called every time your route is entered, whether or not transitionTo was passed a preexisting model. This eliminates the need for beforeModel and afterModel, because you can implement either behavior from within your own attributes or model functions. Therefore, beforeModel and afterModel are deprecated.

This also means we deprecate refresh, because routes essentially always refresh.

https://github.com/ef4/rfcs/blob/routeable-components/active/0000-routeable-components.md