M
M
Makito2015-08-18 16:39:23
Ember.js
Makito, 2015-08-18 16:39:23

What is the error in the description of the model?

I have the following description of the model:

export default DS.Model.extend({
    initValue  : attr('number', {
        defaultValue: 0
    }),
    value: attr('number', {
        defaultValue: function() {
            var val = 0;
            this.get('operations').forEach(function(operation) {
                val += operation.get('value');
            });
            return this.get('initValue') + val;
        }.property('[email protected]')
    }),
    operations: DS.hasMany('operation', {
        async: true
    })
});

When outputting the value value to the template, computed properties are output instead of number.
Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Romanov, 2015-08-18
@Makito

export default DS.Model.extend({
    initValue  : attr('number', {
        defaultValue: 0
    }),
    value: attr('number', {
        defaultValue: function() {
            var val = 0;
            this.get('operations').forEach(function(operation) {
                val += operation.get('value');
            });
            return this.get('initValue') + val;
        }.property('[email protected]') // <- ошибка, так делать не надо
    }),
    operations: DS.hasMany('operation', {
        async: true
    })
});

Change to like this
export default DS.Model.extend({
    initValue  : DS.attr('number', { defaultValue: 0 }),    
    operations: DS.hasMany('operation', { async: true }),

    value: function() {
        var val = 0;
        this.get('operations').forEach(function(operation) {
            val += operation.get('value');
        });
        return this.get('initValue') + val;
    }.property('[email protected]', 'initValue'),
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question