-
Notifications
You must be signed in to change notification settings - Fork 27.4k
[TO DISCUSS] Provide a method to get a list of all defined controllers #13951
Comments
Thanks for the detailed writeup. What I wonder: Adding a |
Well, first I think that if so many people have viewed that question it means that it's not just for my specific use but because there is a greater need. I just gave one example of use. And yes. We don't want to check inside the same controller whether it is client A, B or C. Because we could have hundreds of them and the code would become unmaintainable. So we need to separate those. A |
What's the difference between checking in the controller or in the stateProvider which functions are needed? Why does checking inside the controller become unmaintainable while checking before the controller instantiated does not? In my mind it's just a minimal difference. |
Because when you do it in the routes, it's just a wrapper. Each controller can have its separated logic, display different forms, inputs, data. Make different ajax call by calling different services. If you call one controller only which does all the checking, it may be fine for simple scenarii. But it's gonna get overcomplicated as soon as you have to deal with different logic and so on. There will be a lot of And, using a wrapper, you can still use inheritance between controllers if you want, and share some logic the way you want it. It's powerful. |
In the scenario you describe, there will not be just different controllers (or at least there shouldn't), but there will also be different templates and possibly different services or service wrappers (if not filters, directives etc). I would bet there are much better (and more robust) ways to solve this.
|
There are indeed different templates, the same way there are different controllers; But I don't see how using a different module would allow us to fallback to the generic module when there isn't anything specific to do. There are a lot of generic code and we won't override everything, so we need to fallback most of the time. |
You can always load the generic module (containing the fallback stuff) and loading the user-specific module (which overwrites/decorates the needed components) in certain cases. |
Anyway, that's not really the point of this question in the first place. Is it? |
Although it's very easy to add and will have minimal overhead, I don't feel it promotes any good practices, so I wouldn't be too keen on adding it. |
@Vadorequest - is it your requirement that in some application for, say ClientA, that whenever you ask for some controller, say Are there instances where you would want to still get the original |
@petebacondarwin Yes we do. Because the Controller |
That is a shame because I was really keen on the idea of simply providing an extra module that overrode the generic controllers. |
I guess that, given we have an |
@Vadorequest feel free to submit a PR. |
Duly noted! |
…ss, to check the existence of a given controller. Fixes angular#13951
…ss, to check the existence of a given controller. Fixes angular#13951
Problem
There is no way to detect whether a controller has been defined or not without instanciate it, which isn't a good solution. See discussion on SO: https://stackoverflow.com/questions/19734565/how-to-check-if-an-angularjs-controller-has-been-defined
Use case
Say that we have an angular app which is a generic product which also deals with specific content based on a customer. Say that we have generic controllers that are use by default, except when a specific controller with the customer suffix exists and we want to use that controller, but only if it exists. Otherwise we would use the generic controller. (Basically a controller fallback)
In this case we need to check if the specific controller exists and if it doesn't then use the generic one.
Example
The
resolveClientCtrl
method would check whether or not a specific controller exists or use the provided controller. (this is an example of use, for a specific need)Proposition 1
Angular already has a list of controllers used internally which aren't shared out of its scope. What we could do is simply add a method that provides the list of defined controller as an array of strings.
A solution close to this has been proposed here on SO: http://stackoverflow.com/a/19736852/2391795
Proposition 2
We could also just add a method which doesn't provide the list of controllers but check whether a controller exists or not.
This has been asked two years ago and has more than 5k views in one SO question. I believe this should be in angular "core".
If one of those propositions is accepted (or both) then I'll make a PR.
The text was updated successfully, but these errors were encountered: