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

Commit

Permalink
fix($injector): allow a constructor function to return a function
Browse files Browse the repository at this point in the history
This change makes `$injector.instantiate` (and thus `$provide.service`) to behave the same as native
`new` operator.
  • Loading branch information
vojtajina authored and IgorMinar committed Oct 18, 2013
1 parent dba566a commit c22adbf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ function createInjector(modulesToLoad) {
instance = new Constructor();
returnedValue = invoke(Type, instance, locals);

return isObject(returnedValue) ? returnedValue : instance;
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
}

return {
Expand Down
10 changes: 10 additions & 0 deletions test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,16 @@ describe('injector', function() {
});


it('should allow constructor to return a function', function() {
var fn = function() {};
var Class = function() {
return fn;
};

expect($injector.instantiate(Class)).toBe(fn);
});


it('should handle constructor exception', function() {
expect(function() {
$injector.instantiate(function() { throw 'MyError'; });
Expand Down

2 comments on commit c22adbf

@wbyoko
Copy link
Contributor

@wbyoko wbyoko commented on c22adbf Nov 8, 2013

Choose a reason for hiding this comment

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

This is a possible breaking change for those that use preprocessors (coffeescript) and writing services.

As the service could return a function unknowingly and then not have all defined properties.

Why not just have them write a factory to get this functionality?

@wbyoko
Copy link
Contributor

@wbyoko wbyoko commented on c22adbf Nov 8, 2013

Choose a reason for hiding this comment

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

Note: adding a return at the bottom of coffeescript services does fix issue.

Please sign in to comment.