Skip to content

Commit

Permalink
[Tree] Simplify synchronization (#1100)
Browse files Browse the repository at this point in the history
Simplify synchronization of selection in tree with state
passed-in, letting Angular deal with more of the specifics.
Fixes #1008.
  • Loading branch information
VWoeltjen authored and larkin committed Aug 5, 2016
1 parent 0274490 commit c8931f8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion platform/commonUI/general/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ define([
{
"key": "mctTree",
"implementation": MCTTree,
"depends": ['$parse', 'gestureService']
"depends": ['gestureService']
}
],
"constants": [
Expand Down
15 changes: 6 additions & 9 deletions platform/commonUI/general/src/directives/MCTTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,25 @@ define([
'angular',
'../ui/TreeView'
], function (angular, TreeView) {
function MCTTree($parse, gestureService) {
function link(scope, element, attrs) {
function MCTTree(gestureService) {
function link(scope, element) {
var treeView = new TreeView(gestureService),
expr = $parse(attrs.mctModel),
unobserve = treeView.observe(function (domainObject) {
if (domainObject !== expr(scope.$parent)) {
expr.assign(scope.$parent, domainObject);
scope.$apply();
}
scope.mctModel = domainObject;
scope.$apply();
});

element.append(angular.element(treeView.elements()));

scope.$parent.$watch(attrs.mctModel, treeView.value.bind(treeView));
scope.$watch('mctModel', treeView.value.bind(treeView));
scope.$watch('mctObject', treeView.model.bind(treeView));
scope.$on('$destroy', unobserve);
}

return {
restrict: "E",
link: link,
scope: { mctObject: "=" }
scope: { mctObject: "=", mctModel: "=" }
};
}

Expand Down
8 changes: 4 additions & 4 deletions platform/commonUI/general/test/directives/MCTTreeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ define([
expect(mctTree.restrict).toEqual("E");
});

it("two-way binds to mctObject", function () {
expect(mctTree.scope).toEqual({ mctObject: "=" });
it("two-way binds to mctObject and mctModel", function () {
expect(mctTree.scope).toEqual({ mctObject: "=", mctModel: "=" });
});

describe("link", function () {
Expand All @@ -69,8 +69,8 @@ define([
});

it("watches for mct-model's expression in the parent", function () {
expect(mockScope.$parent.$watch).toHaveBeenCalledWith(
testAttrs.mctModel,
expect(mockScope.$watch).toHaveBeenCalledWith(
"mctModel",
jasmine.any(Function)
);
});
Expand Down

0 comments on commit c8931f8

Please sign in to comment.