5

在下面的“约会”路线中,我可以伸手并获取父“日”的模型

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" });        
        });                                                               
    });                                                                   
});

但是当我深入到新的或编辑的路线中时,我怎样才能(从动作处理程序中)伸手去抓住父“日”模型,就像我在路线中所做的那样?

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

更新

最终的控制器代码现在看起来像这样

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

2 回答 2

4

Toran - 很抱歉添加这个作为额外的答案,我还不能评论 - 是的,它应该免费工作。您可以像这样controllers.postactions块内访问:

var postController = this.get('controllers.post')
于 2013-10-16T17:35:34.520 回答
2

有一种简单的方法可以做到这一点。在AppointmentsEditController添加

needs: "day"

然后您可以通过 访问day控制器this.get('controllers.day')

我总是使用这样的东西:

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

看看这篇文章http://emberjs.com/guides/controllers/dependencies-between-controllers/

我希望这有帮助:)

于 2013-10-16T06:17:07.567 回答