33

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!

Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73
Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69

5 Answers5

78

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.

ndequeker
  • 7,932
  • 7
  • 61
  • 93
Luke Melia
  • 8,389
  • 33
  • 41
  • Thank you! Looks like I will end up getting currentModel. How did you know about that? Can't seem to find it in the docs. – Ramon Tayag Jun 16 '13 at 02:02
  • I've worked in that part of the codebase a bunch. Will you consider opening a PR to add it to the docs? It's not hard to do, and I can coach you if you need help. – Luke Melia Jun 17 '13 at 20:07
  • Sure. Cloning the website now! – Ramon Tayag Jun 20 '13 at 11:31
  • Should I really use `this.currentModel` though? The lack of documentation plus the fact that it is only set in a private method https://github.com/emberjs/ember.js/blob/v1.0.0/packages/ember-routing/lib/system/route.js#L555 makes me think it's not meant for me to use. Always use this.modelFor(routeName) instead? – davekaro Sep 06 '13 at 14:11
  • `this.currentModel` is perfect, but I too would like to see an authoritative documentation source on it. – Patrick M Mar 24 '14 at 21:35
  • 1
    I spoke to Señor Alex Matchneer about this. No plans for `this.currentModel` to go away anytime soon, but he considers `this.modelFor(this.routeName)` the public API. – Luke Melia Mar 25 '14 at 01:38
2

what should work is

this.controllerFor('ScoutsNew').get('content')
peterfromearth
  • 269
  • 2
  • 8
  • oh, then i am sorry. Can you give me a link where this stands. – peterfromearth Jun 15 '13 at 15:30
  • In the official API, where it should be? If not there... http://emberjs.com/api/classes/Ember.Route.html – Wojciech Bednarski Jun 15 '13 at 17:07
  • sorry, but i don't get it. http://emberjs.com/api/classes/Ember.Route.html#method_controllerFor isn't marked as deprecated. – peterfromearth Jun 15 '13 at 17:48
  • 5
    content is deprecated, but model is correct: `this.controllerFor('ScoutsNew').get('model')` – Michael Johnston Nov 13 '13 at 07:53
  • @MichaelJohnston `content` is not deprecated. Also `model` is an alias to `content`. See the API for ObjectController (http://emberjs.com/api/classes/Ember.ObjectController.html#property_content) and for ArrayController(http://emberjs.com/api/classes/Ember.ArrayController.html#property_content) – knownasilya Jun 26 '14 at 14:39
2

this.currentModel isn't really the approved way as described here

but in my version of Ember (1.11) this.modelFor(this.routeName) returns null, so this is what worked for me

this.controllerFor(this.routeName).get('model')
Glenn Lawrence
  • 2,844
  • 1
  • 32
  • 38
0

You could also use this.controller.get('model'); but there are plans to remove the controller.

Till that we can use the above code to retrieve the routes current model

Vinoth Kumar
  • 1,347
  • 1
  • 14
  • 23
0

With Ember 3.0.0 this is a documented way that works for me:

const model = this.controller.model;
hlidka
  • 2,086
  • 1
  • 15
  • 14