How to use "needs" with nested routes / controllers in emberjs RC 2

I have a very simple route setup, which allows me to first show "all" entries for an object. Then, if the user selects a drop-down list, they can filter it down using the date.

I recently upgraded to RC2 and realized that the "needs" have replaced or will soon replace controllerFor.

I am curious how I can use “needs” in the situation below, when I need a nested / internal route for “records.date” to change the content for the parent “record” route when choosing a date.

What is missing below is that inside App.RecordsDateRoute I need to change the contents of the recording controller so that it becomes a new filter (by date this time), and all that seems to me is just resetting the steering wheel template and not showing anything - even when I try to use something simple like

this.controllerFor("records").set('content', App.Record.find(new Date(model.loaded))) 

from setupController method to ReportDateRoute

 App.Router.map(function(match) { return this.resource("records", { path: "/" }, function() { return this.route("date", { path: "/:date_loaded" }); }); }); App.RecordsController = Ember.ArrayController.extend({ selected: 0, dates: Ember.computed(function() { return App.Date.find(); }).property() }); App.RecordsIndexRoute = Ember.Route.extend({ model: function() { this.controllerFor("records").set("selected", 0); return App.Record.find(); } }); App.RecordsDateRoute = Ember.Route.extend({ model: function(params) { //the controllerFor below seems to be working great ... but what about needs? this.controllerFor("records").set("selected", params.date_loaded); return App.Date.create({ loaded: params.date_loaded }); } }); 
+4
source share
2 answers

With rc2, instances of other controllers can be retrieved via "controller.controllerName", in your case it will be this.get('controllers.records') .

The "needs" declaration makes the link to the controller by sorting the import a reference to another controller; in your case, the date controller will be:

 App.RecordsDateRoute = Ember.Route.extend({ needs: ['records'], model: function(params) { this.get("controllers.records").set("selected", params.date_loaded); return App.Date.create({ loaded: params.date_loaded }); } }); 

Regarding App.Record.find(new Date(model.loaded)) find () expects an identifier or object whose keys and values ​​will be used to filter the collection of models, but you give it a Javascript date.

You mean App.Record.find(new App.Date(model.loaded)) , or maybe something like App.Record.find({ loaded: model.loaded }) /* assuming it already a Date */ ?

The specified route also has an initController(controller, model) method, perhaps you could use this instead of "overloading" the model () method with too many responsibilities. http://emberjs.com/api/classes/Ember.Route.html#method_setupController

+3
source

I recently upgraded to RC2 and realized that the "needs" have replaced or will soon replace controllerFor.

To access another controller from route hooks, you must continue to use controllerFor . Controller.needs intended for communication between controllers; it now replaces the obsolete use of the controllerFor method on controllers. AFAIK there is no plan for discounting controllerFor on ember Routes.

I'm curious how I can use “needs” in the situation below, when I need a nested / internal route for “records.date” to change the content for the parent “record” route when choosing a date.

For this use case, it is best to stick to controllerFor. Thus, you can use the needs by indicating that App.RecordsDateController needs = ['records'] you can access the record controller.get('controllers.records') through controller.get('controllers.records') from your setupController route.

What is missing below is that inside App.RecordsDateRoute I need to change the contents of the recording controller so that it becomes a new filter (by date this time), and all that seems to me is just resetting the steering wheel template and not showing anything - even when I try to use something simple, for example this.controllerFor("records").set('content', App.Record.find(new Date(model.loaded))) , in the setupController method RecordsDateRoute

 App.RecordsDateRoute = Ember.Route.extend({ model: function(params) { return App.Date.create({ loaded: params.date_loaded }); }, setupController: function(controller, model) { var recordsController = this.controllerFor("records"); // Moved this from model hook, since here you are 'setting up a controller' recordsController.set("selected", model.date_loaded); // Set query based on current route model var query = { loaded: model.loaded }; recordsController.set("content", App.Record.find(query)); } }); 
+2
source

All Articles