Ember模型仅加载最后记录 [英] Ember model only loads last record

查看:97
本文介绍了Ember模型仅加载最后记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重现 Railscast 410 示例(称为 Raffler ),更改最后版本的设置,并符合我的习惯:




  • Ember 1.0。 0-rc.6

  • Rails 4.0.0

  • Mongoid master(4.0)

  • Haml 4

  • 徽标0.3.0



在这个示例项目中,我们创建一个简单的模型 Entry 调用一个小的Rails Rest API。



除了将Raffler.Entry.find()获取所有条目只加载最后一条记录。



这是我的模型:

  Raffler.Entry = DS.Model.extend 
name:DS.attr('string')
获胜者:DS.attr('boolean')

我的商店:

  DS.RESTAdapter。 configure('plurals',entry:'entries')

Raffler.Store = DS.Store.extend
r evision:12
适配器:DS.RESTAdapter.create()

当调用 Raffler.Entry.find() http:// localhost:3000 / entries 上有一个AJAX请求,并返回所有记录所以我不认为问题是服务器端):

  {条目:[{id:{ $ OID : 51e5b35b492cd4d286000001 }, 名 : 富  赢家:真正},{ ID:{ $ OID: 51e5b35b492cd4d286000002}, 名: 酒吧, 获得者:false},{id:{$ oid:51e5b384492cd4d286000003},name:Baz,winner:true}]} 
pre>

但是,只有最后一个这些记录才真正加载到模型中。



这里在JS控制台:

  e = Raffler.Entry.find()
e.toArray()。length
= > 1
e.objectAt(0).get('name')
=> Baz(总是最后一个)
e.objectAt(1)
=>未定义


解决方案

我终于找到了问题的原因(感谢此问题):这是因为,由默认情况下,Mongoid以 {id:{$ oid:51e5b35b492cd4d286000001}格式的 id 返回JSON >,Ember似乎不明白。



通过在我的Rails上添加这个序列号 Entry model: p>

  class EntrySerializer< ActiveModel :: Serializer 
属性:id,:name,:winner

def id
object._id.to_s
end

end

API请求现在回应此(请注意,没有 $ oid 再次):

  {条目:[{id:51e5b35b492cd4d286000001,name富, 赢家:真正},{ ID: 51e5b35b492cd4d286000002, 名: 酒吧, 赢家:假},{ ID: 51e5b384492cd4d286000003, 名: 巴兹 ,赢家:true}]} 

,而Ember现在加载所有记录:

  Raffler.Entry.find()。toArray()。length 
=> 3

编辑:请注意,这是一个Mongoid 4具体的问题,因为 $ oid 在早期版本中未使用符号。这是一个现有的Rails 3.2 / Mongoid 3.0应用程序的测试:

  1.9.3-p194:006> Mongoid :: VERSION 
=> 3.0.23
1.9.3-p194:007> Node.first.id.as_json
=> 507521e68df996381b00151b

现在我的Ember测试在Rails 4 / Mongoid 4下:

  2.0.0-p247:007> Mongoid :: VERSION 
=> 4.0.0
2.0.0-p247:008> Entry.first.id.as_json
=> {$ oid=>51e5b35b492cd4d286000001}

我添加了 mongoid 标签到我的问题。



Serializer解决方案运行良好,但它意味着为每个单个Mongoid模型创建一个序列化程序...只是返回Mongoid 3行为...不是那么干净...


I'm trying to reproduce Railscasts 410 example (called Raffler), changing the setup for last versions and to match my habits:

  • Ember 1.0.0-rc.6
  • Rails 4.0.0
  • Mongoid master (4.0)
  • Haml 4
  • Emblem 0.3.0

In this example project, we create a simple model Entry that calls a small Rails Rest API.

Everything works as expected, except that calling Raffler.Entry.find() to get all entries only loads the last record.

Here is my model :

Raffler.Entry = DS.Model.extend
  name: DS.attr('string')
  winner: DS.attr('boolean')

My store :

DS.RESTAdapter.configure('plurals', entry: 'entries')

Raffler.Store = DS.Store.extend
  revision: 12
  adapter: DS.RESTAdapter.create()

When calling Raffler.Entry.find() there's an AJAX request on http://localhost:3000/entries and all records are returned (so I don't think the problem is server side) :

{"entries":[{"id":{"$oid":"51e5b35b492cd4d286000001"},"name":"Foo","winner":true},{"id":{"$oid":"51e5b35b492cd4d286000002"},"name":"Bar","winner":false},{"id":{"$oid":"51e5b384492cd4d286000003"},"name":"Baz","winner":true}]}

But only the last of these records is really loaded in the model.

Here in the JS console :

e=Raffler.Entry.find()
e.toArray().length
=> 1
e.objectAt(0).get('name')
=> "Baz" (always the last one)
e.objectAt(1)
=> undefined

解决方案

I've finally found the cause of the problem (thanks to this question): it was because, by default, Mongoid returns JSON with id in the format {"id":{"$oid":"51e5b35b492cd4d286000001"}, that Ember does not seem to understand.

By adding this serializer on my Rails Entry model:

class EntrySerializer < ActiveModel::Serializer
  attributes :id, :name, :winner

  def id
    object._id.to_s
  end

end

API request now responds this (note there's no $oid anymore):

{"entries":[{"id":"51e5b35b492cd4d286000001","name":"Foo","winner":true},{"id":"51e5b35b492cd4d286000002","name":"Bar","winner":false},{"id":"51e5b384492cd4d286000003","name":"Baz","winner":true}]}

and Ember now loads all records :

Raffler.Entry.find().toArray().length
=> 3

EDIT: Note that this is a Mongoid 4 specific issue since the $oid notation wasn't used in earlier versions. Here is a test with an existing Rails 3.2 / Mongoid 3.0 app :

1.9.3-p194 :006 > Mongoid::VERSION
 => "3.0.23"
1.9.3-p194 :007 > Node.first.id.as_json
 => "507521e68df996381b00151b"

Now with my Ember test under Rails 4 / Mongoid 4 :

2.0.0-p247 :007 > Mongoid::VERSION
 => "4.0.0"
2.0.0-p247 :008 > Entry.first.id.as_json
 => {"$oid"=>"51e5b35b492cd4d286000001"}

I've added the mongoid tag to my question.

The Serializer solution works well but it means creating a serializer for every single Mongoid model...just to return to Mongoid 3 behavior...not that clean...

这篇关于Ember模型仅加载最后记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆