ember-data-1.0.0 activemodeladapter错误包括传递给`push`的哈希中的`id`

时间:2013-12-12 13:04:48

标签: ember.js ember-data ember-router

我在后端使用ember-data和activemodel适配器和rails和mongoid(mongodb)。每当我向rails应用程序发出请求时,emberjs都会显示返回的数据,但在Chrome开发者控制台中显示:

Assertion failed: You must include an `id` in a hash passed to `push` 

在jsfiddle中重现的问题

我已在此 jsfiddle 中重现了此问题。当我使用 id 而不是 _id 时,您可以看到同一个 jsfiddle 的不同版本。

后端发送的有效负载

ActiveModel适配器将snake_case start_date 转换为驼峰大小写。我在ember-data rest-serializer中添加了一些自定义代码,将 _id 转换为 id ,并且在此问题中将代码粘贴到较低的位置。

    {"schedules":[

       {
         "_id":"529f38596170700787000000",
         "name":"Hair styling",
         "start_date":"2013-12-12"
       }

     ]}

现在,虽然返回的有效负载包含 id ,但如果我进入google cgrome控制台并运行下面的命令并访问 _data ,它显示 id 未定义。

 a = App.__container__.lookup("controller:schedules")
 b = a.get('content')

我使用控制台中的披露箭头并深入了解 _dat 对象,这就是我所看到的

 _data: Object
   id: undefined

自定义序列化代码

我扩展了activemodeladapter,将mongodb的_id转换为id并将其设置为主键。

App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
   primaryKey: '_id',

   normalize: function(type, hash, prop) {
      this.normalizeMongoidId(hash);
      return this._super(type, hash, prop);
   },

   normalizeMongoidId: function(hash){
     var json = { id: hash._id };
     delete hash._id;

     //hash.id = hash._id;
     //delete hash._id;
   }

  /*
   serialize: function(record, options){
     var json = this._super(record, options);

      json.schedule._id  = json.record.id;

      delete json.record.id
      return json;
   }
  */ 
}); 

余烬数据模型

App.Schedule = DS.Model.extend({
  name: DS.attr('string'),
  organization: DS.belongsTo('organization')
});

这是我的emberjs路由器

App.Router.map(function(){ 
  this.resource('schedules', {path: "/schedules"}, function(){ 
   this.resource('schedule', {path: "/:schedule_id"}, function(){});   

  }); 
});

emberjs路线定义

 App.SchedulesRoute = Ember.Route.extend({
   model: function(){
    return this.store.find('schedule');  
   }
 });

emberjs堆栈跟踪

   Assertion failed: You must include an `id` in a hash passed to `push`   application.js:31601
  (anonymous function) application.js:31601
  Ember.assert application.js:28431
  DS.Store.Ember.Object.extend.push application.js:69415
  (anonymous function) application.js:69483
  Ember.EnumerableUtils.map application.js:30115
  DS.Store.Ember.Object.extend.pushMany application.js:69482
  (anonymous function) application.js:69839
  invokeCallback application.js:36783
  (anonymous function) application.js:36838
  EventTarget.trigger application.js:36621
  (anonymous function) application.js:36921
  DeferredActionQueues.flush application.js:34001
  Backburner.end application.js:34092
  Backburner.run application.js:34131
  Ember.run application.js:34489
  hash.success application.js:74468
  fire application.js:3049
  self.fireWith application.js:3161
  done application.js:8236
  callback

由于

1 个答案:

答案 0 :(得分:0)

我有同样的问题。在你的rails序列化程序中进行计划,(假设你有一个),你应该做

attributes :id, :name, :start_date

并有一个

has_many :schedule 
在SchedulesSerializer中

。如果有效,请告诉我。