Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

DAAN PORON VINCENT DE SNERCK @daanporon @spobo

Slide 3

Slide 3 text

60 Communication Agency @ Leuven Kunstmaan 60 employees (21 developers) Campaign, corporate websites and web applications

Slide 4

Slide 4 text

12 1,5 € Financial sector The mission! Team: 12 people Project duration 1,5 years

Slide 5

Slide 5 text

Our toolbelt SOCKJS Frontend Backend

Slide 6

Slide 6 text

‘$scope’ of the frontend APP 13105 CONTR. 61 DIRECTIVES 91 SERVICES 96 MARKUP 5253 TEST 24812 OTHER

Slide 7

Slide 7 text

Keep your sanity while developing AngularJS apps on an enterprise scale

Slide 8

Slide 8 text

The power of AngularJS! • Two-way data binding • Directives • Dependency Injection • Powerful services • Testability

Slide 9

Slide 9 text

But... with great power comes great responsibility.

Slide 10

Slide 10 text

The dark side of AngularJS • No opinionated conventions • Limited routing

Slide 11

Slide 11 text

CONVENTION A way in which someting is usually done. source: Google

Slide 12

Slide 12 text

Conventions to the rescue! • Code becomes easier to read • Simpler to test • Easier to implement changes or features • Team collaboration • Lower total cost of ownership

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Existing conventions

Slide 15

Slide 15 text

angular-seed partials/ |- home.html |- login.html js/ |- controllers.js |- directives.js |- filters.js |- services.js |- app.js https://github.com/angular/angular-seed One file per type for all things

Slide 16

Slide 16 text

yeoman’s generator-angular js/ |- controllers/ |- homeController.js |- loginController.js |- directives/ |- kmAuthentication.js |- filters/ |- services/ |- usersService.js partials/ |- home.html |- login.html https://github.com/yeoman/generator-angular Improvement: each thing = one file, grouped by type

Slide 17

Slide 17 text

Here’s how we did it!

Slide 18

Slide 18 text

js/ |- app.js |- api/ |- home/ |- login/ |- login.js |- config/ |- states.json |- config.json |- controllers/ |- loginCtrl.js |- directives/ |- kmAuthentication.js |- filters/ |- services/ |- usersService.js |- translations/ |- en.json |- views/ |- login.html

Slide 19

Slide 19 text

js/ |- api/ |- home/ |- login/ • Group code by feature

Slide 20

Slide 20 text

• Group in features by type js/ |- login/ |- config/ |- controllers/ |- directives/ |- filters/ |- services/ |- translations/ |- views/

Slide 21

Slide 21 text

• Each file = one thing • Name it like you $provide it - Unless it’s related

Slide 22

Slide 22 text

• Use suffixes or prefixes to identify the type - Controllers: LoginCtrl.js - Specs: LoginCtrlSpec.js - Factory services: usersFactory.js - Directives: kmTabBar.js - …

Slide 23

Slide 23 text

• Common/Shared folder

Slide 24

Slide 24 text

Angular’s module system angular.module('app.presenters', []).constant('presenters', ['Daan', 'Vincent']); angular.module('app', ['app.presenters']).controller('AppCtrl', function($scope, presenters) { $scope.presenters = presenters; });

Slide 25

Slide 25 text

Will this work? angular.module('app.presenters', []) .constant('presenters', ['Daan', 'Vincent']); angular.module('app.resources', []) .service('presentersFetcher', function(presenters) { presenters.push('Ibe'); return function() { return presenters; }; }); angular.module('app', ['app.presenters', 'app.resources']) .controller('AppCtrl', function($scope, presentersFetcher) { $scope.presenters = presentersFetcher(); }); http://plnkr.co/edit/2pw2XqJHax9oPyG2BbM9?p=preview

Slide 26

Slide 26 text

Benefits • Get an overview of your dependencies • Reusability between projects • Faster tests • Lazy loading?

Slide 27

Slide 27 text

Bootstrapping your app angular.module('app.common', []); angular.module('app.login', ['app.common']); angular.module('app', ['app.login']); angular.module('app.login') .config(['fooProvider', function(fooProvider) { // config phase }]).run([function() { // run phase }); http://plnkr.co/edit/VTVBieWqd9gq0oXJFIqS?p=preview /app.js //.js

Slide 28

Slide 28 text

Keeping Your Codebase Clean 1. Services 2. Directives 3. Controllers 4. Coding styles

Slide 29

Slide 29 text

Services • Should be responsible for only one thing • Should have a clean stable API • Should be reusable

Slide 30

Slide 30 text

Example 1 A angular.module('app.api') .factory('socket', [function() { return { 'send': function(method, action, data, callback) { // return uuid }, 'unregisterCallback': function(uuid) { // unregister callback }, 'setDefaultParam': function(key, value) { // set default param }, 'deleteDefaultParam': function(key) { // delete default param } }; }]);

Slide 31

Slide 31 text

Example 1 B angular.module('app.api') .factory('pull', ['socket', function(socket) { return { 'get': function(action, data) { /* get */ }, 'post': function(action, body, data) { /* post */ }, 'put': function(action, body, data) { /* put */ }, 'delete': function(action, data) { /* delete */ } }; }]); angular.module('app.api') .factory('push', ['socket', function(socket) { return { 'subscribe': function(action, data) { // return subscribtion object with unsubscribe function } }; }]);

Slide 32

Slide 32 text

Example 2 AngularJS built-in $resource service

Slide 33

Slide 33 text

Directives • Should not try to do too much • Should be reusable via configurable attributes

Slide 34

Slide 34 text

Parent: I can manage. Child: Manage me! Closely related parent / child directives. Use parent controller to store state. http://plnkr.co/edit/T47y04WxoDK098UAdc3B?p=preview PROBLEM SOLUTION

Slide 35

Slide 35 text

Directive A: This changed. Directive B: I know what to do! Directives that sort of rely on each other. Introduce shared state and $watch! Mediator Pattern http://plnkr.co/edit/gT1Olo0FwUQgXVLlH4TR?p=preview PROBLEM SOLUTION

Slide 36

Slide 36 text

What about events? • No source of truth -> bad on init • When using ‘hooks’ - e.preventDefault() - e.stopPropagation()

Slide 37

Slide 37 text

Controllers • Should be lean / small / simple - Controller are ‘just’ glue between models and views • Should never touch the DOM

Slide 38

Slide 38 text

JavaScript coding style • Community driven standards - idiomatic.js, airbnb, ... • You should enforce these - via jshint, code reviews, ...

Slide 39

Slide 39 text

Managing big apps 1. Code reviews 2. Configuration 3. Workflow automation 4. Routing

Slide 40

Slide 40 text

1. Code reviews • Why? - Improved code quality - Help improve each other - Fewer defects in code - Improved communication about code content • How? - Github/Bitbuckets/Gitlab Pull Requests system

Slide 41

Slide 41 text

2. Configuration • Easier to test • Environment specific configuration - dev, staging, production, … • Module based

Slide 42

Slide 42 text

3. Workflow automation • Grunt ===Powerful! • Prevents doing repetitive work • Simple tasks to do complex things

Slide 43

Slide 43 text

Generating our config file > APP_ENV=prod grunt config

Slide 44

Slide 44 text

config['fileblocks'] = { options: { removeFiles: true, cwd: paths['app'] }, app: { src: paths['tmp'] + 'index.html', blocks: { app: { src: [ 'js/app.js', 'js/*/*/**/*.js', 'js/**/*.js' ] } } } } Injecting our js files in our index.html (grunt-file-blocks)

Slide 45

Slide 45 text

Injecting our js files in our index.html (grunt-file-blocks)

Slide 46

Slide 46 text

Configuring our unit tests (grunt-usemin)

Slide 47

Slide 47 text

Serving our translations on a dev environment (grunt-contrib-connect)

Slide 48

Slide 48 text

Proxying our web service for IE support (grunt-contrib-connect)

Slide 49

Slide 49 text

4. Routing

Slide 50

Slide 50 text

UI Router ngROUTER VS

Slide 51

Slide 51 text

ngRouter UI Router • $route / $routeProvider • Application mapped to urls • Flat structure • One view per route • $state / $stateProvider • Application mapped to states! • Nested states • Many views per state

Slide 52

Slide 52 text

ngRouter overview index.html /inbox ng-view / inboxCtrl + inbox.html 1. email 1 2. email 2 3. email 3 4. email 4 Where your routes are rendered ng-if / ng-show / ng-switch to modify your template to show email details

Slide 53

Slide 53 text

ngRouter, with nice urls /inbox index.html 1. email 1 2. email 2 3. email 3 4. email 4 ng-view InboxCtrl / inbox.html

Slide 54

Slide 54 text

ngRouter, with nice urls /inbox/ index.html 1. email 1 2. email 2 3. email 3 4. email 4 ng-view InboxDetailsCtrl / inbox_details.html always show email details

Slide 55

Slide 55 text

Pain of ngRouter • duplicate logic • content flashes - reload all emails & lose context • link to hardcoded urls

Slide 56

Slide 56 text

UI Router overview index.html 1. email 1 2. email 2 3. email 3 4. email 4 ui-view / inbox InboxCtrl / inbox.html ui-view / inbox.detail (emailId) EmailDetailsCtrl / email_details.html

Slide 57

Slide 57 text

Benefits of UI Router • States - less brittle to maintain • Faster - less work to do • No content flashes - appy feel • Nested states - maps better to domain • Named views - go crazy {{ email.name }}

Slide 58

Slide 58 text

Routing = Configuration { "inbox": { "path": "/inbox", "controller": "InboxCtrl.js", "templateUrl": "inbox.html" }, "inbox.details": { "path": "/:emailId", "controller": "EmailDetailsCtrl.js", "templateUrl": "email_details.html" } } file: /inbox/config/routes.json

Slide 59

Slide 59 text

Resolvers { "inbox": { "path": "/inbox", "controller": "InboxCtrl.js", "resolve": { "emails": "emailsResolver", "markAsSpamText": { "value": ["Foo!"] }, }, "templateUrl": "inbox.html" } } factory('emailsResolver', ['$http', function ($http) { return function ($stateParams) { return $http.get('/inbox', { userId: $stateParams.userId } ); }; }]);

Slide 60

Slide 60 text

Permissions { "inbox": { "path": "/inbox", "controller": "InboxCtrl.js", "permission": "allowed_inbox", "templateUrl": "inbox.html" } }

Slide 61

Slide 61 text

Mixins angular.module('app').factory('sortMixin', [function() { return function($scope) { $scope.sort = function(values, field) { /* sort logic */ }; }]); sortMixin.js { "inbox": { "controller": "MyCtrl", "mixins": ["sort"] } } routes.json

Slide 62

Slide 62 text

Our own seed • Configuration • Routing by configuration • Mixins • Slimmed down version of our Gruntfile Github.COM/KUNSTMAAN/angular-SUPER-SEED

Slide 63

Slide 63 text

Q&A

Slide 64

Slide 64 text

http://bit.ly/angularmeetup