Skip to content

Commit

Permalink
fix(util, datePicker): support $compileProvider.debugInfoEnabled(false);
Browse files Browse the repository at this point in the history
add `$mdUtil.validateScope( )` to confirm availability of scope value.

Fixes angular#4532.

>  [$compileProvider](https://docs.angularjs.org/guide/production) explains that `$compileProvider.debugInfoEnabled(false)` will make the `scope()` function return undefined: @see angular/angular.js@dbbe6b3
  • Loading branch information
ThomasBurleson authored and kennethcachia committed Sep 23, 2015
1 parent 31168f1 commit e4a115d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/components/datepicker/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,13 @@
if (this.$attrs['ngDisabled']) {
// The expression is to be evaluated against the directive element's scope and not
// the directive's isolate scope.
this.$element.scope().$watch(this.$attrs['ngDisabled'], function(isDisabled) {
self.setDisabled(isDisabled);
});
var scope = this.$mdUtil.validateScope(this.$element) ? this.$element.scope() : null;

if ( scope ) {
scope.$watch(this.$attrs['ngDisabled'], function(isDisabled) {
self.setDisabled(isDisabled);
});
}
}

Object.defineProperty(this, 'placeholder', {
Expand Down
19 changes: 18 additions & 1 deletion src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in

// If the expression evaluates to FALSE, then it is not focusable target
var focusExpression = it[0].getAttribute(attribute);
var isFocusable = focusExpression ? (it.scope().$eval(focusExpression) !== false ) : true;
var isFocusable = !focusExpression || !$mdUtil.validateScope(it) ? true :
(it.scope().$eval(focusExpression) !== false );

if (isFocusable) elFound = it;
});
Expand Down Expand Up @@ -404,6 +405,22 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in
return '' + nextUniqueId++;
},

/**
* By default AngularJS attaches information about binding and scopes to DOM nodes,
* and adds CSS classes to data-bound elements. But this information is NOT available
* when `$compileProvider.debugInfoEnabled(false);`
*
* @see https://docs.angularjs.org/guide/production
*/
validateScope : function(element) {
var hasScope = element && angular.isDefined(element.scope());
if ( !hasScope ) {
$log.warn("element.scope() is not available when 'debug mode' == false. @see https://docs.angularjs.org/guide/production!");
}

return hasScope;
},

// Stop watchers and events from firing on a scope without destroying it,
// by disconnecting it from its parent and its siblings' linked lists.
disconnectScope: function disconnectScope(scope) {
Expand Down
34 changes: 34 additions & 0 deletions src/core/util/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
describe('util', function() {

describe('validateScope',function() {

it("should not find a valid scope when debug is disabled", function() {
module(function($compileProvider) {
$compileProvider.debugInfoEnabled(false);
});

inject(function($compile, $rootScope, $mdUtil) {
var widget = angular.element($compile('<div><button><img></button></div>')($rootScope));
var button = angular.element(widget.children()[0]);

$rootScope.$apply();
expect(button.scope()).toBe(undefined);
expect($mdUtil.validateScope(button)).toBe(false);

});
});

it("should find a valid scope when debug is enabled", function() {
module(function($compileProvider) {
$compileProvider.debugInfoEnabled(true);
});

inject(function($compile, $rootScope, $mdUtil) {
var widget = angular.element($compile('<div><button><img></button></div>')($rootScope));
var button = angular.element(widget.children()[0]);

$rootScope.$apply();
expect(button.scope()).toBeDefined();
expect($mdUtil.validateScope(button)).toBe(true);
});
});
});

describe('with no overrides', function() {
beforeEach(module('material.core'));

Expand Down

0 comments on commit e4a115d

Please sign in to comment.