samedi 22 avril 2017

ember: modeling a shopping cart in ember-data

I'm struggling with getting my head around modeling a shopping cart that needs to persist to the back-end. I think I understand the relationships, and how to retrieve data that's in the model...it's how to actually add the data that's tripping me up.

I've got two pretty simple models:

The cart:

export default DS.Model.extend({
    cartitems: hasMany('cartitem')
});

The cart items

export default DS.Model.extend({
    product: belongsTo('product', { async: true }),
    cart: belongsTo('cart'),

    variantCodes: attr(''),
    quantity: attr('number'),
    itemPrice: attr('number'),
    itemOrigPrice: attr('number')

});

I created a cart service that contains properties, computed properties and methods. In this cart service is where I'm having the trouble, starting with my add method:

...

add(item) {
    this.get('store').findRecord('cart', get(this, 'cartId')).then(result => {
        cart.get('cartitems').addObject(this.get('store').createRecord('cartitem', {
                id: item.skuid,
                variantCodes: item.variantCodes,
                quantity: item.quantity,
                itemPrice: item.itemPrice
            })
        );
        cart.save();
    });
}

That kind of works...it shows the cart and cartitem models (with data and relationsips) in the ember inspector, but doesn't send a payload for the cart items to the API. I'm thinking it's because I have to do a save for each cartitem.

But this rig also seems overly complex and I think would result in a bunch of network requests. Is this even the right approach?

I think the DS.EmbeddedRecordsMixin would help here, but I have no idea how that would look. I can't seem to find any examples of it in use for POSTing to the API. Only for retrieving. Any help appreciated!




Aucun commentaire:

Enregistrer un commentaire