Skip to content

Commit 53a9668

Browse files
authored
Change to didUpdateAttrs() from observers
Changed to use didUpdateAttrs instead of observers. Not only is this the standard ember way now, this also prevent firing update() multiple times if multiple changes occur in a single render cycle. Also added a animate property to allow the user to decide when to animte. This allows disabling animations on update renders if they don't want a reanimation (like a onClick selection of an element in my case).
1 parent 7ff9a1a commit 53a9668

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

addon/components/ember-chart.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,39 @@ export default Ember.Component.extend({
55
tagName: 'canvas',
66
attributeBindings: ['width', 'height'],
77

8-
didInsertElement: function(){
9-
var context = this.get('element');
10-
var data = this.get('data');
11-
var type = this.get('type');
12-
var options = this.get('options');
8+
didInsertElement() {
9+
this._super(...arguments);
10+
let context = this.get('element');
11+
let data = this.get('data');
12+
let type = this.get('type');
13+
let options = this.get('options');
1314

14-
var chart = new Chart(context, {
15+
let chart = new Chart(context, {
1516
type: type,
1617
data: data,
1718
options: options
1819
});
1920
this.set('chart', chart);
20-
this.addObserver('data', this, this.updateChart);
21-
this.addObserver('data.[]', this, this.updateChart);
22-
this.addObserver('options', this, this.updateChart);
2321
},
2422

25-
willDestroyElement: function(){
23+
willDestroyElement() {
24+
this._super(...arguments);
2625
this.get('chart').destroy();
27-
this.removeObserver('data', this, this.updateChart);
28-
this.removeObserver('data.[]', this, this.updateChart);
29-
this.removeObserver('options', this, this.updateChart);
3026
},
3127

32-
updateChart: function(){
33-
var chart = this.get('chart');
34-
var data = this.get('data');
35-
var options = this.get('options');
28+
didUpdateAttrs() {
29+
this._super(...arguments);
30+
let chart = this.get('chart');
31+
let data = this.get('data');
32+
let options = this.get('options');
33+
let animate = this.get('animate');
34+
3635
chart.config.data = data;
3736
chart.config.options = options;
38-
chart.update();
37+
if (animate) {
38+
chart.update();
39+
} else {
40+
chart.update(0);
41+
}
3942
}
4043
});

0 commit comments

Comments
 (0)