-
Notifications
You must be signed in to change notification settings - Fork 3k
resolve value as string? #2047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can do: $stateProvider.state('customers.show', {
url: '/customers/:id',
template: template,
controller: MyCtrl,
resolve: MyCtrl.resolve
}); How you choose to get the |
@allenhwkim I had this same issue and I ended up creating a provider with a method, rather than trying to take IIFEs off my controllers. For example, I was trying to load a document from an API before the state got loaded.
|
Honestly, I do not agree with this approach at all now (in the ng1 context) so I'm going to lock this thread. When you define a state you can (and will) define multiple controllers (for each view) but you can only define one resolve. For this reason alone thinking of resolves as 1-1 with controllers is a bad idea. You may need a few resolves for one controller and more resolves for a second controller and perhaps even more resolves for a controller in a substate. Instead, try not to think of the controller as the important information holder in your app. Controllers should be simple, dumb and cheap, like if you need just one object for the nav view but three objects for the main view, those controllers should be 1 and 3 lines of code respectively. |
Well said proloser. |
@ProLoser You make a valid point...however..consider this scenario (which I'm currently working on in a project):
Am I approaching this issue the wrong way? This seems like it could be a pretty frequent use case. |
Honestly, if you were to say "Okay, i don't want to load this data over and over" then I'd propose caching it in your services that retrieve the data or moving some of the data being resolved up the state tree to where you feel it'd be more appropriate (which further illustrates my point that you should not tie your resolve definitions so closely to controllers). Another point that occurs to me is that 1 resolved resource will actually be used in MULTIPLE controllers, so again it doesn't make sense to directly associate resolve definitions with any specific controller. @jongunter the points you bring up confuse me a bit because I don't really see how they are particularly relevant to this issue. See point 2 if you have performance issues, this discussion was about code organization. |
@jongunter as a side note, I think your code sample over-complicates things (I hate messing with providers in my app) and I don't see what benefit it provides. I also don't really get what it does and don't think it works the way you think it's working. |
The below question is exact the same as stackoverflow
With ui-router, I add all resolve logic in state function like this;
However IMO,
resolve
object must belong to a controller, and it's easy to read and maintain if it is defined within a controller file.However, When I define it as
MyCtrl.resolve
, because of IIFE, I get the following error.When I define that one as string
'MyCtrl.resolve'
, I get thisI see that controller is defined as string, so I think it's also possible to provide the value as string by using a decorator or something.
Has anyone done this approach? So that I can keep my routings.js clean and putting relevant info. in a relevant file?
The text was updated successfully, but these errors were encountered: