Answer the question
In order to leave comments, you need to log in
How to implement computed properties and aggregated data for subcategories and records from a linked model?
There are two category models and costs:
models/category.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
parent_id: DS.attr('number'),
rashody: DS.hasMany('rashody')
});
import DS from 'ember-data';
export default DS.Model.extend({
date: DS.attr('string'),
count: DS.attr('number'),
category: DS.belongsTo('category')
});
Answer the question
In order to leave comments, you need to log in
Regarding 2, look here , I recently had a similar question. You, as I understand it, are sawing a financial accounting service?
I solved it this way:
models/category.js
export default DS.Model.extend({
name: DS.attr('string'),
rashody: DS.hasMany('rashody',{ async: true }),
parentCategory: DS.belongsTo('category', {inverse: 'subCategory'}),
subCategory: DS.hasMany('category', {inverse: 'parentCategory'}),
//расходы по категории
rashCount: function(){
var val = 0;
this.get('rashody').forEach(function(rashod){
val += rashod.get('count');
});
return val;
}.property('[email protected]'),
// расходы по категории и дочерним категориям
subcatRashCount: function(){
var subVal =0;
this.get('subCategory').forEach(function(subCat){
subVal += subCat.get('subcatRashCount');
});
return subVal+ this.get('rashCount');
}.property('[email protected]', 'rashCount')
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question