38

我有以下路线结构

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

当我在 AppointmentIndexRoute 中时,我正在寻找一种使用日(父)模型中的某个元日创建新模型的方法,但是因为日模型还不知道这个约会,所以我不确定如何关联它们直到创建约会/并触发提交。

任何帮助将非常感激

4

2 回答 2

43

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

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

另一个示例在这里:emberjs 1.0.0pre4 如何将上下文对象传递给资源“...索引”路由?

于 2013-02-05T14:03:53.583 回答
3

如果我不使用 ember 数据怎么办?如何在类似的路线中获取父 ID

  this.resource('workspace',function () {
    this.resource('workflow', {path: '/:workspace_id/workflow'}, function () {
      this.route('show', {path: '/:workflow_id'});
    });
  });

此代码将不起作用:

App.WorkflowShowRoute = Em.Route.extend({
  model: function(params) {
      var ws  = this.modelFor('workspace');  //ws is undefined
      return this.store.find('workflow', params.id, ws.id);
  }
});

编辑:我找到了一种解决方法,它并不理想,但完全按照我想要的方式工作。

  this.resource('workspace',function () {
    this.route('new');
    this.route('show', {path: '/:workspace_id'});
    //workflow routes
    this.resource('workflow', {path: '/'}, function () {
      this.route('new', {path:'/:workspace_id/workflow/new'});
      this.route('show', {path: '/:workspace_id/workflow/:workflow_id'});
    });
  });

在我的工作流程路线中,我可以按照我对 params 属性的期望访问 workspace_id :

App.WorkflowShowRoute = Em.Route.extend({
  model: function(params) {
      return this.store.find('workflow', params.workflow_id, params.workspace_id);
  }
});

最后,这是我在 workspace.show 路由助手中的链接:

{{#each workflow in workflows}}
  <li>
    {{#link-to 'workflow.show' this.id workflow.id}}{{workflow.name}}{{/link-to}}
  </li>
{{/each}}
于 2014-09-09T12:15:32.027 回答