From cfb6d4ccbfa6768ed05e71ec51eda73da5da9ebc Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 2 Dec 2015 12:22:37 -0800 Subject: [PATCH] [Representation] Pass attributes one way Instead of two-way binding attributes, only pass them in from the parent scope. This permits users of mct-include to use one-time binding to reduce watch counts (two-way binding across an isolate scope does not permit this.) https://github.com/nasa/openmctweb/issues/314 --- platform/representation/src/MCTInclude.js | 34 +++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/platform/representation/src/MCTInclude.js b/platform/representation/src/MCTInclude.js index 761a798dfa..16d322dde7 100644 --- a/platform/representation/src/MCTInclude.js +++ b/platform/representation/src/MCTInclude.js @@ -57,15 +57,30 @@ define( function MCTInclude(templates, templateLinker) { var templateMap = {}; - function link(scope, element) { - var changeTemplate = templateLinker.link( - scope, - element, - scope.key && templateMap[scope.key] - ); + function link(scope, element, attrs) { + var parent = scope.$parent, + key = parent.$eval(attrs.key), + changeTemplate = templateLinker.link( + scope, + element, + key && templateMap[key] + ), + unwatches = []; - scope.$watch('key', function (key) { + unwatches = ["ngModel", "parameters"].map(function (attr) { + return parent.$watch(attrs[attr], function (value) { + scope[attr] = value; + }, true); + }); + + unwatches.push(parent.$watch(attrs.key, function (key) { changeTemplate(key && templateMap[key]); + })); + + scope.$on("$destroy", function () { + unwatches.forEach(function (unwatch) { + unwatch(); + }); }); } @@ -85,10 +100,7 @@ define( link: link, // May hide the element, so let other directives act first - priority: -1000, - - // Two-way bind key, ngModel, and parameters - scope: { key: "=", ngModel: "=", parameters: "=" } + priority: -1000 }; }