-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Recommend against controllerAs vm #462
Comments
vm stands for ViewModel and imho is a far better name than ctrl, which stands for controller. Yes I know that Angular had chosen the name Controller, but they are used as a ViewModel, hence they are a model for your view. Many people (including experts) have worked more with controllers than with viewmodels, that's why they prefer controller, which I think is wrong. ViewModel is just so much more flexible and self contained. About the use of |
Searchability is a real problem though - when searching for what template the line is in, you get potentially a huge number of matches. Almost every developer I have spoken with recommends against this, including maintainers for some of the most used libraries in the Angular ecosystem (as well as myself). This pattern has also caused a lot of headaches for others in the community when helping developers who are new to Angular, increasing the confusion with terms to grapple for those in need of help. |
function VideoListCtrl() {
var vm = this;
vm.list = [];
} <div ng-controller="VideoListCtrl as video_list_ctrl">
<ul>
<li ng-repeat="video in video_list_ctrl.video_list">{{video.title}}</li>
</ul>
</div> As far as var I prefer the controllerAs syntax as it's a much cleaner implementation than throwing $scope around. The nice thing is that the option is there. It's up to the developer to determine which way to go. |
In HTML ... It can be whatever you want to call it. I start with In the JavaScript, I only do closing this ... had this chat a few times :) good conversation tho! |
The problem I have is that as it is currently recommended in the styleguide, it does not make this distinction with the templates and it is showing negative consequences across the help channels in the ecosystem, including productivity loss. I agree with the controller part in JavaScript ( |
Further complicating matters is that most new comers can't make that distinction between when to just use "vm" and when to have a different name spacing. So, while we're sitting in IRC/Slack helping people we're seeing that virtually all of their controller's are name spaced with "vm". It's not an ideal convention; I think we can do better as a community. |
+1 |
1 similar comment
+1 |
I ran into this myself when first adopting this styleguide. It took significant time to go back and clean up a whole mess of |
many controllers named This guide is not intended to explain Angular. That's what the docs do. There is material that explains controller as. There are blog posts (one which I wrote). @BobbieBarker "Further complicating matters is that most new comers can't make that distinction between when to just use "vm" and when to have a different name spacing." They dont have to. You can use vm everywhere if you want. If its confusing, then you use a new name. I think that's a simple rule. Stay simple, til its not simple. To be clear, I'm only talking about the vm in the view ... and my guidelines are
|
The point is that simply using vm in the view without context is precisely what causes confusion - you, yourself admitted that adding more context to vm such as customerVm is better. There are whole teams at companies that take the guide at face value, and not adding that context to vm is what does a disservice. It simply is not legible or maintainable. My main request is that this needs to be codified into the guide itself. |
IMO - I am against using vm only because I feel it conflicts with the "Why AngularJS?" section at angularjs.org; specifically the last two sentences shown below.
I feel creating a naming convention from the start that is expressive and readable, e.g., 'customerVm and orderVm, is a best practice. Less chance of finding yourself in the situation @zachlysobey described. |
@wesleycho I did not say customerVm is better. I said if vm is not clear to you, make it customerVm. The guide is a guide. Most folks take it and adjust as needed. Absolutely feel free to adjust and name with long names if you like. Have a PR that shows balance? sure, I'll review it. I've also seen many apps where people go nuts on names with The guide starts with |
Makes sense @johnpapa |
This is fallacious reasoning - just because some people abuse naming with lengthy names, that going the opposite extreme is what is needed. I can tell you from having helped and mentored many people in the community, and as well as talking to others who are considered experts in the community as well, the current claim that it is simpler is simply not the case. On the contrary, I have seen near unanimous rejection of this. For a supposed style guide, this is just flat out wrong not to elaborate here in the guide itself when the guide elaborates pretty much everything else in detail. It is incongruous, has demonstrable side-effects, and frankly, avoids addressing the argument for why to include the advice from the guide itself. This response is not a strong justification as to why not amend the guide. |
You are confirming my point. Neither extreme is best. Common sense prevails and help make clarity in our code. I never said it wont be elaborated. I said make a PR. I've been parts of quite a few apps. I'll say it again ... make a PR that is balanced and it will be considered, just like the other 100 contributors have done.
I'm not saying it won't be changed. I've said the opposite ... make a PR that is balanced and it will be considered. There's nothing more to say |
I missed the PR bit, I apologize - but I will say that closing the issue prematurely then was not the right move. The closed issue gave me the impression that this wouldn't be considered (currently juggling drama that suddenly spawned in UI Bootstrap in the past couple of hours, so apologies if my wording and clarity are not quite there currently). |
everything is considered, as long as it is an angular style and follows the what, why, and how (and how not sometimes). and in this case a when would be good. |
Also i think the third bullet point helps cover the fact that you *can* use multiple names johnpapa#577 johnpapa#462
There has been talk over the past few months about the
vm
pattern -var ctrl = this
seems to almost unilaterally be preferred by all of the experts I have talked to (& by myself), but this is minor.The problematic recommendations are
controllerAs: vm
andFoo as vm
- this is less searchable, as well as obfuscating the name. For example,Auth as auth
would allow the user to describe in the template more appropriately, such as<form ng-submit="auth.login()">
- human readability is much better, as well as searchability due to being able to quickly identify the template of interest. Doinggrep -r 'auth' src/
is sure to return much more relevant results thangrep -r 'vm' src/
, and similarly for any other project-wide searches.The text was updated successfully, but these errors were encountered: