dimanche 23 avril 2017

ember: cannot access model from service

I am working on a cart service that will be injected, and I am using ember-data to store the cart data (because I need to persist the cart to the back-end).

Naturally, within the service I need to create computed properties on the cart data so that I can provide sums of quantities and totals.

But I am having a really, really hard time accessing the 'cart' model from the store in order to do this. Here's what I am trying to do:

export default Service.extend({

    store: inject.service(),

    cartObj: null, // set in init() so cart data can be used in computed properties

    init() {
        this._super(...arguments);

        // get cart model from store, set it to property on the service
        let cartObj = this.get('store').peekAll('cart').get('firstObject');
        set(this, 'cartObj', cartObj);
    },

    lineItems: computed('cartObj.cartitems.[]', function() {
        // cartitems is a collection stored in each cart object
        return get(this, 'cartObj.cartitems');
    }),
    total: computed.sum('itemPrices'), // cart total

...

No matter what I try in init(): this.get('store').peekAll('cart').get('firstObject') or this.store.peekAll('cart').get('firstObject') or variations of the same with .objectAt(0) ...nothing works.

I either get errors about "get" not being a function, or the cartObj assignment evaluates to null

EVEN THOUGH the model is there, with data in it:

enter image description here

What am I missing here?




Aucun commentaire:

Enregistrer un commentaire