Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 859
Ramon Tayag Avatar asked Jun 15 '13 12:06

Ramon Tayag


People also ask

What is model in Ember JS?

Model is a class that extends the functionality of the Ember Data. When a user refreshes the page, the contents of page should be represented by a model. In Ember. js, every route has an associated model. The model helps to improve the performance of application.

What is setupController in Ember?

setupController: function(controller, model) { controller. set('model', model); } }); The setupController hook receives the route handler's associated controller as its first argument. In this case, the PostRoute 's setupController receives the application's instance of App. PostController .


1 Answers

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.

like image 99
Luke Melia Avatar answered Sep 18 '22 13:09

Luke Melia