Ember: how do you access the model from the router?

Ramon Tayag picture Ramon Tayag · Jun 15, 2013 · Viewed 19.7k times · Source

Based on what I've read (please correct me if I'm mistaken), the logic that handles when a model should be saved and where to transition next should be in the router.

If that is the case, I'm running into a bit of a problem: I don't know how to access the model from the route.

This is my controller (and the console logs "CREATED" after I press submit):

App.ScoutsNewController = Ember.ObjectController.extend
  submit: ->
    model = @get('model')
    model.on 'didCreate', ->
      console.log 'CREATED' # I want to  redirect to the index after creation
    model.save()

I should move that logic into the route, right? Let's try that:

App.ScoutsNewRoute = Ember.Route.extend
  model: ->
    App.Scout.createRecord()

  events:
    submit: ->
      # Based on what I've read, the right place to put the code you see in the controller is here. How do I get access to the model?
      # I have tried @get('model'), @get('content')

Note: I understand that the submit event bubbles up from the view, to the controller, then finally the route, stopping at any one of them that has "submit" defined. So since I want the route to handle it, I removed the controller. I'm able to see any console.log done in the route, I just need to be able to get to the model instance.

I'm using Ember v1.0.0-rc.5-7-g610589a

Thanks!

Answer

Luke Melia picture Luke Melia · Jun 15, 2013

Two options: this.currentModel or this.modelFor(routeName)

Update

I spoke to Señor Alex Matchneer about this. There are no plans for this.currentModel to go away anytime soon, but he considers this.modelFor(this.routeName) the public API.