GraFiddle / angular-chart

AngularJS directive for adjustable dynamically updating c3 charts
http://grafiddle.github.io/angular-chart/
MIT License
127 stars 40 forks source link

Usage of scope.apply() on onselect() #57

Closed ulilicht closed 9 years ago

ulilicht commented 9 years ago

Hi,

I'm not completely sure about this, but as far as I understand we have to call scope.apply() before executing functions like onselect.

My Problem: I'm triggering some scope variable change with onSelect and it is not applied. When i change https://github.com/maxklenk/angular-chart/blob/master/src/chart.js#L616

if (scope.options.selection.onselected) {
      scope.options.selection.onselected();
}

to

 if (scope.options.selection.onselected) {
       scope.$apply(function(){
           scope.options.selection.onselected();
       });
 }

It works.

Is is a problem in my application code or in the directive?

maxklenk commented 9 years ago

I think scope.options.selection.onselected(); is called in the current implementation and executing a function in the angular world triggers a digest circle which would propagate the changes further in your application. I am really careful with calling scope.$apply to create exactly one digest circle when interactions happen. My problem here is that the application should be notified because we change the options object, by only using scope.$apply when a callback function is given would make this behavior inconsistent. I think scope.$apply should be called for every selection (or for each multiple selections made at once).