@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('operations.@each.value')
    }),
    operations: DS.hasMany('operation', {
        async: true
    })
});

При выводе в шаблон значения value выводится computed properties вместо number.
В чем ошибка?
  • Вопрос задан
  • 111 просмотров
Решения вопроса 1
Kaer_Morchen
@Kaer_Morchen
Разрабатываю web-приложения.
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('operations.@each.value') // <- ошибка, так делать не надо
    }),
    operations: DS.hasMany('operation', {
        async: true
    })
});

Переделайте на так
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('operations.@each.value', 'initValue'),
});
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы