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 })
});
app/models/box.js:
export default DS.Model.extend({
qty: DS.attr('number'),
basket: DS.belongsTo('basket'),
cartLines: DS.hasMany('cart-line', { async: true })
});
app/models/cart-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'm newbie, so I'm studying and makings mistakes. Sorry.
1) Which is the best Ember way to filter relationships when I first enter in route? For example now I load every box in my app whith boxes: this.store.findAll('box')
. I need a way to not load all the box in my webapp, just the one in basket. I need the "query with filter" directly from a backend?
2) Which is the best Ember way for calculate subTotal? Now Ember gives me the subTotal of all products! Not just the total of my products in basket. Why this? I studied this: "http://ift.tt/2dV844x" but nothing. This code in controller gives me an error:
subTotal: computed('boxes.@each.cartLines', function() {
return DS.PromiseArray.create({
promise: this.get('boxes').then(boxes => {
return boxes.filter(i => i.get('cart-line'));
})
});
})
and I know this is not logically correct, but just now it gives me: "basket.js:8 Uncaught TypeError: this.get(...).then is not a function". Why this?
3) Am I right with that relationship encapsulation?
Aucun commentaire:
Enregistrer un commentaire