Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a parent routes model from inside controller action?

Tags:

ember.js

Inside my route for "appointments" below I can reach up and get the model for the parent "day"

App.Router.map(function(match) {                                          
    this.resource("day", { path: "/day/:day" }, function() {              
        this.resource("appointments", { path: "/appointments" }, function() {
            this.route("new", { path: "/:appointment_start/new" });
            this.route("edit", { path: "/:appointment_id/edit" });        
        });                                                               
    });                                                                   
});

But when I'm deep inside the new or edit routes, how can I reach up (from within the actions handler) and grab the parent "day" model like I did in the route?

App.AppointmentsEditController = Ember.Controller.extend({                
    actions: {
        updateAppointment: function(appointment) {
            var day = this.get('??');
        }
    }
});

Update

The final controller code now looks like this

App.AppointmentsEditController = Ember.Controller.extend({       
    needs: 'day',         
    actions: {
        updateAppointment: function(appointment) {
            var day = this.get('controllers.day');
        }
    }
});
like image 370
Toran Billups Avatar asked Oct 16 '13 04:10

Toran Billups


People also ask

How do I use attribute routing on the controller?

To make attribute routing less repetitive, route attributes on the controller are combined with route attributes on the individual actions. Any route templates defined on the controller are prepended to route templates on the actions. Placing a route attribute on the controller makes all actions in the controller use attribute routing.

What is the use of mapcontrollerroute inside useendpoints?

Inside the call to UseEndpoints, MapControllerRoute is used to create a single route. The single route is named default route. Most apps with controllers and views use a route template similar to the default route.

How to create a single route using mapcontrollerroute?

Inside the call to UseEndpoints, MapControllerRoute is used to create a single route. The single route is named default route. Most apps with controllers and views use a route template similar to the default route. REST APIs should use attribute routing.

How to send a message to the parent component using routerlink?

The routerLink is set to the parent component route. Just to add up, queryParams directive is used to send a message to the parent component via the query string. In the parent component file the query parameter can be accessed as follows: Within the message variable the parameter is received and the message s stored.


2 Answers

Toran - sorry to add this as an extra answer, I can't comment yet - yes, it should work for free. You can access controllers.post from within the actions block like this:

var postController = this.get('controllers.post')
like image 111
sjmog Avatar answered Nov 15 '22 07:11

sjmog


There is simple way to do it. In AppointmentsEditController add

needs: "day"

Then you can access to day controller via this.get('controllers.day').

I always use something like this:

App.CommentsControler = Ember.Controller.extend({
    needs: "post",
    postBinding: "controllers.post",
    ...
    postName: function() {
        return this.post.name;
    }.property("post.name")
})  

Take a look of this article http://emberjs.com/guides/controllers/dependencies-between-controllers/

I hope this help :)

like image 32
Artur Małecki Avatar answered Nov 15 '22 09:11

Artur Małecki