javascript Is there a in-depth code guide to React JS similar to John Papa’s code guide to Angular JS?
Posted on 8 noviembre, 2019
Contents
I’m pretty new to Angular and I’d like to learn good practices from the beginning. I’ve come across three Angular style guides and they all seem to be great. I’d like to know which one would be best for me to adopt, but I’m in no position to be the judge on that, since I have little experience with big Angular apps. Use JSCS for checking your coding styles your JavaScript and be sure to customize the JSCS options file and include in source control. See the JSCS docs for details on the options.
Your threshold may be different, so adjust as needed. Separate route configuration into its own file. Examples might be app.route.js for the main module and admin.route.js for the admin module.
If planning to produce documentation, use jsDoc syntax to document function names, description, params and returns. Use @namespace and @memberOf to match your app structure. Create an application root module whose role is to pull together all of the modules and features of your application. Refactor logic for making data operations and interacting with data to a factory. Make data services responsible for XHR calls, local storage, stashing in memory, or any other data operations. The following example defines the app module and its dependencies, defines a controller, and defines a factory all in the same file.
Even in smaller apps I prefer this separation from the rest of the configuration. When a controller must be paired with a view and either component may be re-used by other controllers or views, define controllers along with their routes. Notice that the important stuff is scattered in the preceding example. In the example below, notice that the important stuff is up top. For example, the members bound to the controller such as vm.avengers and vm.title.
Keep the App Module Thin
The previous version of this style guide, which can be found here. This guide is not intended to serve as a demo of reusable code, code snippets, or advanced solutions though it may drop a few here or there. Rather it is intended to be a starting point for a team looking for consistency. Demonstrates use of grunt and gulp with an angular app. The dev world is rich, fascinating and helpful. There’s reddit, discord, slack, github, gitter, irc etc etc etc etc.
While this guide explains the what, why and how, I find it helpful to see them in practice. This guide is accompanied by a sample application that follows these styles and patterns. You can find the sample application here in the modular folder.
Identify
Use function declarations to hide implementation details. Keep your accessible members of the factory up top. Point those to function declarations that appears later in the file. I find that 10 Awesome Kid-Friendly YouTube Channels for Kids Interested in Coding the Angular community is an incredible group who are passionate about sharing experiences. As such, Angular expert Todd Motto and I have collaborated on many styles and conventions.
Use a route resolve when you want to decide to cancel the route before ever transitioning to the View. When creating a directive that makes sense as a stand-alone element, allow restrict E and optionally restrict A . Generally, if it could be its own control, E is appropriate. General guideline is allow EA but lean towards implementing as an element when it’s stand-alone and as an attribute when it enhances its existing DOM element.
The implementation details are down below. If you have questions with the guide, feel free to leave them as issues in the repo. The idea is to keep the content up to date and use github’s native feature to help tell the story with issues and PR’s, which are all searchable via google. Because odds are if you have a questions, someone else does too!
- Use a route resolve when you want to decide to cancel the route before ever transitioning to the View.
- I’ve come across three Angular style guides and they all seem to be great.
- Unit testing helps maintain clean code, as such I included some of my recommendations for unit testing foundations with links for more information.
- Because odds are if you have a question, someone else does too!
I encourage you to check out Todd’s guidelines to get a sense for his approach and how it compares. I find that the AngularJS community is an incredible group who are passionate about sharing experiences. As such, a friend and AngularJS expert Todd Motto and I have collaborated on many styles and conventions. John papa provides the best file structure according to me and also many useful frameworks like YEOMAN follows john papa sort of app structure.
Factory and Service Names
Ideal candidates include providers and constants. Structure your app such that you can Locate your code quickly, Identify the code at a glance, keep the Flattest structure you can, and Try to stay DRY. The structure should follow these 4 basic guidelines. Use $inject to manually identify your dependencies for Angular components. Use controllerAs syntax with a directive to be consistent with using controller as with view and controller pairings.
I’m also working on a Pluralsight course titled Angular 2 First Look due out in early March. Of course, I’ll be teaching the main concepts too. You signed in with another tab or window. There are multiple versions of Angular, and thus there are multiple versions of the guide. This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use file templates or snippets to help follow consistent styles and patterns. Here are templates and/or snippets for some of the web development editors and IDEs. Use JS Hint for linting your JavaScript and be sure to customize the JS Hint options file and include in source control. See the JS Hint docs for details on the options. Inject code into module configuration that must be configured before running the angular app.
These may include a shell view and controller may act as the container for the app, navigation, menus, content areas, and other regions. When there are multiple modules, the main module file is named app.module.js while other dependent modules are named after what they represent. For example, an admin module is named admin.module.js. The respective registered module names would be app and admin. If you need to conditionally cancel a route before the controller is activated, use a route resolver.
Small Functions
Declare modules without a variable using the setter syntax. Use unique naming conventions with separators for sub-modules. My friend Ward has certainly helped influence the ultimate evolution of this guide. Angular Stash is a collection of hand-picked resources and our own articles and interviews with Angular developers. Until then, here is a quick glimpse at a project structure that I have been working with.
If you have questions with the guide, feel free to leave them as issues in the repository. If you find a typo, create a pull request. The idea is to keep the content up to date and use github’s native feature to help tell the story with issues and PR’s, which are all searchable via google. Because odds are if you have a question, someone else does too!
Manipulate DOM in a Directive
It because approaches to working with the angular and Style Guides exist as much as there is the specialists. Define routes for views in the module where they exist. Each module should contain the routes for the views in the module. Use subtle animations with Angular to transition between states for views and primary visual elements.
Unit testing helps maintain clean code, as such I included some of my recommendations for unit testing foundations with links for more information. Keep a flat folder structure as long as possible. When you get to 7+ files, begin considering separation. The following code is not using minification safe dependencies. Avoid using the shortcut syntax of declaring dependencies without using a minification-safe approach.
I tried to share my reasons in this document to shed some light on how I think about the challenges and solutions. This has always helped me, and I hope it helps you. These styles are based on my development experience with Angular, https://forexaggregator.com/ presentations, Pluralsight training courses and working in teams. Have a near term view of implementation and a long term vision. In other words, start small but keep in mind on where the app is heading down the road.
Factories should have a single responsibility, that is encapsulated by its context. Once a factory begins to exceed that singular purpose, a new factory should be created. Define a controller for a view, and try not to reuse the controller for other views. Instead, move reusable logic to factories and keep the controller simple and focused on its view. Defer logic in a controller by delegating to services and factories.
Got something to say?