Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit cb80f28

Browse files
committed
fixup! feat(compiler): respect preAssignBindingsEnabled state
1 parent f87256b commit cb80f28

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/core/services/compiler/compiler.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ angular
8888
*
8989
*/
9090
MdCompilerProvider.$inject = ['$compileProvider'];
91-
/** @this */
9291
function MdCompilerProvider($compileProvider) {
9392

9493
var provider = this;
@@ -97,8 +96,8 @@ function MdCompilerProvider($compileProvider) {
9796
* @ngdoc method
9897
* @name $mdCompilerProvider#respectPreAssignBindingsEnabled
9998
*
100-
* @param {boolean=} enabled update the respectPreAssignBindingsEnabled state if provided, otherwise just return
101-
* the current mdPreAssignBindingsEnabled state
99+
* @param {boolean=} respected update the respectPreAssignBindingsEnabled state if provided, otherwise just return
100+
* the current Material preAssignBindingsEnabled state
102101
* @returns {*} current value if used as getter or itself (chaining) if used as setter
103102
*
104103
* @kind function
@@ -130,18 +129,12 @@ function MdCompilerProvider($compileProvider) {
130129
* `$compileProvider.preAssignBindingsEnabled()` is set to `false` (i.e. default behavior in AngularJS 1.6.0 or newer)
131130
* makes it hard to unit test Material Dialog/Toast controllers using the `$controller` helper as it always follows
132131
* the `$compileProvider.preAssignBindingsEnabled()` value.
133-
*
134-
* @deprecated
135-
* sinceVersion="1.2.0"
136-
* removeVersion="1.3.0"
137-
*
138-
* This method and the option to assign the bindings before calling the controller's constructor
139-
* will be removed in v1.3.0.
140132
*/
133+
// TODO change it to `true` in Material 1.2.
141134
var respectPreAssignBindingsEnabled = false;
142-
this.respectPreAssignBindingsEnabled = function(enabled) {
143-
if (angular.isDefined(enabled)) {
144-
respectPreAssignBindingsEnabled = enabled;
135+
this.respectPreAssignBindingsEnabled = function(respected) {
136+
if (angular.isDefined(respected)) {
137+
respectPreAssignBindingsEnabled = respected;
145138
return this;
146139
}
147140

@@ -163,16 +156,11 @@ function MdCompilerProvider($compileProvider) {
163156
* Note that this doesn't affect directives/components created via regular AngularJS methods which constitute most
164157
* Material & user-created components; their behavior can be checked via `$compileProvider.preAssignBindingsEnabled()`
165158
* in AngularJS `>=1.5.10 <1.7.0`.
166-
*
167-
* @deprecated
168-
* sinceVersion="1.2.0"
169-
* removeVersion="1.3.0"
170-
*
171-
* This method will be removed in v1.3.0.
172159
*/
173160
this.preAssignBindingsEnabled = function() {
174161
if (!respectPreAssignBindingsEnabled) {
175162
// respectPreAssignBindingsEnabled === false
163+
// We're ignoring the AngularJS `$compileProvider.preAssignBindingsEnabled()` value in this case.
176164
return true;
177165
}
178166

src/core/services/compiler/compiler.spec.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ describe('$mdCompiler service', function() {
167167
[
168168
{respectPreAssignBindingsEnabled: true},
169169
{respectPreAssignBindingsEnabled: false},
170+
// TODO change `equivalentTo` to `true` in Material 1.2.
170171
{respectPreAssignBindingsEnabled: '"default"', equivalentTo: false}
171172
].forEach(function(options) {
172173
var realRespectPreAssignBindingsEnabled = options.respectPreAssignBindingsEnabled;
@@ -175,13 +176,15 @@ describe('$mdCompiler service', function() {
175176
realRespectPreAssignBindingsEnabled;
176177

177178
describe('with respectPreAssignBindingsEnabled set to ' + realRespectPreAssignBindingsEnabled, function() {
179+
var $mdCompilerProvider;
178180
var preAssignBindingsEnabledInAngularJS = angular.version.minor < 6;
179181

180182
beforeEach(function() {
181-
module(function($mdCompilerProvider) {
182-
console.log(angular.version, preAssignBindingsEnabledInAngularJS);
183+
module(function(_$mdCompilerProvider_) {
184+
$mdCompilerProvider = _$mdCompilerProvider_;
185+
183186
// Don't set the value so that the default state can be tested.
184-
if (realRespectPreAssignBindingsEnabled === true || realRespectPreAssignBindingsEnabled === false) {
187+
if (typeof realRespectPreAssignBindingsEnabled === 'boolean') {
185188
$mdCompilerProvider.respectPreAssignBindingsEnabled(realRespectPreAssignBindingsEnabled);
186189
}
187190
});
@@ -202,13 +205,33 @@ describe('$mdCompiler service', function() {
202205
return compileData;
203206
}
204207

208+
it('should call $onInit even if bindToController is set to false', function() {
209+
var isInstantiated = false;
210+
211+
function TestController($scope, name) {
212+
isInstantiated = true;
213+
expect($scope.$apply).toBeTruthy();
214+
expect(name).toBe('Bob');
215+
}
216+
217+
TestController.prototype.$onInit = jasmine.createSpy('$onInit');
218+
219+
compileAndLink({
220+
template: 'hello',
221+
controller: TestController,
222+
bindToController: false,
223+
locals: {name: 'Bob'}
224+
});
225+
226+
expect(TestController.prototype.$onInit).toHaveBeenCalledTimes(1);
227+
expect(isInstantiated).toBe(true);
228+
});
229+
205230
// Bindings are not preassigned only if we respect the AngularJS config and they're
206231
// disabled there. This logic will change in Material 1.2.0.
207232
if (respectPreAssignBindingsEnabled && !preAssignBindingsEnabledInAngularJS) {
208233
it('$mdCompileProvider.preAssignBindingsEnabled should report bindings are not pre-assigned', function() {
209-
module(function($mdCompilerProvider) {
210-
expect($mdCompilerProvider.preAssignBindingsEnabled()).toBe(false);
211-
});
234+
expect($mdCompilerProvider.preAssignBindingsEnabled()).toBe(false);
212235
});
213236

214237
it('disabled should assign bindings after constructor', function() {
@@ -239,9 +262,7 @@ describe('$mdCompiler service', function() {
239262
});
240263
} else {
241264
it('$mdCompileProvider.preAssignBindingsEnabled should report bindings are pre-assigned', function() {
242-
module(function($mdCompilerProvider) {
243-
expect($mdCompilerProvider.preAssignBindingsEnabled()).toBe(false);
244-
});
265+
expect($mdCompilerProvider.preAssignBindingsEnabled()).toBe(true);
245266
});
246267

247268
it('enabled should assign bindings at instantiation', function() {

0 commit comments

Comments
 (0)