• Stars
    star
    268
  • Rank 148,256 (Top 3 %)
  • Language
  • License
    GNU General Publi...
  • Created over 9 years ago
  • Updated almost 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

toy-netflix-carousel

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

pulse

Pulse is an annual bullet journal that lets you keep track of your goals.
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

pldtfibr-wifi-password

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

scrollbar-compensate

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

m

(n.) minimalistic stylesheets
CSS
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

react-infinite-scroll

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

pitg

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

dota2-responses-slack-bot

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

pasei

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

axios-base-url

๐Ÿ”ฉ An axios interceptor that allows you to set a base url for all requests
JavaScript
2
star
33

jquery-element-scroll

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

react-confirm

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

js-str-concat

[DEPRECATED] Conditionally concatenate strings
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

toy-vue-serve-hey

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

laravel-antispam

An antispam quiz package for Laravel.
PHP
1
star
41

srph.github.io

A personal GH page
CSS
1
star
42

klass

โœ’๏ธ Create ES5 JavaScript classes declaratively
JavaScript
1
star
43

techbrew

Website for Tech Brew Labs
JavaScript
1
star
44

toy-react-tag-input

๐Ÿ’ฉ Attempt to make a "tagging" input on ReactJS.
JavaScript
1
star
45

react-hoc-compose

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

toy-fade-carousel

Fade carousel built with React
1
star
47

boomebang

Instagram Boomerang for the web.
TypeScript
1
star
48

react-notification

Primitives to build your growls and dreams
JavaScript
1
star
49

cinemapogi

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

dotenv-autoload

๐Ÿ‘€ Autoload dotenv through recursive finding
JavaScript
1
star
51

2022

SHIP. IT.
TypeScript
1
star
52

node-stringify-object-values

Stringify an object's values
JavaScript
1
star
53

habits

Getting back on track
TypeScript
1
star
54

keplr-suggest-chain-prompt-bug

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

angular-flash

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

yehey

[WIP] Static assets versioning
JavaScript
1
star
57

taima

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

website

1
star
59

websait

Upcoming personal websait.
CSS
1
star
60

react-use

React Hooks โ€” ๐Ÿ‘
TypeScript
1
star
61

parcelwind

Boilerplate for Parcel and Tailwind
HTML
1
star
62

aoc-2022

JavaScript
1
star
63

miniventory

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

chunkr

Make long numbers readable
TypeScript
1
star
65

issue-evmos-ibc

Debugging ibc transactions to evmos
TypeScript
1
star