From 444398e9d40777c9763b2c913eac52d6c4da73ba Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Wed, 22 Oct 2014 18:13:12 -0700 Subject: [PATCH] refactor(jsonTree): remove dependancy on inspectedApp --- panel/components/json-tree/json-tree.js | 15 +++++---- panel/components/json-tree/json-tree.spec.js | 33 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 panel/components/json-tree/json-tree.spec.js diff --git a/panel/components/json-tree/json-tree.js b/panel/components/json-tree/json-tree.js index 7fe0aa8..6385678 100644 --- a/panel/components/json-tree/json-tree.js +++ b/panel/components/json-tree/json-tree.js @@ -6,26 +6,29 @@ var BAT_JSON_TREE_TEMPLATE = '
'; /* * TODO: remove dependency on inspectedApp service */ -function batJsonTreeDirective($compile, inspectedApp) { +function batJsonTreeDirective() { return { restrict: 'E', terminal: true, scope: { - scopeId: '=', - val: '=' + batInspect: '&', + batModel: '=' }, link: jsonTreeLinkFn }; function jsonTreeLinkFn(scope, element, attrs) { - var root = angular.element(BAT_JSON_TREE_TEMPLATE) + var root = angular.element(BAT_JSON_TREE_TEMPLATE); element.append(root); var branches = { '': root }; - scope.$watch('val', function (val) { + scope.$watch('batModel', function (val) { + if (!val) { + return; + } Object. keys(val). filter(function (key) { @@ -67,7 +70,7 @@ function batJsonTreeDirective($compile, inspectedApp) { // you can't expand an empty array if (val['~array-length'] !== 0) { parentElt.on('click', function () { - inspectedApp.watch(scope.scopeId, fullPath); + scope.batInspect({ path: fullPath }); parentElt.addClass('expanded'); }); } diff --git a/panel/components/json-tree/json-tree.spec.js b/panel/components/json-tree/json-tree.spec.js new file mode 100644 index 0000000..096cce8 --- /dev/null +++ b/panel/components/json-tree/json-tree.spec.js @@ -0,0 +1,33 @@ +'use strict'; + +describe('batJsonTree', function () { + + var $compile, $rootScope, element; + + beforeEach(module('batarang.json-tree')); + beforeEach(inject(function (_$compile_, _$rootScope_) { + $compile = _$compile_; + $rootScope = _$rootScope_; + })); + + it('should not throw on an undefined model', function () { + expect(compileTree).not.toThrow(); + }); + + it('should render a simple model', function () { + $rootScope.data = { + '': { '$id': 1 } + } + compileTree(); + expect(element.text()).toBe('$id: 1'); + }); + + function compileTree() { + element = compile(''); + $rootScope.$apply(); + } + + function compile(template) { + return $compile(template)($rootScope); + } +});