嵌入式有许多关系并不总是有效

时间:2016-05-18 19:14:37

标签: ember.js ember-data relationship

我有一个ticket模型,其ticket_holders属性为hasMany关系

ticket_holders: DS.hasMany('ticket-holder'),

ticket-holder模型的ticket属性定义为belongsTo

ticket: DS.belongsTo('ticket')

ticket序列化程序中,我定义了EmbeddedRecordsMixin并设置attrs属性,如此

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
    attrs: {
        ticket_holders: {embedded: 'always'}
    },
    ...

API中的示例ticket如下所示

{
   "eid":"5060",
   "user_id":"13193",
   "ticket_id":"612",
   "ticket_purchase_code":"1ab9e0f20178220a75d5d2ca18322efa",
   "ticket_purchase_behalf_name":null,
   "ticket_purchase_behalf_email":null,
   "ticket_name_alias":null,
   "ticket_cost":"0.00",
   "ticket_ticketing_time":"2015-11-17 11:24:32",
   "ticket_qty":"1",
   "ticket_total":"1.00",
   "ticket_paid":"1",
   "ticket_transaction_id":"noch_5060564b54c0c42ffy0M8tpk",
   "ticket_validation_phrase":"",
   "ticket_pay_type":"card",
   "ticket_qty_redeemed":"0",
   "ticket_refunded":"0",
   "ticket_promo_code_used":"",
   "ticket_name":"Ticket #2",
   "ticket_purchase_user_name":"Tony Stark",
   "ticket_holders":[
      {
         "holder_code":"hc_299dc35e",
         "holder_user_id":"0",
         "holder_id":"8181",
         "holder_name":"Jordan again",
         "holder_email":"Riser.jordan@gmail.com",
         "holder_phone":"",
         "holder_paddle_number":"4888",
         "holder_rsvp":"0",
         "holder_redeemed":"1",
         "holder_last_updated":"2016-05-13 19:07:13",
         "ticket_qrcode":"barcode\/barcode.processor.php?encode=QRCODE&bdata=&qrdata_type=link&qr_link_link=http%3A%2F%2Fbidr.co%2Fl%2F8qexEa&height=500&scale=2&bgcolor=%23ffffff&color=%231B3448&file=&folder&type=png&Genrate=Create+Barcode&ECLevel=L&margin=",
         "ticket_pdf":"http:\/\/bidr.co\/ticket\/pdf_ticket.php?s1=1ab9e0f20178220a75d5d2ca18322efa&s2=hc_299dc35e"
      },
      ...
   ]
}

ticket的primaryKey设置为primaryKey: 'ticket_purchase_code',所以在我使用的存储方法的normalize函数中,我遍历每个ticket抓住它的ticket_purchase_code并且然后循环遍历每个ticket的{​​{1}}数组,并在每个被调用的ticket_holders上设置一个新属性,即我从其父ticket_purchase_code条记录中获取的属性。

当我尝试遍历ticket然后在模板中ticket-holder时,似乎ticket并不总是归因于ticket

ticket_holders

有时我只会在每个{{#each tickets as |purchase|}} ... {{#each purchase.ticket_holders as |ticket|}} ... {{/each> {{/each}} 下看到一个ticket-holder,有时我会在每个ticket下看到所有正确的ticket-holder,但更一致的是我只看到一个ticket ticket-holder

下的ticket

我的关系设置错了吗?

我还尝试将我在ticket_purchase_code序列化程序中循环的每个ticket_holder上的ticket更改为ticket_id,看看它是否有用但是它无法正常工作任

1 个答案:

答案 0 :(得分:1)

我在自己乔丹面前遇到过这种情况。对我而言,情况是我有 2 门票,两者都有相同的持票人,即"ticket_id":"612"和{{ 1}}都有"ticket_id":"633"。根据{{​​1}}穷人"holder_id":"8181"的性质,只能链接到单个故障单,因此只有最后一张加载到具有belongsTo的ember数据的故障单才能保留他。< / p>

解决方案 尝试在持票人中评论"holder_id":"8181",看看它们是否都像您期望的那样加载。

"holder_id":"8181"

示例,在此http://jsbin.com/sevipa/1/edit?html,js,output中,

DS.belongsTo('ticket')

我评论了与 // Try commenting the ling below out ticket: DS.belongsTo('ticket') 的关系,以便Matt H将出现在前两家公司中。如果你把线放回去,你会发现他只是继续属于约翰逊鞋修理,因为它装入了Housing4Hope之后的余烬数据。