【问题标题】:Ember & ember data: How to use multiple dynamic segments with multiple models?Ember & ember 数据:如何将多个动态细分与多个模型一起使用?
【发布时间】:2013-10-05 22:47:20
【问题描述】:

我正在尝试使用 ember 和 ember 数据构建 github 问题跟踪器。 github API 是通过节点服务器访问的(并且工作正常),但我在尝试使用 ember 的动态段时遇到问题,并且我不确定构建路由和模型的最佳方式。

如果我希望 url 为 /user/repo/issue,我应该如何构建我的路由和模型?

我已经尝试了动态段和资源/路线的所有不同组合...

this.resource('user', {path: ':user'}, function(){
  this.resource('repo', {path: ':repo'}, function(){
    this.resource('issue', {path: ':issue_id'});
  });
}); // 1

this.resource('repo', {path: ':userAndRepo'}, function(){
  this.resource('issue', {path: ':issue_id'});
}); // 2

this.resource('repo', {path: ':userAndRepo'});
this.resource('issue', {path: ':userAndRepo/:issue_id'}); //3

但我似乎无法让它顺利运行。我扯着胡子试图理解它。

我得到的最接近的是 :userAndRepo 的组合段,但是对于我在问题模板中的任何链接,我必须在 IssuesController 上明确设置该模型,以便我的链接是 @ 987654325@ 此外,每次我从子 issue 路由移回父 repo 路由时,我的 API 都会受到影响。

任何想法或建议将不胜感激。抱歉,如果我没有很好地解释自己。

【问题讨论】:

    标签: javascript ember.js ember-data


    【解决方案1】:

    1 号是正确的实现。这是一个包含嵌套资源的简单示例(我很懒,只是使用了 pojos,但是您可以看到模型挂钩何时被命中)。

    http://emberjs.jsbin.com/EWogUdE/36/edit

    http://emberjs.jsbin.com/EWogUdE/36#/

     App.Router.map(function () {
       this.resource('people', function(){
         this.resource('person', {path:':name'}, function(){
           this.resource('dog', {path:':dogname'});
         });
       });
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-12
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多