【问题标题】:Ember fails when changing ID value更改 ID 值时 Ember 失败
【发布时间】:2013-07-17 20:06:30
【问题描述】:

我目前有我的路线设置,以便所有船都在根/,我可以通过转到/boats/:boat_id去特定的船

无论出于何种原因,如果我将 ID 设置为 :boat_id,那么 root 将不会显示任何船只并在控制台中抛出此错误:

断言失败:无法在未定义的对象上调用带有 'id' 的 get。

如果我将其更改为 :id,则根目录可以正常工作,但 /boats/{any id} 不起作用。有什么想法吗?

这是我的代码:

BoatBuilder.Router.map(function() {
  this.resource('boats', { path: '/' });
  this.resource('boat', { path: '/boat/:boat_id' }, function() {
    this.route("motor");
    this.route("accessories");
  });

  this.route("summary");
});

BoatBuilder.BoatsRoute = Ember.Route.extend({
  model: function () {
    return BoatBuilder.Boat.find();
  }
});

BoatBuilder.Boat = DS.Model.extend({
  iem_name: DS.attr('string'),
  url_text: DS.attr('string'),
  price_retail: DS.attr('number')
});

【问题讨论】:

  • 如果我建议的解决方案解决了您的问题,您能否提供反馈。干杯。

标签: routing ember.js


【解决方案1】:

如果你的模型不使用 url 中的 id 属性,你应该在你的路由上定义一个序列化方法。见http://emberjs.com/guides/routing/defining-your-routes/

此外,如果您通过#linkTo 访问/boats/:boat_id,您还需要在路由器中添加序列化挂钩。

改编自我上面粘贴的 ember guides 链接,应该是这样的:

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

 App.BoatRoute = Ember.Route.extend({
      model: function(params) {
           // the server returns 

      },

    serialize: function(model) {
       // this will make the URL `/boat/:boat_id`
     return { boat_id: model.id };
    }
 });

您可能需要对其进行调整以满足您的需要。

【讨论】:

    【解决方案2】:

    如果我将其更改为 :id,则根目录可以正常工作,但 /boats/{any id} 不起作用。有什么想法吗?

    使用您的实际路由器地图应该不是这样去特定的船吗?

    /boat/:boat_id
    

    所有的船都是这样的:

    /
    

    【讨论】:

    • 试过了,没有骰子:-(
    • @WebDevDude,编辑了我的答案,我提到的发生了什么?
    【解决方案3】:

    不要认为您需要 boats 的资源,因为您内部没有嵌套路由。所以试试:

    BoatBuilder.Router.map(function() {
      this.route('boats', { path: '/' });
      this.resource('boat', { path: '/boat/:boat_id' }, function() {
        this.route("motor");
        this.route("accessories");
      });
    
      this.route("summary");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      相关资源
      最近更新 更多