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

brg*_*brg 6 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` 
Run Code Online (Sandbox Code Playgroud)

这个问题在jsfiddle中重现

我在这个jsfiddle中重现了这个问题.当我使用id而不是_id时,你可以看到同一个jsfiddle工作的不同版本.

后端发送的有效负载

ActiveModel适配器将snake_case start_date转换为camel-case.我已经为ember-data rest-serializer添加了一些自定义代码,以便将_id转换为id,并且在此问题中将代码粘贴到较低的位置.

    {"schedules":[

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

     ]}
Run Code Online (Sandbox Code Playgroud)

现在虽然返回有效载荷包括ID,如果我进入谷歌cgrome控制台并运行以下commmands并访问_data,它显示的ID是不确定的.

 a = App.__container__.lookup("controller:schedules")
 b = a.get('content')
Run Code Online (Sandbox Code Playgroud)

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

 _data: Object
   id: undefined
Run Code Online (Sandbox Code Playgroud)

自定义序列化代码

我扩展了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;
   }
  */ 
}); 
Run Code Online (Sandbox Code Playgroud)

余烬数据模型:

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

这是我的emberjs路由器

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

  }); 
});
Run Code Online (Sandbox Code Playgroud)

emberjs路线定义:

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

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
Run Code Online (Sandbox Code Playgroud)

谢谢

Hri*_*shi 0

我遇到过同样的问题。在你的 Rails 序列化器中进行调度(假设你有一个),你应该这样做

attributes :id, :name, :start_date
Run Code Online (Sandbox Code Playgroud)

并有一个

has_many :schedule 
Run Code Online (Sandbox Code Playgroud)

在您的 SchedulesSerializer 中。让我知道这个是否奏效。