101
ANGULARJS

Beyond AngularJS: Best practices and more

Embed Size (px)

DESCRIPTION

Given at the Intuit front-end conference, Ari Lerner demonstrates best-practices for AngularJS applications and demonstrates how to best build your web application.

Citation preview

Page 1: Beyond AngularJS: Best practices and more

ANGULARJS

Page 2: Beyond AngularJS: Best practices and more

ANGULARJS

AND BEYOND

Page 3: Beyond AngularJS: Best practices and more
Page 4: Beyond AngularJS: Best practices and more

WHO AM I?

ARI LERNER, FULLSTACK.IOAuthor of and

Author of a few others (,

)Teacher at ,

Co-founder of ,

Background in distributed computing and infrastructure

ng-book (https://ng-book.com) ng-newsletter(http://ng-newsletter.com)

D3 on Angular(http://leanpub/d3angularjs) Riding Rails with AngularJS(https://leanpub.com/angularjs-rails)

HackReactor (http://hackreactor.com) GeneralAssembly (http://generalassemb.ly)

Fullstack.io (http://fullstack.io) FullstackEdu(https://fullstackedu.com)

Page 5: Beyond AngularJS: Best practices and more
Page 6: Beyond AngularJS: Best practices and more

NG-BOOK.COM

Page 7: Beyond AngularJS: Best practices and more

FULLSTACKEDU

Page 8: Beyond AngularJS: Best practices and more

CORPORATE TRAININGAll sized companies, large and small1k+ total participantsRefined for more than a yearFrom DevOps to front-end

Page 9: Beyond AngularJS: Best practices and more

CORPORATE TRAININGContact us at [email protected]@auser

Page 10: Beyond AngularJS: Best practices and more

HACK REACTORFantastic JavaScript-based courseFantastic Angular curriculumThe harvard of programming schools

Page 11: Beyond AngularJS: Best practices and more

AGENDA1. HTML today2. Why Angular?

Page 12: Beyond AngularJS: Best practices and more

AGENDA4. Community5. Best practices

Page 13: Beyond AngularJS: Best practices and more

AGENDA6. Town hall

Page 14: Beyond AngularJS: Best practices and more

HTML IS OLD

Page 15: Beyond AngularJS: Best practices and more

HTML IS OLD

OLDER THAN SOME OF US<html> <head> <title>Really basic html</title> </head> <body> <h1 id="headline">Hello world</h1> <span class="notice"></span> <button id="buyBtn">Click me</button> </body></html>

Page 16: Beyond AngularJS: Best practices and more
Page 17: Beyond AngularJS: Best practices and more

STATIC MARKUP

Page 18: Beyond AngularJS: Best practices and more

WHAT ABOUT WRITING WEB APPLICATIONS?

Page 19: Beyond AngularJS: Best practices and more

WHAT ABOUT WRITING WEB APPLICATIONS?Interactivity?Immediate responseDesktop, powerful

Page 20: Beyond AngularJS: Best practices and more

HOW DO WE ADD INTERACTION TODAY?var content = document.findElementById('headline'), newDiv = document.createElement('div');// Do some interesting things here// with our new div elementscontent.appendChild(newDiv);

Page 21: Beyond AngularJS: Best practices and more

OR

Page 22: Beyond AngularJS: Best practices and more

JQUERY<div class="notice"></div><button id="exampleBuyBtn">Click me</button>

$("button#buyBtn").on('click', function(event) { $('div.notice').html('You clicked the button');});

Click me

Page 23: Beyond AngularJS: Best practices and more

IMPERATIVE WRAPPER AROUND DOMMANIPULATION

Page 24: Beyond AngularJS: Best practices and more

NOT A WEB APPLICATION MAKER

Page 25: Beyond AngularJS: Best practices and more

When all you have is a hammer everything lookslike a DOM to manipulate

Page 26: Beyond AngularJS: Best practices and more

WHAT'S WRONG WITH THIS PICTURE?

Page 27: Beyond AngularJS: Best practices and more

WHAT'S WRONG WITH THIS PICTURE?tight couplingstructureless code baselow-level interaction

Page 28: Beyond AngularJS: Best practices and more

BUILDING ACCESS TO DOM, NOT WEBAPPLICATION CODE

Page 29: Beyond AngularJS: Best practices and more

HOW RUDIMENTARY

Page 30: Beyond AngularJS: Best practices and more

OKAY, THEN WHAT SHOULD WE DO?

Page 31: Beyond AngularJS: Best practices and more

WHAT IF WE REINVENTED HTML?

Page 32: Beyond AngularJS: Best practices and more

HTML IS DECLARATIVE

SHOULDN'T OUR INTERACTION BE AS WELL?

Page 33: Beyond AngularJS: Best practices and more
Page 34: Beyond AngularJS: Best practices and more

ENTER ANGULARJS

Page 35: Beyond AngularJS: Best practices and more

A MVW FRONT-END FRAMEWORK

Page 36: Beyond AngularJS: Best practices and more

A MVW FRONT-END FRAMEWORK

MODEL-VIEW-WHATEVER

Page 37: Beyond AngularJS: Best practices and more
Page 38: Beyond AngularJS: Best practices and more

SUPER FAST

IN DEVELOPMENT AND PRODUCTION

Page 39: Beyond AngularJS: Best practices and more
Page 40: Beyond AngularJS: Best practices and more

SPONSORED BY GOOGLE

AND IN PRODUCTION ALL-OVER-THE-INTERNET

Page 41: Beyond AngularJS: Best practices and more
Page 42: Beyond AngularJS: Best practices and more
Page 43: Beyond AngularJS: Best practices and more
Page 44: Beyond AngularJS: Best practices and more
Page 45: Beyond AngularJS: Best practices and more
Page 46: Beyond AngularJS: Best practices and more

AND MANY MORE

Page 47: Beyond AngularJS: Best practices and more

COST EFFICIENT

FOR DEVELOPMENT AND PRODUCTION

Page 48: Beyond AngularJS: Best practices and more
Page 49: Beyond AngularJS: Best practices and more

BUILT WITH TESTING IN MIND

Page 50: Beyond AngularJS: Best practices and more

BUILT WITH TESTING IN MIND

WITH FANTASTIC TOOLING

Page 51: Beyond AngularJS: Best practices and more
Page 52: Beyond AngularJS: Best practices and more

HIGHLY ACTIVE COMMUNITY

AND MANY OPEN-SOURCE COMPONENTS

Page 53: Beyond AngularJS: Best practices and more
Page 54: Beyond AngularJS: Best practices and more

COMPLETELY FREE

Page 55: Beyond AngularJS: Best practices and more

COMPLETELY FREE

SERIOUSLY, MIT LICENSED

Page 56: Beyond AngularJS: Best practices and more
Page 57: Beyond AngularJS: Best practices and more

BEST PRACTICES

Page 58: Beyond AngularJS: Best practices and more

WHY BEST PRACTICES?Crufty codeProduct longevityOptimizationExtensibilityShareabilityMaintainability...

Page 59: Beyond AngularJS: Best practices and more

TEST TEST TEST

Page 60: Beyond AngularJS: Best practices and more

NEVER PREPEND MODULES WITH NGDon't want to collide with ng packages

Page 61: Beyond AngularJS: Best practices and more

NEVER PREPEND MODULES WITH NGangular.module('ngApp', [])// ...

Page 62: Beyond AngularJS: Best practices and more

INSTEADangular.module('inApp', [])// ...

Page 63: Beyond AngularJS: Best practices and more

MODULARIZE YOUR CODEangular.module('inApp', [ 'in.login', 'in.typeahead', // ... ]);

Page 64: Beyond AngularJS: Best practices and more

MODULARIZE YOUR CODEWrite once

Page 65: Beyond AngularJS: Best practices and more

MODULARIZE YOUR CODEWrite once, use often

Page 66: Beyond AngularJS: Best practices and more

USE BROWSERIFY// login/index.jsmodule.exports = angular.module('inApp.login', [])

// main.jsangular.module('inApp', [ require('./login').name]);

Page 67: Beyond AngularJS: Best practices and more

USE BROWSERIFYModule-based dependencies allow our app to be highly

extensible and force us to develop with modules

Page 68: Beyond AngularJS: Best practices and more

USE UIROUTERmodule.exports = angular.module('inApp.root', [ 'ui.router' ]);

Page 69: Beyond AngularJS: Best practices and more

USE UIROUTERmodule.exports = angular.module('inApp.root', ['ui.router']).config(function($stateProvider) { $stateProvider .state('root', { abstract: true, url: '/' }) .state('root.home', { url: '', views: { '@': { templateUrl: '/scripts/root/home.html' } } }) // ...

Page 70: Beyond AngularJS: Best practices and more

USE UIROUTERState-based routing is far more powerful than simple URL-based.

It's extensible and gives us far-greater flexibility.

Page 71: Beyond AngularJS: Best practices and more

OPTIMIZE LATE<div class='profile' ng-repeat='user in users'> <span class="name">{{ user.name }}</span> <span class="email">{{ user.email }}</span></div>

Page 72: Beyond AngularJS: Best practices and more

OPTIMIZE LATEPreoptimization stunts growth and over-complicates an existing

code.

Page 73: Beyond AngularJS: Best practices and more

OPTIMIZE LATEWe can always optimize to infinity

Page 74: Beyond AngularJS: Best practices and more

USE .PROVIDER() WHENPOSSIBLE

Providers make it easy to distribute module-based services

Page 75: Beyond AngularJS: Best practices and more

USE .PROVIDER() WHENPOSSIBLE

module.exports = angular.module('inApp', ['ui.router']).provider('User', function() { var backendUrl = 'http://default.url;

this.setBackendUrl = function(url) { backendUrl = url; };

this.$get = function($http) { return this; };})

Page 76: Beyond AngularJS: Best practices and more

USE .PROVIDER() WHENPOSSIBLE

angular.module('inApp', ['ui.router']).config(function(UserProvider) { UserProvider.setBackendUrl('http://intuit.com/api/v1');});

Page 77: Beyond AngularJS: Best practices and more

SEARCH FIRST, WRITE LASTChances are someone has written something to do the thing you

are wanting to do. Search for it, then write it.

Page 78: Beyond AngularJS: Best practices and more

USE GULP/GRUNT// ...gulp.task('jshint', function() { return gulp.src(path.join(config.src.scriptDir, config.src.scriptFiles)) .pipe($.jshint(config.src.jshint)) .pipe($.jshint.reporter(stylish));});// ...

Page 79: Beyond AngularJS: Best practices and more

USE GULP/GRUNTUsing a build-system provides consistantly correct, production-

ready code.

Page 80: Beyond AngularJS: Best practices and more

PAIR PROGRAM

Page 81: Beyond AngularJS: Best practices and more

DON'T USE [] NOTATION, USE A PRE-MINIFIER

Save your fingers

Page 82: Beyond AngularJS: Best practices and more

DON'T USE [] NOTATION, USE A PRE-MINIFIER

.controller(['UserService', function(UserService) {

}]);

Page 83: Beyond AngularJS: Best practices and more

DON'T USE [] NOTATION, USE A PRE-MINIFIER

.controller(function(UserService) {

});

Page 84: Beyond AngularJS: Best practices and more

USE XSRF/CSRF TOKENS/COOKIESCross-Site Request Forgery tokens allow the backend to ensure

a request coming in matches a previously known request

Page 85: Beyond AngularJS: Best practices and more

USE XSRF/CSRF TOKENS/COOKIESmodule.exports = angular.module('inApp.login', []).config(function($httpProvider) { $httpProvider.defaults.xsrfHeaderName = 'X-INT-XSRF'; $httpProvider.defaults.xsrfCookieName = 'int-xsrftoken'; $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';});

Page 86: Beyond AngularJS: Best practices and more

TEST TEST TESTTesting ensures we know what's going on with our app

Page 87: Beyond AngularJS: Best practices and more

TEST TEST TESTUnit test (as much as possible)E2E test (for CI servers, mostly)

Page 88: Beyond AngularJS: Best practices and more

TEST TEST TESTUse zones.js (or Angular 2.0) for better stacktraces

Page 89: Beyond AngularJS: Best practices and more

COMMUNITY

Page 90: Beyond AngularJS: Best practices and more

COMMUNITYAngular's power comes from the highly-engaged community

Page 91: Beyond AngularJS: Best practices and more

BOOKS

Page 92: Beyond AngularJS: Best practices and more

TUTORIALS

Page 93: Beyond AngularJS: Best practices and more

TRAININGContact us at [email protected]@auser

Page 94: Beyond AngularJS: Best practices and more

COMMUNITY PLUGINS

Page 95: Beyond AngularJS: Best practices and more

WHAT CAN WE DO TO CONTRIBUTE?

Page 96: Beyond AngularJS: Best practices and more

WHAT CAN WE IMPLEMENT FOR OURSELVES?

Page 97: Beyond AngularJS: Best practices and more

I18N

Page 98: Beyond AngularJS: Best practices and more

I18NUse angular-translate

Page 99: Beyond AngularJS: Best practices and more

ASK ME ANYTHING

Page 100: Beyond AngularJS: Best practices and more

THANK YOU

Page 101: Beyond AngularJS: Best practices and more

NG-BOOK.COM

630+ page book with all this information and much much more.

The only constantly updating book available for Angular today.