Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(jqLite): make injector() and scope() work with the document object
Browse files Browse the repository at this point in the history
For typical app that has ng-app directive on the html element, we now can do:

angular.element(document).injector() or .injector()
angular.element(document).scope() or .scope()

instead of:

angular.element(document.getElementsByTagName('html')[0]).injector()
...
  • Loading branch information
IgorMinar committed Mar 22, 2012
1 parent 541bedd commit 5fdab52
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ function JQLiteController(element, name) {

function JQLiteInheritedData(element, name, value) {
element = jqLite(element);

// if element is the document object work with the html element instead
// this makes $(document).scope() possible
if(element[0].nodeType == 9) {
element = element.find('html');
}

while (element.length) {
if (value = element.data(name)) return value;
element = element.parent();
Expand Down
47 changes: 47 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ describe('jqLite', function() {
expect(deepChild.inheritedData('myData')).toBeFalsy();
dealoc(element);
});


it('should work with the child html element instead if the current element is the document obj',
function() {
var item = {},
doc = jqLite(document),
html = doc.find('html');

html.data('item', item);
expect(doc.inheritedData('item')).toBe(item);
expect(html.inheritedData('item')).toBe(item);
dealoc(doc);
}
);
});


Expand All @@ -118,6 +132,18 @@ describe('jqLite', function() {
dealoc(element);
});

it('should retrieve scope attached to the html element if its requested on the document',
function() {
var doc = jqLite(document),
html = doc.find('html'),
scope = {};

html.data('$scope', scope);

expect(doc.scope()).toBe(scope);
expect(html.scope()).toBe(scope);
dealoc(doc);
});

it('should walk up the dom to find scope', function() {
var element = jqLite('<ul><li><p><b>deep deep</b><p></li></ul>');
Expand Down Expand Up @@ -147,6 +173,27 @@ describe('jqLite', function() {
expect(span.injector()).toBe(injector);
dealoc(template);
});


it('should retrieve injector attached to the html element if its requested on document',
function() {
var doc = jqLite(document),
html = doc.find('html'),
injector = {};

html.data('$injector', injector);

expect(doc.injector()).toBe(injector);
expect(html.injector()).toBe(injector);
dealoc(doc);
});


it('should do nothing with a noncompiled template', function() {
var template = jqLite('<div><span></span></div>');
expect(template.injector()).toBeUndefined();
dealoc(template);
});
});


Expand Down

0 comments on commit 5fdab52

Please sign in to comment.