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

respect explicit return values from controller functions (fixes #11147) #11326

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1967,13 +1967,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
for (i in elementControllers) {
controller = elementControllers[i];
var controllerResult = controller();
if (controllerResult !== controller.instance &&
controller === controllerForBindings) {
// Remove and re-install bindToController bindings
thisLinkFn.$$destroyBindings();
thisLinkFn.$$destroyBindings =
if (controllerResult !== controller.instance && (isObject(controllerResult) || isFunction(controllerResult))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in particular, don't worry about the isObject() || isFunction() stuff, since that's handled in controller.js anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind
I'm not sure what you are asking for here. We should mimic the behavior of native javascript: If they return an object or function use that as the result of the instantiation, otherwise return "as normal":

function returnsStr() {
  this.foo = "bar";
  return "baz";
}

new returnsStr() === {foo:"bar"} // "baz" return value is ignored

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is duplicating code which already runs in the controller() function itself, it's not needed here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

particularly here (the instantiate object is not updated here, because the compiler needed to be able to determine if a new result was returned so that the bindings could be reset --- so you still need to set controller.instance)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this diff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind,
I see what you are saying, my above concern is already handled by the call to the controller() callback. I am adding a test case that verifies it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh!
Come on Github! Real time updates for line comments!

controller.instance = controllerResult;
$element.data('$' + directive.name + 'Controller', controllerResult);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a few different circumstances that this happens, so we might need to be a bit more careful about this --- be sure to include test coverage that covers controllers for transclude directives too

if (controller === controllerForBindings) {
// Remove and re-install bindToController bindings
thisLinkFn.$$destroyBindings();
thisLinkFn.$$destroyBindings =
initializeDirectiveBindings(scope, attrs, controllerResult,
bindings, scopeDirective);
bindings, scopeDirective);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change indentation (you can put this all on one line though --- this is v8 style sneaking in =)

}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4197,6 +4197,28 @@ describe('$compile', function() {
});


it('should respect controller return value', function() {
module(function() {
directive('logControllerProp', function(log) {
return {
controller: function($scope) {
this.foo = 'baz'; // value should not be used.
return {foo: 'bar'};
},
link: function(scope, element, attrs, controller) {
log(controller.foo);
}
};
});
});
inject(function(log, $compile, $rootScope) {
element = $compile('<log-controller-prop></log-controller-prop>')($rootScope);
expect(log).toEqual('bar');
expect(element.data('$logControllerPropController').foo).toEqual('bar');
});
});


it('should get required parent controller', function() {
module(function() {
directive('nested', function(log) {
Expand Down