Creating a model based on an existing one in a Route's `model`


#1

I’m trying to create a route for creating a new model based on an existing model with a cloneVolumeFrom method which is called by overriding the Route’s model. Unfortunately, since the basis model isn’t loaded immediately, it’s data isn’t accessible in the router yet. I’m using ember-model for the data layer, but I think there’s a generic solution to this sort of need that I haven’t figured out yet. Is there a pattern for doing this, or am I approaching this the wrong way?

Thanks, Louis


#2

Sorry to double post, but I think I may have solved it (or at least made myself some more subtle errors). By using the fetch method, I can then use the returned promise and return after acting on the promise’s data.


#3

Well, I’ve gotten myself into a mess trying to handle the difference between a URL load and a transition via linkTo. I think I have to handle it something like the following in the route:

Library.BooksCloneVolumeRoute = Ember.Route.extend
  model: (params)->
    Library.Book.cloneVolumeFrom(params.book_id)

  setupController: (controller, model)->
    controller.set 'model', Library.Book.cloneVolumeFrom(model.get('id'))

Unfortunately, the promise doesn’t seem to resolve properly since in the Ember Inspector, the model is an [object Object] with an attribute fulfillmentValue: Library.Book:ember739. Is there a recommended way to handle this?


#4

This seems to work:

Library.BooksCloneVolumeRoute = Ember.Route.extend
  setupController: (controller, model)->
    Library.Book.cloneVolumeFrom(model.get('id')).then (model)->
      controller.set('model', model)

This feels a little weird, because while the model is properly set when entering via URL as well as transitionTo, the initial model hook is loading the original basis model rather than the new derived model. I don’t think this is a deal-breaker, but I feel like the beforeModel hook may be a better way to do this, I just can’t figure out how.