From 560454e7c253b96c0bf461d0c2c7e336be17f560 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Fri, 30 Oct 2015 15:55:56 -0700 Subject: [PATCH] [Representation] Verify change before representing An mct-representation may have a refresh triggered either by a key change or a domain object change; both will typically happen in the same digest cycle. Track what prior state was an abort refreshes if nothing will change. --- .../representation/src/MCTRepresentation.js | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/platform/representation/src/MCTRepresentation.js b/platform/representation/src/MCTRepresentation.js index 10b6d3ccec..d1937389d2 100644 --- a/platform/representation/src/MCTRepresentation.js +++ b/platform/representation/src/MCTRepresentation.js @@ -31,7 +31,6 @@ define( function () { "use strict"; - /** * Defines the mct-representation directive. This may be used to * present domain objects as HTML (with event wiring), with the @@ -96,6 +95,9 @@ define( }), toClear = [], // Properties to clear out of scope on change counter = 0, + couldRepresent = false, + lastId, + lastKey, changeTemplate = templateLinker.link($scope, element); // Populate scope with any capabilities indicated by the @@ -141,6 +143,13 @@ define( }); } + function unchanged(canRepresent, id, key) { + return canRepresent && + couldRepresent && + id === lastId && + key === lastKey; + } + // General-purpose refresh mechanism; should set up the scope // as appropriate for current representation key and // domain object. @@ -149,7 +158,13 @@ define( representation = lookup($scope.key, domainObject), path = representation && getPath(representation), uses = ((representation || {}).uses || []), - canRepresent = !!(path && domainObject); + canRepresent = !!(path && domainObject), + id = domainObject && domainObject.getId(), + key = $scope.key; + + if (unchanged(canRepresent, id, key)) { + return; + } // Create an empty object named "representation", for this // representation to store local variables into. @@ -173,6 +188,11 @@ define( delete $scope[property]; }); + // To allow simplified change detection next time around + couldRepresent = canRepresent; + lastId = id; + lastKey = key; + // Populate scope with fields associated with the current // domain object (if one has been passed in) if (canRepresent) {