From 676d38481676a35131b610154cca2e1aedcd5577 Mon Sep 17 00:00:00 2001 From: Maxim Georgievskiy Date: Wed, 13 Apr 2016 06:09:14 +0300 Subject: [PATCH] Fix a bug about destroy Bug: when we switch from one state (page) to another, and we have deckgrid in both states, then new deckgrid is created, then destroy is called... but is called for just created new deckgrid instead of old one. Reason: angular calls directive function only once. So that we always have the only instance of Descriptor (and several inctances of Deckgrid). Hence, descriptor.$$deckgrid will always point to the recently assigned deckgrid. Fix: instead of using $$destroy in Descriptor (that has one instance only), use destroy of deckgrid directly (specifying correct instance of DeckGrid, so that I bind it to just created instance of DeckGrid in $$link function). --- angular-deckgrid.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/angular-deckgrid.js b/angular-deckgrid.js index e57bda8..1285dd5 100644 --- a/angular-deckgrid.js +++ b/angular-deckgrid.js @@ -78,17 +78,6 @@ angular.module('akoenig.deckgrid').factory('DeckgridDescriptor', [ } - /** - * @private - * - * Cleanup method. Will be called when the - * deckgrid directive should be destroyed. - * - */ - Descriptor.prototype.$$destroy = function $$destroy () { - this.$$deckgrid.destroy(); - }; - /** * @private * @@ -98,8 +87,6 @@ angular.module('akoenig.deckgrid').factory('DeckgridDescriptor', [ Descriptor.prototype.$$link = function $$link (scope, elem, attrs, nullController, transclude) { var templateKey = 'deckgrid/innerHtmlTemplate' + (++this.$$templateKeyIndex) + '.html'; - scope.$on('$destroy', this.$$destroy.bind(this)); - if (angular.isUndefined(attrs.cardtemplate)) { if (angular.isUndefined(attrs.cardtemplatestring)) { // use the provided inner html as template @@ -136,6 +123,8 @@ angular.module('akoenig.deckgrid').factory('DeckgridDescriptor', [ scope.mother = scope.$parent; this.$$deckgrid = Deckgrid.create(scope, elem[0]); + + scope.$on('$destroy', this.$$deckgrid.destroy.bind(this.$$deckgrid)); }; return {