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

Commit

Permalink
feat(jqLite): add .controller() method
Browse files Browse the repository at this point in the history
extend JQuery with .controller() method which retrieves the closest controller for a given element
  • Loading branch information
mhevery committed Mar 19, 2012
1 parent 192ff61 commit 6c5a05a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ function bindJQuery() {
jqLite = jQuery;
extend(jQuery.fn, {
scope: JQLitePrototype.scope,
controller: JQLitePrototype.controller,
injector: JQLitePrototype.injector,
inheritedData: JQLitePrototype.inheritedData
});
Expand Down
38 changes: 25 additions & 13 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@
*
* ## In addtion to the above, Angular privides an additional method to both jQuery and jQuery lite:
*
* - `scope()` - retrieves the current Angular scope of the element.
* - `injector()` - retrieves the Angular injector associated with application that the element is
* part of.
* - `controller(name)` - retrieves the controller of the current element or its parent. By default
* retrieves controller associated with the `ng-controller` directive. If `name` is provided as
* camelCase directive name, then the controller for this directive will be retrieved (e.g.
* `'ngModel'`).
* - `injector()` - retrieves the injector of the current element or its parent.
* - `scope()` - retrieves the {@link api/angular.module.ng.$rootScope.Scope scope} of the current
* element or its parent.
* - `inheritedData()` - same as `data()`, but walks up the DOM until a value is found or the top
* parent element is reached.
*
Expand Down Expand Up @@ -268,6 +272,18 @@ function JQLiteAddNodes(root, elements) {
}
}

function JQLiteController(element, name) {
return JQLiteInheritedData(element, '$' + (name || 'ngController' ) + 'Controller');
}

function JQLiteInheritedData(element, name, value) {
element = jqLite(element);
while (element.length) {
if (value = element.data(name)) return value;
element = element.parent();
}
}

//////////////////////////////////////////
// Functions which are declared directly.
//////////////////////////////////////////
Expand Down Expand Up @@ -321,20 +337,16 @@ function isBooleanAttr(element, name) {

forEach({
data: JQLiteData,
inheritedData: function(element, name, value) {
element = jqLite(element);
while (element.length) {
if (value = element.data(name)) return value;
element = element.parent();
}
},
inheritedData: JQLiteInheritedData,

scope: function(element) {
return jqLite(element).inheritedData('$scope');
return JQLiteInheritedData(element, '$scope');
},

controller: JQLiteController ,

injector: function(element) {
return jqLite(element).inheritedData('$injector');
return JQLiteInheritedData(element, '$injector');
},

removeAttr: function(element,name) {
Expand Down Expand Up @@ -449,7 +461,7 @@ forEach({

// JQLiteHasClass has only two arguments, but is a getter-only fn, so we need to special-case it
// in a way that survives minification.
if (((fn.length == 2 && fn !== JQLiteHasClass) ? arg1 : arg2) === undefined) {
if (((fn.length == 2 && (fn !== JQLiteHasClass && fn !== JQLiteController)) ? arg1 : arg2) === undefined) {
if (isObject(arg1)) {
// we are a write, but the object properties are the key/values
for(i=0; i < this.length; i++) {
Expand Down
25 changes: 23 additions & 2 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ describe('jqLite', function() {
describe('injector', function() {
it('should retrieve injector attached to the current element or its parent', function() {
var template = jqLite('<div><span></span></div>'),
span = template.children().eq(0),
injector = angular.bootstrap(template);
span = template.children().eq(0),
injector = angular.bootstrap(template);


expect(span.injector()).toBe(injector);
Expand All @@ -150,6 +150,27 @@ describe('jqLite', function() {
});


describe('controller', function() {
it('should retrieve controller attached to the current element or its parent', function() {
var div = jqLite('<div><span></span></div>'),
span = div.find('span');

div.data('$ngControllerController', 'ngController');
span.data('$otherController', 'other');

expect(span.controller()).toBe('ngController');
expect(span.controller('ngController')).toBe('ngController');
expect(span.controller('other')).toBe('other');

expect(div.controller()).toBe('ngController');
expect(div.controller('ngController')).toBe('ngController');
expect(div.controller('other')).toBe(undefined);

dealoc(div);
});
});


describe('data', function() {
it('should set and get and remove data', function() {
var selected = jqLite([a, b, c]);
Expand Down

0 comments on commit 6c5a05a

Please sign in to comment.