From 49820ef9ff3f450556ea1ebd25fc13054355b7b8 Mon Sep 17 00:00:00 2001 From: Juan Date: Wed, 6 Apr 2016 19:10:14 -0500 Subject: [PATCH] watch for changes on data --- angular-peity.js | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/angular-peity.js b/angular-peity.js index 67911b5..c0f4400 100644 --- a/angular-peity.js +++ b/angular-peity.js @@ -10,27 +10,37 @@ var buildChartDirective = function ( chartType ) { }, link: function ( scope, element, attrs ) { - var options = {}; - if ( scope.options ) { - options = scope.options; - } + var options = {}; + if ( scope.options ) { + options = scope.options; + } - var span = document.createElement( 'span' ); - span.textContent = scope.data.join(); + var span = document.createElement( 'span' ); + span.textContent = scope.data.join(); + + if ( !attrs.class ) { + span.className = ""; + } else { + span.className = attrs.class; + } - if ( !attrs.class ) { - span.className = ""; - } else { - span.className = attrs.class; - } + if (element[0].nodeType === 8) { + element.replaceWith( span ); + } else { + element[0].appendChild( span ); + } + + jQuery( span ).peity( chartType, options ); + + var watcher = scope.$watch('data', function(){ + span.textContent = scope.data.join(); + jQuery( span ).change(); + }) - if (element[0].nodeType === 8) { - element.replaceWith( span ); - } else { - element[0].appendChild( span ); - } + scope.$on('$destroy', function(){ + watcher(); + }); - jQuery( span ).peity( chartType, options ); } };