These are my files:
Models
app / models / basket.js:
export default DS.Model.extend({ name: DS.attr('string'), house: DS.belongsTo('house', { async: true }), boxes: DS.hasMany('box', { async: true }) });
application / models / box.js:
export default DS.Model.extend({ qty: DS.attr('number'), basket: DS.belongsTo('basket'), cartLines: DS.hasMany('cart-line', { async: true }) });
application / models / basket-line.js:
export default DS.Model.extend({ qty: DS.attr('number'), box: DS.belongsTo('box'), product: DS.belongsTo('product') });
app / models / product.js:
export default DS.Model.extend({ name: DS.attr('string'), price: DS.attr('number') });
Routes
app / routes / basket.js:
export default Ember.Route.extend({ model(params) { return Ember.RSVP.hash({ basket: this.store.findRecord('basket', params.basket_id), boxes: this.store.findAll('box'), products: this.store.findAll('product') }); }, setupController(controller, models) { controller.setProperties(models); } });
Controllers
app / controllers / basket.js:
export default Ember.Controller.extend({ subTotal: Ember.computed(' boxes.@each.cartLines ', function () { return this.products.reduce((price, product) => { var total = price + product.get('price'); return total; }, 0); }) });
Questions:
I am a newbie, so I study errors and errors. Unfortunately.
1) What is the best Ember method for filtering relationships when you first enter a route? For example, now I load each box into my application with boxes: this.store.findAll('box') . I need a way to not load all the boxes in my webapp, just in the trash. Do I need a "filter request" directly from the backend?
UPDATE QUESTION 2) What is the best way to calculate subTotal level? Now, with the code below, Ember gives me subTotal, but only in console.log(tot) and after promises! Why is this? How can I wait for promises? I do not understand what to do:
subTotal: Ember.computed(' basket.boxes.@each.cartLines ', function () { let count = 0; console.log('subTotal called: ', count); // It should be 0 ever count = count + 1; return this.get('basket.boxes').then(boxes => { boxes.forEach(box => { box.get('cartLines').then(cartLines => { cartLines.reduce(function (tot, value) { console.log('tot:', tot + value.get('product.price')); return tot + value.get('product.price'); }, 0); }); }); }); });
It gives me the [object Object] template, because I also use hbs {{log subTotal}} , and in the console it gives me the following:
subTotal called: 0 ember.debug.js:10095 Class {__ember1476746185015: "ember802", __ember_meta__: Meta} subTotal called: 0 ember.debug.js:10095 Class {__ember1476746185015: "ember934", __ember_meta__: Meta} ember.debug.js:10095 Class {isFulfilled: true, __ember1476746185015: "ember934", __ember_meta__: Meta} subTotal called: 0 ember.debug.js:10095 Class {__ember1476746185015: "ember1011", __ember_meta__: Meta} ember.debug.js:10095 Class {isFulfilled: true, __ember1476746185015: "ember1011", __ember_meta__: Meta} tot: 3.5 tot: 6 tot: 13.5 tot: 21 tot: 24.5 tot: 27 tot: 3.5 tot: 6 tot: 13.5 tot: 21 tot: 24.5 tot: 27 tot: 3.5 tot: 6 tot: 13.5 tot: 21 tot: 24.5 tot: 27
Why it shows three times subTotal called: 0 , regardless of whether there are zero or one or a thousand products. It always calls three times subTotal called: 0 , why ?
Is it good to use computed properties with promises?
3) Am I correct with this encapsulation of relationships?
QUESTION 2 UPDATED :
Now I use this code, but to no avail:
import Ember from 'ember'; import DS from 'ember-data'; export default Ember.Controller.extend({ totalCount: Ember.computed(' basket.boxes.@each.cartLines ', function () { let total = 0; const promise = this.get('basket.boxes').then(boxes => { boxes.map(box => { // const trypromise = boxes.map(box => { console.log('box:', box); box.get('cartLines').then(cartLines => { console.log('cartLines:', cartLines); const cartLinesPromise = cartLines.map(cartLine => { console.log('cartLine:', cartLine); // return cartLine.get('qty'); // return cartLine; // }); return { qty: cartLine.get('qty'), price: cartLine.get('product.price') }; // return cartLines.map(cartLine => { // console.log('cartLine:', cartLine); // return cartLine.get('qty'); // // return { // // qty: cartLine.get('qty'), // // price: cartLine.get('product.price') // // }; // }); }) // }); return Ember.RSVP .all(cartLinesPromise) .then(cartLinesPromise => { console.log('cartLinesPromise:', cartLinesPromise); // cartLinesPromise.reduce((tot, price) => { // console.log('tot:', tot); // console.log('price:', price); // console.log('tot+price:', tot + price); // return tot + price, 0; // }); return total = 10; // return total; }) }); }); // return total; }); return DS.PromiseObject.create({ promise }); }) })
Comments for many attempts.
In the template, I use:
{{log 'HBS totalCount:' totalCount}} {{log 'HBS totalCount.content:' totalCount.content}} Total: {{totalCount.content}}
But promise has null content.
Where am I wrong?
Any wrong return ?
Is this code โpromisingโ correct?