ember.js - ember-data-1.0.0 activemodeladapter 错误在传递给 `id` 的哈希中包含 `push`

标签 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中重现了这个问题。 。您可以看到相同的不同版本jsfiddle 当我使用 id 而不是 _id 时工作。

后端发送的负载

ActiveModel适配器将snake_case start_date转换为camel-case。我已向 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;
   }
  */ 
}); 

Ember 数据模型:

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

谢谢

最佳答案

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

attributes :id, :name, :start_date

并且有一个

has_many :schedule 

在 SchedulesSerializer 中。让我知道这是否有效。

关于ember.js - ember-data-1.0.0 activemodeladapter 错误在传递给 `id` 的哈希中包含 `push`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20544371/

相关文章:

Ember.js 重定向到模板

javascript - Ember js 函数打印源代码而不是它的实际值

javascript - Ember.js,正文后添加奇怪的字符

ember.js - 保存父模型时保存嵌入的子模型

javascript - 在 ember 中继续调用 mouseDown 上的函数

ember.js - DS.RESTAdapter 的预期 JSON 响应的完整列表是什么?

ember.js - EmberJS,了解 DS 模型生命周期

view - EmberJS : How to handle actions from the View not the Route/Controller

javascript - 您如何维护页面状态,以便您可以使用 emberjs 提供永久链接?

ember.js - 子路由(或资源)如何从模型 Hook 内访问父资源的模型?