refactor(jsonTree): remove dependancy on inspectedApp

test-unit-sauce
Brian Ford 10 years ago
parent d4edd956f6
commit 444398e9d4

@ -6,26 +6,29 @@ var BAT_JSON_TREE_TEMPLATE = '<div class="properties-tree"></div>';
/* /*
* TODO: remove dependency on inspectedApp service * TODO: remove dependency on inspectedApp service
*/ */
function batJsonTreeDirective($compile, inspectedApp) { function batJsonTreeDirective() {
return { return {
restrict: 'E', restrict: 'E',
terminal: true, terminal: true,
scope: { scope: {
scopeId: '=', batInspect: '&',
val: '=' batModel: '='
}, },
link: jsonTreeLinkFn link: jsonTreeLinkFn
}; };
function jsonTreeLinkFn(scope, element, attrs) { function jsonTreeLinkFn(scope, element, attrs) {
var root = angular.element(BAT_JSON_TREE_TEMPLATE) var root = angular.element(BAT_JSON_TREE_TEMPLATE);
element.append(root); element.append(root);
var branches = { var branches = {
'': root '': root
}; };
scope.$watch('val', function (val) { scope.$watch('batModel', function (val) {
if (!val) {
return;
}
Object. Object.
keys(val). keys(val).
filter(function (key) { filter(function (key) {
@ -67,7 +70,7 @@ function batJsonTreeDirective($compile, inspectedApp) {
// you can't expand an empty array // you can't expand an empty array
if (val['~array-length'] !== 0) { if (val['~array-length'] !== 0) {
parentElt.on('click', function () { parentElt.on('click', function () {
inspectedApp.watch(scope.scopeId, fullPath); scope.batInspect({ path: fullPath });
parentElt.addClass('expanded'); parentElt.addClass('expanded');
}); });
} }

@ -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('<bat-json-tree bat-model="data"></bat-json-tree>');
$rootScope.$apply();
}
function compile(template) {
return $compile(template)($rootScope);
}
});