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

feat(ngView): reference resolved locals in scope #13400

Closed
wants to merge 1 commit into from
Closed

feat(ngView): reference resolved locals in scope #13400

wants to merge 1 commit into from

Conversation

shahata
Copy link
Contributor

@shahata shahata commented Nov 27, 2015

this will make it easier to use ng-route with component pattern by being able to configure something like $routeProvider.when('/', {resolve: {items: ($http) => $http.get(...)}, template: '<my-app items="resolve.items" />'}); and not having to configure a dummy controller that puts the resolved values on the scope.

@shahata
Copy link
Contributor Author

shahata commented Nov 27, 2015

In a nutshell I tried to write a new application with using only module.component and avoiding standalone controllers and ended up writing something like:

$routeProvider.when('/', {
  template: '<my-app items="resolve.items" />',
  resolve: {items: ($http) => $http.get()},
  controllerAs: 'resolve',
  controller: function (items) { this.items = items; }
});

It is pretty ugly that I need to define the controller only so I can get the resolve results in the template...
With this PR I can do this instead:

$routeProvider.when('/', {
  template: '<my-app items="resolve.items" />',
  resolve: {items: ($http) => $http.get()}
});

@@ -120,6 +120,26 @@ describe('ngView', function() {
});


it('should use default resolve controller when resolve map defined with no controller', function() {
Copy link
Member

Choose a reason for hiding this comment

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

The test description is kibd of undescriptive (I wouldn't have guessed what's supposed to do just by reading the description).

@shahata shahata changed the title feat(ngRoute): provide default resolve controller feat(ngView): reference resolved locals in scope Nov 28, 2015
@petebacondarwin
Copy link
Contributor

How about a resolvesAs property to be consistent with controllerAs when attaching things to the scope?
Then module.component could default to $resolves...

@shahata
Copy link
Contributor Author

shahata commented Nov 30, 2015

@petebacondarwin sure, that makes sense. I'll update the PR. Do you prefer resolvesAs over resolveAs? For me the second sounds better, but maybe that's my broken English :)

@petebacondarwin
Copy link
Contributor

Right, so the property that we are aliasing is resolve right? So the property that specifies what this alias should be called is resolveAs.
You're right.

@shahata
Copy link
Contributor Author

shahata commented Dec 1, 2015

Added resolveAs. @petebacondarwin do you think we should consider this change for 1.5? Using angular-route with components is very strange otherwise...

this will make it easier to use ng-route with component pattern by being able to configure something like `$routeProvider.when('/', {resolve: {items: ($http) => $http.get(...)}, template: '<my-app items="$resolve.items" />'});` and not having to configure a dummy controller that puts the resolved values on the scope.
@shahata
Copy link
Contributor Author

shahata commented Dec 1, 2015

oh, I read your original comment again. You mean that this functionality will work only if the route has resolveAs and that module.component will pass resolveAs: '$resolve'? I'm not sure that this is possible since resolveAs is a property of the route, not of the component...

@petebacondarwin
Copy link
Contributor

I didn't realise that this had nothing to do with module.component.

How about:

$routeProvider.when('/', {
  template: '<my-app items="myResolve.items" />',
  resolve: {items: ($http) => $http.get(…)}
  controllerAs: 'myResolve'
});

Then that would mean that a default controller is created that contains all the resolves as properties.
This would not work if a controller was actually specified. In otherwords:

$routeProvider.when('/', {
  template: '<my-app items="$resolve.items" />',
  resolve: {items: ($http) => $http.get(…)}
  controllerAs: '$resolve',
  controller: function() { ... }
});

In this second case the resolves would not be bound to the specified controller.

Then the most simple case is that neither controller nor controllerAs are provided, in which case controllerAs defaults to $resolve and the controller defaults to one that contains the resolves:

$routeProvider.when('/', {
  template: '<my-app items="$resolve.items" />',
  resolve: {items: ($http) => $http.get(…)}
});

@petebacondarwin
Copy link
Contributor

Actually looking at the actual code of the PR - I think that this is good as it is. Let's merge!

@gkalpak
Copy link
Member

gkalpak commented Dec 7, 2015

There should be docs for this feature right ?

@petebacondarwin
Copy link
Contributor

Agh! Good point @gkalpak

gkalpak added a commit to gkalpak/angular.js that referenced this pull request Dec 10, 2015
gkalpak added a commit to gkalpak/angular.js that referenced this pull request Dec 10, 2015
gkalpak added a commit to gkalpak/angular.js that referenced this pull request Dec 10, 2015
gkalpak added a commit to gkalpak/angular.js that referenced this pull request Dec 11, 2015
gkalpak added a commit that referenced this pull request Dec 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants