Ember ember-views.render-double-modify弃用

时间:2015-12-17 03:51:47

标签: ember.js ember-components

我正在努力消除我最近在我的代码中引入的弃用,我需要一些帮助。

弃用是:

  

您在一次渲染中修改了两次...这在Ember 1.x中是不可靠的,将在Ember 2.0中删除[弃用ID:ember-views.render-double-modify]

我要做的是显示分数列表,然后显示总分。我需要降低最低分,而不是将其包含在计算总数中。我有这个工作。当我尝试通过classNameBindings将CSS类添加到已排除的分数时,就会弃用。

我非常确定当我在computedTotal的计算属性中执行Ember.set时会发生这种情况。

我的问题是,当我在表格中更改分数时,如何更新CSS总分也会更新?

代码详情: 我有两个组成部分; 得分行判断行。分数行包含一系列分数对象,循环遍历每个分数,调用法官分数组件。

Ember      : 2.2.0
Ember Data : 2.2.1

更新这是一个有效的Ember Twiddle,展示了这个问题:

https://ember-twiddle.com/6696d907b604aa750201?numColumns=1

index.js - (针对此问题提取的模拟代码)

let scores = new Ember A();
scores.pushObject(Ember.Object.create({ points: 1 }));
scores.pushObject(Ember.Object.create({ points: 2 }));
scores.pushObject(Ember.Object.create({ points: 3 }));

index.hbs

{{score-row scores=scores}}

得分-row.hbs

{{#each scores as |score|}}
    {{judge-score score=score}}
{{/each}}

{{calculatedTotal}}

得分-row.js:

calculatedTotal: Ember.computed('scores.@each.points', () => {
    let totalScore = 0,
        scores = this.get('scores');

    if(Ember.isPresent(scores)) {
        var i,
            scoresLength = scores.length,
            sortedAscending,
            numLowToDrop = 1;

        sortedAscending = this.get('sortedScores');

        for(i = 0; i < scoresLength; i++) {
            currentScoreObj = sortedAscending.objectAt(i);

            // I think this is what is causing the ember-views.render-double-modify
            Ember.set(currentScoreObj, '_droppedLow', (i < numLowToDrop));
        }

        sortedAscending.forEach((score) => {
            if( !score.get('_droppedLow') ) {
                totalScore += +score.get('points');
            }
        });
    }
    return totalScore;
}),

// had to write my own sort because scores.sortBy('points') was sorting as if 
// points were a string and not a number ?
sortedScores: Ember.computed.sort('scores.@each.points', (score1, score2) => {
    if (+score1.get('points') > +score2.get('points')) {
        return 1;
    } else if (+score1.get('points') < +score2.get('points')) {
        return -1;
    }
    return 0;
})

法官score.hbs

{{input value=score.points}}

法官score.js

import Ember from 'ember';

export default Ember.Component.extend({
    classNameBindings: [
        "score._droppedLow:droppedLow"
    ]
});

谢谢!

1 个答案:

答案 0 :(得分:0)

我想知道是否每次调用pushObject时它会重新触发你的计算属性?

试试这个:

let scores = new Ember A([
  Ember.Object.create({ points: 1 }),
  Ember.Object.create({ points: 1 })
]);