我试图消除我最近在我的代码中引入的弃用,我需要一些帮助.
弃用是:
您在单个渲染中修改了两次... 这在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" ] });
谢谢!