[Edit] Fix state-watching

Repair/simplify state-watching in representer for toolbar
in edit mode; avoids issue where destroy calls detach
watches for state change in toolbar prematurely. WTD-881.
This commit is contained in:
Victor Woeltjen 2015-02-23 14:51:17 -08:00
parent 182d02599d
commit c183f08886
2 changed files with 22 additions and 13 deletions

View File

@ -76,7 +76,6 @@ define(
key = (representation || {}).key;
// Track the represented object
domainObject = representedObject;
// Ensure existing watches are released
destroy();
}

View File

@ -42,6 +42,11 @@ define(
toolbarObject.state = toolbar.getState();
}
// Get state (to watch it)
function getState() {
return toolbarObject.state;
}
// Update selection models to match changed toolbar state
function updateState(state) {
// Update underlying state based on toolbar changes
@ -52,8 +57,18 @@ define(
commit("Changes from toolbar.");
}
function initialize() {
// If we have been asked to expose toolbar state...
if (attrs.toolbar) {
// Expose toolbar state under that name
scope.$parent[attrs.toolbar] = toolbarObject;
}
}
// Represent a domain object using this definition
function represent(representation) {
//
initialize();
// Clear any existing selection
scope.selection = [];
// Get the newest toolbar definition from the view
@ -62,24 +77,19 @@ define(
updateSelection([]);
}
// Destroy; stop watching the parent for changes in
// toolbar state.
// Destroy; remove toolbar object from parent scope
function destroy() {
if (unwatch) {
unwatch();
unwatch = undefined;
// Clear exposed toolbar state (if any)
if (attrs.toolbar) {
delete scope.$parent[attrs.toolbar];
}
}
// If we have been asked to expose toolbar state...
// If this representation exposes a toolbar, set up watches
// to synchronize with it.
if (attrs.toolbar) {
// Expose toolbar state under that name
scope.$parent[attrs.toolbar] = toolbarObject;
// Detect and handle changes to state from the toolbar
unwatch = scope.$parent.$watchCollection(
attrs.toolbar + ".state",
updateState
);
scope.$watchCollection(getState, updateState);
// Watch for changes in the current selection state
scope.$watchCollection("selection", updateSelection);
}