diff --git a/platform/representation/src/MCTRepresentation.js b/platform/representation/src/MCTRepresentation.js index 49b2ae0f57..98a814c362 100644 --- a/platform/representation/src/MCTRepresentation.js +++ b/platform/representation/src/MCTRepresentation.js @@ -136,6 +136,14 @@ define( } } + // Destroy (deallocate any resources associated with) any + // active representers. + function destroyRepresenters() { + activeRepresenters.forEach(function (activeRepresenter) { + activeRepresenter.destroy(); + }); + } + // General-purpose refresh mechanism; should set up the scope // as appropriate for current representation key and // domain object. @@ -152,10 +160,8 @@ define( // via the "inclusion" field $scope.inclusion = representation && getPath(representation); - // Any existing gestures are no longer valid; release them. - activeRepresenters.forEach(function (activeRepresenter) { - activeRepresenter.destroy(); - }); + // Any existing representers are no longer valid; release them. + destroyRepresenters(); // Log if a key was given, but no matching representation // was found. @@ -209,6 +215,10 @@ define( // model's "modified" field, by the mutation capability. $scope.$watch("domainObject.getModel().modified", refreshCapabilities); + // Make sure any resources allocated by representers also get + // released. + $scope.$on("$destroy", destroyRepresenters); + // Do one initial refresh, so that we don't need another // digest iteration just to populate the scope. Failure to // do this can result in unstable digest cycles, which diff --git a/platform/representation/test/MCTRepresentationSpec.js b/platform/representation/test/MCTRepresentationSpec.js index 337f214a86..a50347df70 100644 --- a/platform/representation/test/MCTRepresentationSpec.js +++ b/platform/representation/test/MCTRepresentationSpec.js @@ -106,7 +106,7 @@ define( mockSce.trustAsResourceUrl.andCallFake(function (url) { return url; }); - mockScope = jasmine.createSpyObj("scope", [ "$watch" ]); + mockScope = jasmine.createSpyObj("scope", [ "$watch", "$on" ]); mockElement = jasmine.createSpyObj("element", JQLITE_FUNCTIONS); mockDomainObject = jasmine.createSpyObj("domainObject", DOMAIN_OBJECT_METHODS);