Skip to content

Commit 569bdc4

Browse files
Richard Solomoutoddmotto
Richard Solomou
authored andcommittedOct 13, 2016
Updates to reflect latest Angular 2 conventions (toddmotto#125)
* Removed unneeded semicolons * Added components.js and common.js to file structure * Renamed index.js files to *.module.js * Updated exported variables to match Angular 2 module naming conventions * Updated i18n code examples to match README.md
1 parent 689b108 commit 569bdc4

File tree

7 files changed

+514
-492
lines changed

7 files changed

+514
-492
lines changed
 

‎README.md

+70-68
Original file line numberDiff line numberDiff line change
@@ -85,65 +85,65 @@ export default AppComponent;
8585
A root module is then created, with `AppComponent` imported and registered with `.component('app', AppComponent)`. Further imports for submodules (component and common modules) are made to include all components relevant for the application.
8686

8787
```js
88-
// app.js
88+
// app.module.js
8989
import angular from 'angular';
9090
import uiRouter from 'angular-ui-router';
9191
import AppComponent from './app.component';
92-
import Components from './components/components';
93-
import Common from './common/common';
92+
import ComponentsModule from './components/components.module';
93+
import CommonModule from './common/common.module';
9494

95-
const root = angular
95+
const AppModule = angular
9696
.module('app', [
97-
Components,
98-
Common,
97+
ComponentsModule,
98+
CommonModule,
9999
uiRouter
100100
])
101101
.component('app', AppComponent)
102102
.name;
103103

104-
export default root;
104+
export default AppModule;
105105
```
106106

107107
**[Back to top](#table-of-contents)**
108108

109109
### Component module
110110

111-
A Component module is the container reference for all reusable components. See above how we import `Components` and inject them into the Root module, this gives us a single place to import all components for the app. These modules we require are decoupled from all other modules and thus can be moved into any other application with ease.
111+
A Component module is the container reference for all reusable components. See above how we import `ComponentsModule` and inject them into the Root module, this gives us a single place to import all components for the app. These modules we require are decoupled from all other modules and thus can be moved into any other application with ease.
112112

113113
```js
114114
import angular from 'angular';
115-
import Calendar from './calendar';
116-
import Events from './events';
115+
import CalendarModule from './calendar/calendar.module';
116+
import EventsModule from './events/events.module';
117117

118-
const components = angular
118+
const ComponentsModule = angular
119119
.module('app.components', [
120-
Calendar,
121-
Events
120+
CalendarModule,
121+
EventsModule
122122
])
123123
.name;
124124

125-
export default components;
125+
export default ComponentsModule;
126126
```
127127

128128
**[Back to top](#table-of-contents)**
129129

130130
### Common module
131131

132-
The Common module is the container reference for all application specific components, that we don't want to use in another application. This can be things like layout, navigation and footers. See above how we import `Common` and inject them into the Root module, this gives us a single place to import all common components for the app.
132+
The Common module is the container reference for all application specific components, that we don't want to use in another application. This can be things like layout, navigation and footers. See above how we import `CommonModule` and inject them into the Root module, this gives us a single place to import all common components for the app.
133133

134134
```js
135135
import angular from 'angular';
136-
import Nav from './nav';
137-
import Footer from './footer';
136+
import NavModule from './nav';
137+
import FooterModule from './footer';
138138

139-
const common = angular
139+
const CommonModule = angular
140140
.module('app.common', [
141-
Nav,
142-
Footer
141+
NavModule,
142+
FooterModule
143143
])
144144
.name;
145145

146-
export default common;
146+
export default CommonModule;
147147
```
148148

149149
**[Back to top](#table-of-contents)**
@@ -157,7 +157,7 @@ import angular from 'angular';
157157
import uiRouter from 'angular-ui-router';
158158
import CalendarComponent from './calendar.component';
159159

160-
const calendar = angular
160+
const CalendarModule = angular
161161
.module('calendar', [
162162
uiRouter
163163
])
@@ -172,17 +172,17 @@ const calendar = angular
172172
})
173173
.name;
174174

175-
export default calendar;
175+
export default CalendarModule;
176176
```
177177

178178
**[Back to top](#table-of-contents)**
179179

180180
### File naming conventions
181181

182-
Keep it simple and lowercase, use the component name, e.g. `calendar.*.js*`, `calendar-grid.*.js` - with the name of the type of file in the middle. Use `index.js` for the module definition file, so you can import the module by directory name.
182+
Keep it simple and lowercase, use the component name, e.g. `calendar.*.js*`, `calendar-grid.*.js` - with the name of the type of file in the middle. Use `*.module.js` for the module definition file, as it keeps it verbose and consistent with Angular 2.
183183

184184
```
185-
index.js
185+
calendar.module.js
186186
calendar.controller.js
187187
calendar.component.js
188188
calendar.service.js
@@ -201,45 +201,47 @@ File structure is extremely important, this describes a scalable and predictable
201201
├── app/
202202
│ ├── components/
203203
│ │ ├── calendar/
204-
│ │ │ ├── index.js
204+
│ │ │ ├── calendar.module.js
205205
│ │ │ ├── calendar.controller.js
206206
│ │ │ ├── calendar.component.js
207207
│ │ │ ├── calendar.service.js
208208
│ │ │ ├── calendar.spec.js
209209
│ │ │ └── calendar-grid/
210-
│ │ │ ├── index.js
210+
│ │ │ ├── calendar-grid.module.js
211211
│ │ │ ├── calendar-grid.controller.js
212212
│ │ │ ├── calendar-grid.component.js
213213
│ │ │ ├── calendar-grid.directive.js
214214
│ │ │ ├── calendar-grid.filter.js
215215
│ │ │ └── calendar-grid.spec.js
216-
│ │ └── events/
217-
│ │ ├── index.js
218-
│ │ ├── events.controller.js
219-
│ │ ├── events.component.js
220-
│ │ ├── events.directive.js
221-
│ │ ├── events.service.js
222-
│ │ ├── events.spec.js
223-
│ │ └── events-signup/
224-
│ │ ├── index.js
225-
│ │ ├── events-signup.controller.js
226-
│ │ ├── events-signup.component.js
227-
│ │ ├── events-signup.service.js
228-
│ │ └── events-signup.spec.js
216+
│ │ ├── events/
217+
│ │ │ ├── events.module.js
218+
│ │ │ ├── events.controller.js
219+
│ │ │ ├── events.component.js
220+
│ │ │ ├── events.directive.js
221+
│ │ │ ├── events.service.js
222+
│ │ │ ├── events.spec.js
223+
│ │ │ └── events-signup/
224+
│ │ │ ├── events-signup.module.js
225+
│ │ │ ├── events-signup.controller.js
226+
│ │ │ ├── events-signup.component.js
227+
│ │ │ ├── events-signup.service.js
228+
│ │ │ └── events-signup.spec.js
229+
│ │ └── components.module.js
229230
│ ├── common/
230231
│ │ ├── nav/
231-
│ │ │ ├── index.js
232+
│ │ │ ├── nav.module.js
232233
│ │ │ ├── nav.controller.js
233234
│ │ │ ├── nav.component.js
234235
│ │ │ ├── nav.service.js
235236
│ │ │ └── nav.spec.js
236-
│ │ └── footer/
237-
│ │ ├── index.js
238-
│ │ ├── footer.controller.js
239-
│ │ ├── footer.component.js
240-
│ │ ├── footer.service.js
241-
│ │ └── footer.spec.js
242-
│ ├── app.js
237+
│ │ ├── footer/
238+
│ │ │ ├── footer.module.js
239+
│ │ │ ├── footer.controller.js
240+
│ │ │ ├── footer.component.js
241+
│ │ │ ├── footer.service.js
242+
│ │ │ └── footer.spec.js
243+
│ │ └── common.module.js
244+
│ ├── app.module.js
243245
│ └── app.component.js
244246
└── index.html
245247
```
@@ -363,16 +365,16 @@ TodoController.$inject = ['TodoService'];
363365

364366
export default TodoController;
365367

366-
/* ----- todo/index.js ----- */
368+
/* ----- todo/todo.module.js ----- */
367369
import angular from 'angular';
368370
import TodoComponent from './todo.component';
369371

370-
const todo = angular
372+
const TodoModule = angular
371373
.module('todo', [])
372374
.component('todo', TodoComponent)
373375
.name;
374376

375-
export default todo;
377+
export default TodoModule;
376378
```
377379

378380
This example shows a stateful component, that fetches state inside the controller, through a service, and then passes it down into stateless child components. Notice how there are no Directives being used such as `ng-repeat` and friends inside the template. Instead, data and functions are delegated into `<todo-form>` and `<todo-list>` stateless components.
@@ -429,7 +431,7 @@ class TodoFormController {
429431
this.onAddTodo(
430432
this.EventEmitter({
431433
todo: this.todo
432-
});
434+
})
433435
);
434436
// without EventEmitter wrapper
435437
this.onAddTodo({
@@ -444,17 +446,17 @@ TodoFormController.$inject = ['EventEmitter'];
444446

445447
export default TodoFormController;
446448

447-
/* ----- todo/todo-form/index.js ----- */
449+
/* ----- todo/todo-form/todo-form.module.js ----- */
448450
import angular from 'angular';
449451
import TodoFormComponent from './todo-form.component';
450452

451-
const todoForm = angular
453+
const TodoFormModule = angular
452454
.module('todo.form', [])
453455
.component('todoForm', TodoFormComponent)
454456
.value('EventEmitter', payload => ({ $event: payload}))
455457
.name;
456458

457-
export default todoForm;
459+
export default TodoFormModule;
458460
```
459461

460462
Note how the `<todo-form>` component fetches no state, it simply receives it, mutates an Object via the controller logic associated with it, and passes it back to the parent component through the property bindings. In this example, the `$onChanges` lifecycle hook makes a clone of the initial `this.todo` binding Object and reassigns it, which means the parent data is not affected until we submit the form, alongside one-way data flow new binding syntax `'<'`.
@@ -534,13 +536,13 @@ TodoService.$inject = ['$http'];
534536

535537
export default TodoService;
536538

537-
/* ----- todo/index.js ----- */
539+
/* ----- todo/todo.module.js ----- */
538540
import angular from 'angular';
539541
import uiRouter from 'angular-ui-router';
540542
import TodoComponent from './todo.component';
541543
import TodoService from './todo.service';
542544

543-
const todo = angular
545+
const TodoModule = angular
544546
.module('todo', [
545547
uiRouter
546548
])
@@ -552,14 +554,14 @@ const todo = angular
552554
url: '/todos',
553555
component: 'todo',
554556
resolve: {
555-
todoData: TodoService => TodoService.getTodos();
557+
todoData: TodoService => TodoService.getTodos()
556558
}
557559
});
558560
$urlRouterProvider.otherwise('/');
559561
})
560562
.name;
561563

562-
export default todo;
564+
export default TodoModule;
563565
```
564566

565567
**[Back to top](#table-of-contents)**
@@ -628,18 +630,18 @@ TodoAutoFocus.$inject = ['$timeout'];
628630

629631
export default TodoAutoFocus;
630632

631-
/* ----- todo/index.js ----- */
633+
/* ----- todo/todo.module.js ----- */
632634
import angular from 'angular';
633635
import TodoComponent from './todo.component';
634636
import TodoAutofocus from './todo-autofocus.directive';
635637

636-
const todo = angular
638+
const TodoModule = angular
637639
.module('todo', [])
638640
.component('todo', TodoComponent)
639641
.directive('todoAutofocus', TodoAutoFocus)
640642
.name;
641643

642-
export default todo;
644+
export default TodoModule;
643645
```
644646

645647
Or using ES2015 `Class` (note manually calling `new TodoAutoFocus` when registering the directive) to create the Object:
@@ -667,18 +669,18 @@ TodoAutoFocus.$inject = ['$timeout'];
667669

668670
export default TodoAutoFocus;
669671

670-
/* ----- todo/index.js ----- */
672+
/* ----- todo/todo.module.js ----- */
671673
import angular from 'angular';
672674
import TodoComponent from './todo.component';
673675
import TodoAutofocus from './todo-autofocus.directive';
674676

675-
const todo = angular
677+
const TodoModule = angular
676678
.module('todo', [])
677679
.component('todo', TodoComponent)
678680
.directive('todoAutofocus', () => new TodoAutoFocus)
679681
.name;
680682

681-
export default todo;
683+
export default TodoModule;
682684
```
683685

684686
**[Back to top](#table-of-contents)**
@@ -710,18 +712,18 @@ TodoService.$inject = ['$http'];
710712

711713
export default TodoService;
712714

713-
/* ----- todo/index.js ----- */
715+
/* ----- todo/todo.module.js ----- */
714716
import angular from 'angular';
715717
import TodoComponent from './todo.component';
716718
import TodoService from './todo.service';
717719

718-
const todo = angular
720+
const TodoModule = angular
719721
.module('todo', [])
720722
.component('todo', TodoComponent)
721723
.service('TodoService', TodoService)
722724
.name;
723725

724-
export default todo;
726+
export default TodoModule;
725727
```
726728

727729
**[Back to top](#table-of-contents)**

0 commit comments

Comments
 (0)