belongsTo使用ActiveModelAdapter未映射关联

时间:2014-11-24 08:28:31

标签: ember.js ember-data

我正在建立一个电子商务网站。我的模型及其关联如下 -

App.Product = DS.Model.extend({
  // variables
  name: attr(),
  description: attr(),
  display_price: attr(),
  stock: attr(),
  dates: attr(),

  // associations
  taxons: DS.hasMany('taxon'),
  provider: DS.belongsTo('provider', { async: true }),
  images: DS.hasMany('image', { async: true }),
  variant: hasOne('variant'), // 
})

App.Variant = DS.Model.extend({
  // variables
  isMaster: attr(),
  name: attr(),
  images: attr(),

  // associations
  lineItems: DS.hasMany('lineItem'),
  product: DS.belongsTo('product')
})

App.LineItem = DS.Model.extend({
  // variables
  quantity: attr(),
  price: attr(),
  date: attr(),
  meal_type: attr(),
  total: attr(),
  display_amount: attr(),

  // associations
  variant: DS.belongsTo('variant'),
  cart: DS.belongsTo('cart')
})

我通过发送ajax请求(使用ActiveModelAdapter)加载所有产品变体。 现在,为了在购物车中添加变体,我有一个接受variant_id和数量的api终点。此终点响应包含所有行项目的整个购物车。 当我从变体详细信息视图中添加变体时(通过单击'添加'按钮),变量将成功添加,并显示带有响应的购物车。现在,为了增加添加的行项目的数量,我需要从LineItem中找到变体。为此,我正在做以下事情 -

lineItem.get('variant')

这不起作用。据我所知,由于变种belongsTo LineItem,这应该有效。

修改 Json在加载产品时的响应 -

products: [
{
  id: 1821,
  name: "Almayass- delight",
  price: "15.0",
  display_price: "$15.00",
  available_on: "2014-06-11T18:30:00.000-05:00",
  permalink: "almayass-delight",
  provider_id: 631,
  taxon_ids: [
    821,
    871,
    591,
    1041
  ],
  stock: 40,
  available: true,
  image_ids: [
    3231
  ],
  variant_id: 1811,
  dates: {
    2014-11-20: [
      1,
      2
    ],
    2014-11-21: [
      2
    ]
  },
  description: "BY VESELKA in EAST VILLAGEOne cheese, one potato, one sauerkraut and one   mushroom pierogi, one meatless stuffed cabbage, kasha with mushroom gravy served with sour cream. Delish."
  }
]

此外,当我发送POST请求以创建行项目时,我得到整个购物车作为回应。答复如下 -

{
      "line_items": [
      {
        "id":60615,
        "quantity":2,
        "price":"20.0",
        "variant_id":2471,
        "display_amount":"$40.00",
        "total":"40.0",
        "date":"2014-11-24",
        "meal_type":1,
        "product_name":"abc",
        "product_images":[
          {
            "id":2301,
            "attachment_file_name":"HanDynasty_MapoTofuVegetarian_1_U.jpg",
            "attachment_width":400,
            "attachment_height":400,
          },
          {
            "id":2281,
            "attachment_file_name":"Junoon_MurgLababdar_1_U.jpg",
            "attachment_width":1204,
            "attachment_height":587,
          }
        ]
      },
      {
        "id":60614,
        "quantity":1,
        "price":"15.0",
        "variant_id":1811,
        "display_amount":"$15.00",
        "total":"15.0",
        "date":"2014-11-24",
        "meal_type":1,
        "product_name":"Almayass- delight",
        "product_images":[
          {
            "id":3231,
            "attachment_file_name":"Almayass_AlmayassDelight_1_U.jpg",
            "attachment_width":400,
            "attachment_height":400,
          }
        ]
      }]
    }

请帮忙!

0 个答案:

没有答案