Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit 88567ae

Browse files
gary-bSomeKittens
authored andcommitted
fix(jsonTree): detect deep model changes via events
Sometimes when navigating around the scopeTree the jsonTree didn't update. I believe this was caused by the $watch on the model in jsonTree being passed the objectEquality flag. This results in the use of the angular.equal function to detect if the model has changed. This function ignores any properties that start with a $... Thus when only the $properties were different across multiple models, $watch didn't trigger. The solution: a) Use a watch with reference equality to detect when the user selects a different scope to inspect. b) Listen for model:change events to detect when the model has been updated from ngHint.
1 parent a2b6e48 commit 88567ae

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

panel/components/json-tree/json-tree.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@ function batJsonTreeDirective() {
5151

5252
return;
5353
}
54-
Object.
55-
keys(val).
56-
filter(function (key) {
57-
return key.substr(0, 2) !== '$$';
58-
}).
59-
sort(byPathDepth).
60-
forEach(function (key) {
61-
buildDom(val[key], key);
62-
});
63-
}, true);
54+
renderModel(val)
55+
});
6456

57+
scope.$on('model:change', function(ev, data){
58+
if(data.id && scope.batModel && scope.batModel[''] && (data.id === scope.batModel[''].$id)){
59+
renderModel(scope.batModel);
60+
}
61+
});
62+
63+
function renderModel(val){
64+
Object.
65+
keys(val).
66+
filter(function (key) {
67+
return key.substr(0, 2) !== '$$';
68+
}).
69+
sort(byPathDepth).
70+
forEach(function (key) {
71+
buildDom(val[key], key);
72+
});
73+
}
6574

6675
function buildDom(object, depth) {
6776
branches[depth].html('');

panel/components/json-tree/json-tree.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,47 @@ describe('batJsonTree', function () {
2222
expect(element.text()).toBe('$id: 1');
2323
});
2424

25+
describe('model:change event', function() {
26+
it('should render model again when parameter has same id as model', function () {
27+
$rootScope.data = {
28+
'': {'$id': 1}
29+
};
30+
compileTree();
31+
$rootScope.data = {
32+
'': {'$id': 2}
33+
};
34+
$rootScope.$broadcast('model:change', { id: 2 });
35+
$rootScope.$apply();
36+
expect(element.text()).toBe('$id: 2');
37+
});
38+
39+
it('should not render model again when parameter has different id than model', function () {
40+
$rootScope.data = {
41+
'': {'$id': 1}
42+
};
43+
compileTree();
44+
$rootScope.$broadcast('model:change', { id: 2 });
45+
$rootScope.$apply();
46+
expect(element.text()).toBe('$id: 1');
47+
});
48+
49+
it('should not throw error if model undefined', function () {
50+
$rootScope.data = undefined;
51+
compileTree();
52+
expect(function () {
53+
$rootScope.$broadcast('model:change', { id: 2 });
54+
}).not.toThrow();
55+
});
56+
57+
it("should not throw error if '' property not set on model", function () {
58+
$rootScope.data = {};
59+
compileTree();
60+
expect(function () {
61+
$rootScope.$broadcast('model:change', { id: 2 });
62+
}).not.toThrow();
63+
});
64+
});
65+
2566
function compileTree() {
2667
element = compile('<bat-json-tree bat-model="data"></bat-json-tree>');
2768
$rootScope.$apply();

0 commit comments

Comments
 (0)