【问题标题】:Ember.js Dynamic Segments Without Nested Routes没有嵌套路由的 Ember.js 动态段
【发布时间】:2013-05-09 07:20:11
【问题描述】:

你们可能需要更多细节来回答这个问题,但我认为这可能很简单。我正在使用 Ember Data 和夹具适配器。

这会正确地将 URL 映射到我的模型中的每个项目。

App.Router.map(function() {
  this.resource('quotes', function(){
    this.resource('quote', {path: '/:quote_id' })
  });
});

App.QuotesRoute = Ember.Route.extend ({
    model: function(){
        return App.Quote.find();
    }
});

但事实并非如此。

App.Router.map(function() {
  this.resource('quotes', {path: '/:quote_id' });
});

App.QuotesRoute = Ember.Route.extend ({
    model: function(){
        return App.Quote.find();
    }
});

Ember 是否只知道在嵌套资源时返回 App.Quote.find(quote_id)?

【问题讨论】:

  • this.route('quotes', {path: '/:quote_id' }); 呢?
  • Uncaught Error: assertion failed: Cannot call get with 'id' on an undefined object.
  • 还有这个this.resource('quotes', {path: '/qoutes/:quote_id' });

标签: ember.js


【解决方案1】:

第二个示例中的路由控制器将生成为 Ember.ArrayController,这将与为“/:quote_id”路径生成的控制器不匹配,因为它是单例。

如果您通过使用 return App.Quote.find(1); 或将返回一条记录的东西传递单个模型会发生什么。

我仍在尝试了解您希望使用第二个代码示例发生什么。你期待什么样的逻辑?

【讨论】:

  • 在第一个示例中,如果我访问whatever.com/#/quotes/1,它会从我的夹具数据中返回第一条记录。在第二个例子中,它没有。它不返回任何东西,它告诉我Cannot call get with 'id' on an undefined object.我真的只是想了解他们为什么表现不同。
  • 行为有所不同,因为在第一个示例中,模型数组被加载到(自动创建)Quotes 类型为Ember.ArrayController 的控制器中,并且路由支持数组到单例的过程这对您的路由定义是隐含的,并自动将数组中的第一条记录加载到为Quote 控制器创建的Ember.ObjectController 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 2014-07-05
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
相关资源
最近更新 更多