L
L
lands_of_infinity2015-08-21 13:10:17
Ember.js
lands_of_infinity, 2015-08-21 13:10:17

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')
});

models/rashody.js
import DS from 'ember-data';

export default DS.Model.extend({
    date: DS.attr('string'),
    count: DS.attr('number'),
    category: DS.belongsTo('category')
});

How to do it right:
1. Implement subcategories (it's not the best option for me to store the parrent_id value).
2. Create for the category "Computed Properties and Aggregate Data with @each (Computed Properties and Aggregate Data with @each)" which will return the sum of the rashody.count fields associated with the category and its subcategories

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Makito, 2015-08-21
@Makito

Regarding 2, look here , I recently had a similar question. You, as I understand it, are sawing a financial accounting service?

L
lands_of_infinity, 2015-08-23
@lands_of_infinity

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')
   
});

Now it is not clear how to display categories and subcategories in the template.
Goods
- Food -
Flour -
Milk - Household -
Soap
-
Powder
Services - Dry
cleaning
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question