当前位置: 动力学知识库 > 问答 > 编程问答 >

ember.js - belongsTo Associations not getting mapped using ActiveModelAdapter

问题描述:

I am working on building an e-commerce site. My models and their associations are as follows-

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')

})

I am loading all the product variants by sending an ajax request (using ActiveModelAdapter).

Now, for adding the variants in the cart, I have an api end point which accepts variant_id and quantity. This end point responds with the whole Cart containing all the Line Items.

When I add the variants from the variant detail view (by clicking 'add' button), the variant is added successfully and I show the Cart with the response. Now, for increasing the quantity of the added Line Item, I need to find the variant from the LineItem. To do so, I am doing the following-

lineItem.get('variant')

This does not work. To my understanding, since variant belongsTo LineItem, this should have worked.

EDIT

Json response while loading products-

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."

}

]

Also, when I send POST request to create line Item, I get the whole cart in response. The response is as follows-

{

"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,

}

]

}]

}

Please help!

分享给朋友:
您可能感兴趣的文章:
随机阅读: