Files & Folders
Module Organization
Naming Objects
Organizing Your Project
Files & Folders
app/
css/
app.css
img/
index.html
index-async.html
js/
app.js
controllers.js
directives.js
filters.js
services.js
lib/
angular/
angular.js
angular.min.js
angular-*.js
version.txt
partials/
partial1.html
partial2.html
test/
e2e/
runner.html
scenarios.js
unit/
controllersSpec.js
directivessSpec.js
filtersSpec.js
servicesSpec.js
Directory layout by
angular-seed
Files & Folders
app/
css/
app.css
img/
index.html
js/
app.js
controllers
controller1.js
controller2.js
directives
directive1.js
directive1.js
filters
filter1.js
filter2.js
services
service1.js
service2.js
partials/
partial1.html
partial2.html
lib/
angular/
angular.js
angular.min.js
test/
e2e/
runner.html
scenarios.js
unit/
controllersSpec.js
directivessSpec.js
filtersSpec.js
servicesSpec.js
Directory layout
Organized by type
Files & Folders
app/
app.module.js
app.config.js
layout/
shell.html
shell.controller.js
shell.controller.spec.js
topnav.html
topnav.controller.js
topnav.controller.spec.js
footer.html
footer.controller.js
footer.controller.spec.js
auth/
auth.html
auth.controller.js
auth.directive.html
auth.filters.js
auth.route.js
services/
data.service.js
localstorage.service.js
logger.service.js
customers/
customer-detail.controller.js
customer-detail.controller.spec.js
customers.controller.spec.js
customers.controller-detail.spec.js
customers.module.js
customers.route.js
customers.route.spec.js
sessions/
sessions.html
sessions.controller.js
sessions.routes.js
session-detail.html
session-detail.controller.js
css/
Directory layout
Organized by feature
Modules
A module should be...
Specialized
Independent
Decomposable
Recomposable
Substitutable
Organizing modules
Module recomendation
App
Module1
Module2
Module3
Main module might not have actual objects in it, just a collection of sub-modules.
Sub-modules you want to add to the main module application
Name collisions
Avoiding name collisions
Name objects uniquely for each module.
Use a simple camel-case prefix of two or three digits to represent the module.
Naming Objects
Controllers
- Costumers
- CostumersCtrl
- CostumersController
- costumers.controller
As long as the objects names are consistent trough the project, we are ok .
Services
- Costumers
- CostumersSvc
- CostumersService
- costumers.service
Directives*
- myAvatar
- myAvatarDrct
- my-avatar
Partial Views
- customersView.html
- customers.html
Uses a wrapping function to encapsulate private data in a closure
The Module Pattern
var myModule = (function () {
return {
hello: function hello() {
return 'Hello, world!';
}
};
}());
IIFE: Immediately Invoked Function Expressions.
Exposes a public interface.
Loads asynchronously modules at runtime.
Asynchronous Module Definition (AMD)
define(['amd1', 'amd2'],
function myModule(amd1, amd2) {
var testResults = {
test1: amd1.test(),
test2: amd2.test()
},
// Define a public API for your module:
api = {
testResults: function () {
return testResults;
}
};
return api;
});
RequireJs
Creates a bundle file
Universal Module Definition (UMD)
//bar.js
module.exports = function (n) {
return n * 3
};
//foo.js
var bar = require('./bar');
module.exports = function (n) {
return n * bar(n);
};
CommonJs, Node-style modules.
BrowserifyJs
Task Automation
Grunt
Gulp
Testing
TDD
...
it('should have Customers controller', function() {
// TODO
});
it('should find 1 Customer when filtered by name', function() {
// TODO
});
it('should have 10 Customer', function() {
// TODO -> Mock data
});
it('should return Customers via XHR', function() {
// TODO ->$httpBackend
});
// and so on
Karma & Jasmine
Mocha
Sample App
Angular
Browserify
Gulp
Karma & Jasmine
Less
Ng Materials
Thanks!