• Stars
    star
    268
  • Rank 153,144 (Top 4 %)
  • Language
  • License
    GNU General Publi...
  • Created about 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Thinking in Components

angularjs-structure-styleguide

Basic, personal, opinionated style on structuring your AngularJS, adhering to John Papa's LIFT principle.

What is LIFT?

  • Locating our code is easy
  • Identify code at a glance
  • Flat structure as long as we can
  • Try to stay DRY (Don’t Repeat Yourself) or T-DRY

Unmaintained

This style guide is no longer maintained.

Table of Contents

  1. Context
  2. Overview
  3. App
  4. Core
  5. Components
  6. Overall

1. Context

This was created to offer a more in-depth guide for AngularJS developers that want their apps to scale, maintainable, and whatever

This guide gives emphasis to components or directives inspired by ReactJS and Flux. For more information in regards to this emphasis, check this article I wrote on Medium.

Please, feel free to diverge from this guide.

Back to top

2. Overview

Basically, this is how our app will be.

First-level simplification

β”œβ”€β”€ app/
β”œβ”€β”€ components/
β”œβ”€β”€ core/
β”œβ”€β”€ dist/
β”œβ”€β”€ less|sass/
β”œβ”€β”€ vendor/

Extended

β”œβ”€β”€ app/
|  β”œβ”€β”€ user
|  |  β”œβ”€β”€ partials/
|  |  β”œβ”€β”€ components/
|  |  |  β”œβ”€β”€ ThatDirective/
|  |  |  |  β”œβ”€β”€ i18n/
|  |  |  |  β”œβ”€β”€ tests/
|  β”œβ”€β”€ user.create/
|  β”œβ”€β”€ user.edit/
|  β”œβ”€β”€ newsCategory/
|  β”œβ”€β”€ newsCategory.create/
|  β”œβ”€β”€ newsCategory.edit/
β”œβ”€β”€ components/
β”œβ”€β”€ core/
|  β”œβ”€β”€ constants/
|  β”œβ”€β”€ filters/
|  β”œβ”€β”€ resources/
|  β”œβ”€β”€ services/
|  β”œβ”€β”€ utils/
|  β”œβ”€β”€ app.js
|  β”œβ”€β”€ bootstrap.js
|  β”œβ”€β”€ constants.module.js
|  β”œβ”€β”€ filters.module.js
|  β”œβ”€β”€ components.module.js
|  β”œβ”€β”€ resources.module.js
|  β”œβ”€β”€ services.module.js
β”œβ”€β”€ dist/
|  β”œβ”€β”€ css/
|  β”œβ”€β”€ js/
|  β”œβ”€β”€ images/
|  β”œβ”€β”€ views/
β”œβ”€β”€ less|sass/
β”œβ”€β”€ vendor/

Back to top

3. App

The app folder contains all the app's states. And each state may contain the controllers, directives, services to be specifically used for itself or child states.

β”œβ”€β”€ app/
|  β”œβ”€β”€ user/
|  |  β”œβ”€β”€ i18n/
|  |  |  β”œβ”€β”€ en.js
|  |  |  β”œβ”€β”€ kr.js
|  |  β”œβ”€β”€ components/
|  |  β”œβ”€β”€ tests/
|  |  β”œβ”€β”€ partials/
|  |  β”œβ”€β”€ user.state.js
|  |  β”œβ”€β”€ user.controller.js
|  |  β”œβ”€β”€ user.html
β”œβ”€β”€ ...

Q: What is a state?

Used to register a state, see ui-router. This also has been asked before, see this issue.

Q: What if I started having more than 1 partial, controllers, and other things?

Stop using multiple controllers, partials, services. It is more recommended to use directives for the sake of modularity and maintainability. It is only okay to use partials for chunk of non-functioning mark-up.

β”œβ”€β”€ app/
|  β”œβ”€β”€ user-profile/
|  |  β”œβ”€β”€ components/
|  |  |  β”œβ”€β”€ Avatar/
|  |  |  |  β”œβ”€β”€ Uploader.js
|  |  |  |  β”œβ”€β”€ Webcam/
|  |  |  |  |   β”œβ”€β”€ Webcam.js
|  |  |  |  |   β”œβ”€β”€ WebcamShootButton.js/
|  |  β”œβ”€β”€ user.state.js
|  |  β”œβ”€β”€ user.controller.js
|  |  β”œβ”€β”€ user.html
β”œβ”€β”€ ...

Q: What if I have nested states?

As much as possible, try to avoid nested directories of states. For example, we have this state hierarchy:

- main
  - user
    - user.create
    - user.edit
    - user.delete
    - profile
      - profile.create
      - profile.edit
      - profile.delete

This is how we structure our directory. Why? This way, nested states can be easily found and understood while adhering to the LIFT principle.

β”œβ”€β”€ app/
|  β”œβ”€β”€ news/
|  β”œβ”€β”€ news.create/
|  β”œβ”€β”€ news-category/
|  β”œβ”€β”€ news-category.edit/

Q: How do I indicate a url nest or a state nest?

Use .(dot) for states of the same module. For instance, if we have the users CRUD module, then we'd have users.create, users.edit, ...).

Otherwise, use - to signify hierarchy. For instance, we have a group module which is under the users module, then we'd have: (users, users.create, ..., users-groups, users-groups.create, ..).

To elaborate, here's an example: an app consists of a news CRUD [url: /news/*] and a news category CRUD [url: /news/categories/*]. We expect it to have these states:

news (abstract state)
news.index
news.create
news.edit

news-category (abstract state)
news-category.index
news-category.create
news-category.edit

This is how we create our directory:

β”œβ”€β”€ app/
|  β”œβ”€β”€ news/
|  β”œβ”€β”€ news.index/
|  β”œβ”€β”€ news.create/
|  β”œβ”€β”€ news.edit/
|  β”œβ”€β”€ news.category/
|  β”œβ”€β”€ news-category.index/
|  β”œβ”€β”€ news-category.create/
|  β”œβ”€β”€ news-category.edit/

Q: What if my state is composed of two words?

Use camelCase; do not separate it with a -(dash). Do not use -(dash) to signify that a name consists of two words. Use it to signify a nest or hierarchy. If a state consists of two separate words (e.g, soulja boy, sticky nav), use camelCase. For example, souljaBoy, stickyNav.**.

Why? This allows us to properly signify and understand what a dot (.) and dash (-) does.

β”œβ”€β”€ user/
β”œβ”€β”€ user-awesomeName/
β”œβ”€β”€ user-anotherModule/
β”œβ”€β”€ user-maybeAnotherModule/
β”œβ”€β”€ user-thatModule/
β”œβ”€β”€ user-thatModule.index/
β”œβ”€β”€ user-thatModule.create/

Q: Where do I put my tests or i18n?

Tests and i18n should be put close to our components or state as possible.

Why? This avoids the replication of our structure for our tests; and, makes them easier to view.

Q: How do I handle each language for the i18n?

The filename of each i18n should signify only the language it is supposed to handle. If a component only has one i18n file, simply put it at the same directory it will be used with.

|  β”œβ”€β”€ user
|  |  β”œβ”€β”€ en.js
|  |  β”œβ”€β”€ user.controller.js
|  |  β”œβ”€β”€ user.state.js
|  |  β”œβ”€β”€ ...

Back to top

4. Core

The core folder contains all modules, app bootstrapper (see angular.bootstrap), and all common or shared files (in short, non-specific components) used in the app such as services, constants, controllers, resources, utils, and filters.

β”œβ”€β”€ core/
|  β”œβ”€β”€ constants/
|  β”œβ”€β”€ filters/
|  β”œβ”€β”€ resources/
|  β”œβ”€β”€ services/
|  β”œβ”€β”€ utils/
|  |   β”œβ”€β”€ progress.config.js/
|  |   β”œβ”€β”€ restangular.config.js/
|  β”œβ”€β”€ app.js
|  β”œβ”€β”€ bootstrap.js
|  β”œβ”€β”€ constants.module.js
|  β”œβ”€β”€ components.module.js
|  β”œβ”€β”€ filters.module.js
|  β”œβ”€β”€ resources.module.js
|  β”œβ”€β”€ services.module.js

Q: What does each of the js file in the root of the core folder do?

  • app.js is our application module, the highest-level module, which contains all other modules. For instance, angular.module('app', ['app.resources', 'app.directives', 'app.services', 'app.constants']); .
  • bootstrap.js does nothing but bootstrap the app angular.bootstrap(document, ['app']).
  • *.module.js is the module to be respectively used for each (all constants in the constants/ directory should use constant.module.js).

Q: What are resources?

These come in handy when you frequently request an API for data. Fill in resources with your $http-service-wrappers to wrap $http or Restangular methods, or store data from a server response. Otherwise, put in the service as a normal service.

5. Components

The components folder contains mostly general-solution|non-feature-specific directives.

β”œβ”€β”€ components/
|  β”œβ”€β”€  StickyNavThatDoesThat/
|  |  β”œβ”€β”€ tests/
|  |  β”œβ”€β”€ StickyNavThatDoesThat.spec.js
|  |  |  β”œβ”€β”€ Hamburger.spec.js
|  |  |  β”œβ”€β”€ Search.spec.js
|  |  β”œβ”€β”€ i18n/
|  |  |  β”œβ”€β”€ en.js
|  |  |  β”œβ”€β”€ jp.js
|  |  |  β”œβ”€β”€ kr.js
|  |  β”œβ”€β”€ tests/
|  |  β”œβ”€β”€ StickyNavThatDoesThat.js
|  |  β”œβ”€β”€ Hamburger.js
|  |  β”œβ”€β”€ Search.js
|  β”œβ”€β”€ AwesomeProgressBar/
|  |  β”œβ”€β”€ i18n/
|  |  β”œβ”€β”€ tests/
|  |  β”œβ”€β”€ AwesomeProgressBar.js
|  β”œβ”€β”€ components.module.js

Q: Why is this named as components not directives?

It took me a long while to decide whether how it should be named. After some time, I preferred to use components because our directives are actually being used as components. Again, your preference.

Q: Why are the directives separated from other angular modules?

I had decided to separate directives because:

  1. Directives are way too large to be nested inside the core folder.
  2. They need emphasis and will contain mostly a large part of your app

Write the file names of your directives (components) in StudlyCase.

Why? To emphasize components. In the future, I might tweak a huge part of the guide to StudyCase. camelCase does not seem to give proper emphasis to itself, so.

Back to top

6. Overall

Do not forget that this is a personal, opinionated structure styleguide. Although I have been using an almost-similar structure in production, your structure will vary on your project (team size, etc) from time-to-time. Make sure to keep it simple.

It is always better to create feature-based instead of role-based structure. Because, based on my experience, role-based structure will always be harder to write, read, and maintain.

Like I said in the Context, please feel free to diverge.

Back to top

Acknowledgement

angularjs-structure-styleguide Β© 2014, Kier Borromeo (srph). Released under the MIT License.

srph.github.io Β Β·Β  GitHub @srph Β Β·Β  Twitter @_srph

This style guide has been largely influenced by John Papa's AngularJS Style Guide (which also includes a lecture with regards to an AngularJS app structure).

Contribution

I'd suggest to issue a proposal or a question first (for discussion purposes) before submitting a pull request. This avoids unrewarded / unmerged pull requests (if ever).

If you have any questions, issues, or whatever, feel free to send me a tweet on twitter, or just submit an issue.

More Repositories

1

gh-polls-web

Web front-end for GitHub Polls.
Vue
90
star
2

npm-scripts-info

Display the description of your npm scripts
JavaScript
89
star
3

careflix

A personal Netflix clone with sync-play, invitation system, and chatting features.
PHP
38
star
4

axios-response-logger

πŸ“£ Axios interceptor which logs responses
JavaScript
25
star
5

react-link-state

A helper util to use LinkedStateMixin for ES6 React Components
JavaScript
23
star
6

jumars-tindahan

πŸͺ A sample cart app written using Alt and friends.
JavaScript
8
star
7

e-dental

πŸ₯ IT22-FA3 EDMS, built with Laravel
PHP
6
star
8

pulse

Pulse is an annual bullet journal that lets you keep track of your goals.
TypeScript
5
star
9

toy-netflix-carousel

Coding exercise implementing Netflix's hover effect trademark on its carousel
TypeScript
5
star
10

remax

An opinionated yet dead-simple non-universal React+Redux starter kit.
JavaScript
5
star
11

jqt

A low-level jQuery plugin for node hiding and showing through css transitions
JavaScript
5
star
12

cookie-machine

πŸͺ A universal/isomorphic cookie library
JavaScript
5
star
13

toy-template-builder

An example drag-n-drop template builder built with React.
TypeScript
5
star
14

angular-timestamp-filter

πŸ“† AngularJS filter which converts date to timestamp
JavaScript
4
star
15

scrollbar-compensate

A hack to compensate for scrollbar for modals
JavaScript
4
star
16

m

(n.) minimalistic stylesheets
CSS
4
star
17

pldtfibr-wifi-password

πŸ‘€ A mac-address reverser to obtain the default password of a PLDTHOMEFIBR Wifi.
JavaScript
4
star
18

angular-infinite-scroll

A simple infinite scroll solution for AngularJS
JavaScript
4
star
19

smarf

Dota hero organizer on your browser
PHP
3
star
20

react-uploadi

Build upload UIs with dropzone and previews out of the box.
JavaScript
3
star
21

dotfiles

JavaScript
3
star
22

dream-rose

The web app for Dream ROSE Online
PHP
3
star
23

til

🍨 A swaggy top of daily thoughts
Shell
3
star
24

angular-xhr

[DEPRECATED] Send $http requests with directives.
JavaScript
3
star
25

react-timesheet

[WIP] An opinionated timesheet component for React
JavaScript
3
star
26

djiyo

A Digital Ocean-esque CSS Starter Kit
CSS
3
star
27

bookmarks

πŸ”– List for whatever.
2
star
28

pitg

[DROPPED] - A stackoverflow-style commuinity board
PHP
2
star
29

dota2-responses-slack-bot

A Slack bot referencing Dota 2 responses to appropriate chat messages.
JavaScript
2
star
30

pasei

A dissertation grading application for Pasay High School
PHP
2
star
31

axios-base-url

πŸ”© An axios interceptor that allows you to set a base url for all requests
JavaScript
2
star
32

jquery-element-scroll

[DEPRECATED] A basic jquery plugin that allows you to smooth scroll to the preceding / next element
JavaScript
2
star
33

react-confirm

Replace native `confirm` function with yer modals.
JavaScript
2
star
34

js-str-concat

[DEPRECATED] Conditionally concatenate strings
JavaScript
2
star
35

react-infinite-scroll

A simple infinite scroll React.js component
JavaScript
2
star
36

autodrive

Generate direct-download links from a Google Drive URL.
TypeScript
2
star
37

toy-range-slider

An example horizontal range slider in React
TypeScript
1
star
38

toy-react-dnd-listly

A Trello-like UI implemented in React DnD
JavaScript
1
star
39

laravel-antispam

An antispam quiz package for Laravel.
PHP
1
star
40

toy-vue-serve-hey

A survey builder built with Vue.js
Vue
1
star
41

srph.github.io

A personal GH page
CSS
1
star
42

toy-react-tag-input

πŸ’© Attempt to make a "tagging" input on ReactJS.
JavaScript
1
star
43

klass

βœ’οΈ Create ES5 JavaScript classes declaratively
JavaScript
1
star
44

techbrew

Website for Tech Brew Labs
JavaScript
1
star
45

yehey

[WIP] Static assets versioning
JavaScript
1
star
46

react-hoc-compose

Compose factory components on the go for incredibly lazy people
JavaScript
1
star
47

toy-fade-carousel

Fade carousel built with React
1
star
48

boomebang

Instagram Boomerang for the web.
TypeScript
1
star
49

react-notification

Primitives to build your growls and dreams
JavaScript
1
star
50

cinemapogi

A fresh-looking viewer for Resorts World Manila Blockbusterseats
TypeScript
1
star
51

dotenv-autoload

πŸ‘€ Autoload dotenv through recursive finding
JavaScript
1
star
52

2022

SHIP. IT.
TypeScript
1
star
53

node-stringify-object-values

Stringify an object's values
JavaScript
1
star
54

habits

Getting back on track
TypeScript
1
star
55

keplr-suggest-chain-prompt-bug

A repo to reproduce an issue with Keplr's suggest chain feature
JavaScript
1
star
56

angular-flash

An AngularJS module that allows you to flash a notification to the DOM
JavaScript
1
star
57

website

1
star
58

taima

[WIP] A productivity timer built with React and Electron
JavaScript
1
star
59

parcelwind

Boilerplate for Parcel and Tailwind
HTML
1
star
60

aoc-2022

JavaScript
1
star
61

miniventory

Mini inventory management system for Kier and Joyce's store
TypeScript
1
star
62

chunkr

Make long numbers readable
TypeScript
1
star
63

issue-evmos-ibc

Debugging ibc transactions to evmos
TypeScript
1
star
64

websait

Upcoming personal websait.
CSS
1
star