如何使用ember.js访问嵌套索引路径中的父模型?

Tor*_*ups 38 ember.js

我有以下路线结构

App.Router.map(function(match) {
    this.route("days", { path: "/" });
    this.resource("day", { path: "/:day_id" }, function() {
        this.resource("appointment", { path: "/appointment" }, function() {
            this.route("edit", { path: "/edit" });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

当我在AppointmentIndexRoute里面时,我正在寻找一种方法来创建一个新的模型,使用日期(父)模型的某一天,但因为日模型还不知道这个约会,我不确定如何关联它们直到创建约会/并且启动提交.

任何帮助将非常感激

Mik*_*tti 43

AppointmentIndexRoute模型钩子中,您可以使用modelFor('day')来访问父模型.例如:

App.AppointmentIndexRoute = Ember.Route.extend({
  model: function(params) {
    day = this.modelFor("day");
    ...
  }
});
Run Code Online (Sandbox Code Playgroud)

另一个例子是:emberjs 1.0.0pre4如何将上下文对象传递给资源"...索引"路由?

  • 请注意,现在(Ember 2.0)你需要使用路径的完整路径,所以它将是`this.modelFor('days.day')` (10认同)