advanced tips & tricks for using angular js

70
Advanced Tips and Tricks using AngularJS Simon Guest, Distinguished Engineer. Neudesic, LLC

Upload: simon-guest

Post on 07-May-2015

11.635 views

Category:

Technology


0 download

DESCRIPTION

Session from GIDS 2014, showing tips and tricks for using AngularJS for creating production-ready JavaScript applications

TRANSCRIPT

Page 1: Advanced Tips & Tricks for using Angular JS

Advanced Tips and Tricks using AngularJSSimon Guest, Distinguished Engineer. Neudesic, LLC

Page 2: Advanced Tips & Tricks for using Angular JS

The last decade of server-side HTML...

Page 3: Advanced Tips & Tricks for using Angular JS
Page 4: Advanced Tips & Tricks for using Angular JS

What's wrong with this model?

Page 5: Advanced Tips & Tricks for using Angular JS

What's wrong with this model?» Most UI actions require round trip

» Poor mobile experience

» Devices are getting more powerful

» Servers need to scale

» Any offline scenario is near impossible

Page 6: Advanced Tips & Tricks for using Angular JS

The next decade of client-side JavaScript

Page 7: Advanced Tips & Tricks for using Angular JS
Page 8: Advanced Tips & Tricks for using Angular JS

How does this help?» UI actions handled locally

» Vasly improved mobile experience

» Taking advantage of the power of the device

» Server can handle more clients

» Offline becomes more manageable

Page 9: Advanced Tips & Tricks for using Angular JS

Too many choices...

Page 10: Advanced Tips & Tricks for using Angular JS
Page 11: Advanced Tips & Tricks for using Angular JS
Page 12: Advanced Tips & Tricks for using Angular JS

Audience Poll

Page 13: Advanced Tips & Tricks for using Angular JS

Who knows what AngularJS is?

Page 14: Advanced Tips & Tricks for using Angular JS

Who has built AngularJS sample apps?

Page 15: Advanced Tips & Tricks for using Angular JS

Who is using AngularJS for an ongoing project?

Page 16: Advanced Tips & Tricks for using Angular JS

Angular's ups and downs...

Page 17: Advanced Tips & Tricks for using Angular JS
Page 18: Advanced Tips & Tricks for using Angular JS

Goal of this sessionShare 10 tips and tricks that we've learned from real world AngularJS projects

Page 19: Advanced Tips & Tricks for using Angular JS

Goal of this sessionCompile different tips and tricks from around the Web

Page 20: Advanced Tips & Tricks for using Angular JS

Goal of this sessionShare as much code as possible

Page 21: Advanced Tips & Tricks for using Angular JS

10. StructureHow should I structure my AngularJS project?

Page 22: Advanced Tips & Tricks for using Angular JS

10. Structure» Roll your own

» Angular Seed (https://github.com/angular/angular-seed)

» Yeoman Angular Generator (http://yeoman.io)

Page 23: Advanced Tips & Tricks for using Angular JS
Page 24: Advanced Tips & Tricks for using Angular JS

10. Structure» Roll your own

» Angular Seed (https://github.com/angular/angular-seed)

» Yeoman Angular Generator (http://yeoman.io)

Page 25: Advanced Tips & Tricks for using Angular JS

10. Structure» Come back to pivoting on functionality when the

project gets large

Page 26: Advanced Tips & Tricks for using Angular JS

10. Structure> app-> controllers--> todoController.js-> services--> todoService.js-> directives--> todoItemDirective.js

Page 27: Advanced Tips & Tricks for using Angular JS

10. Structure> app-> todo--> controller.js--> service.js--> itemDirective.js

Page 28: Advanced Tips & Tricks for using Angular JS

9. MinificationShould I minify my AngularJS project?

Page 29: Advanced Tips & Tricks for using Angular JS

9. Minification» Due to the "DI" way that Angular works,

minification is hard

Page 30: Advanced Tips & Tricks for using Angular JS

9. Minification» Due to the "DI" way that Angular works,

minification is hard

» Do you really need to minify?

Page 31: Advanced Tips & Tricks for using Angular JS

9. Minification» Due to the "DI" way that Angular works,

minification is hard

» Do you really need to minify?-- AngularJS includes are already compiled JS-- Yeoman template includes as much minification as you need-- Disable property renaming, and manually change constructors

Page 32: Advanced Tips & Tricks for using Angular JS

8. DirectivesWhat are they?

Page 33: Advanced Tips & Tricks for using Angular JS

8. Directives» They make AngularJS unique

» They make your markup more declarative

» They support templates nicely

» They promote re-use

» They prevent lots of JavaScript!

Page 34: Advanced Tips & Tricks for using Angular JS

7. Page LoadingHow do I create a good user experience?

Page 35: Advanced Tips & Tricks for using Angular JS

7. Page Loading» Flash of {{your.stuff}} when you load the page

Page 36: Advanced Tips & Tricks for using Angular JS

7. Page Loading» Flash of {{your.stuff}} when you load the page

» Use ng-cloak directive in order to hide declarations until they are initialized

» Use ng-bind instead of {{}} in your index.html

Page 37: Advanced Tips & Tricks for using Angular JS

6. Internet ExplorerDoes AngularJS work with IE?

Page 38: Advanced Tips & Tricks for using Angular JS
Page 39: Advanced Tips & Tricks for using Angular JS

6. Internet Explorer» Support for IE8 is being removed from Angular 1.3

onwards

» If you are going to keep supporting IE8 with Angular 1.2...-- Never use <custom-directive>-- Use <div custom-directive> instead

» Ensure that IE is part of test plan (if you are using Selenium)

Page 40: Advanced Tips & Tricks for using Angular JS

5. Development EnvironmentWhat tools should I be using?

Page 41: Advanced Tips & Tricks for using Angular JS

5. Development Environment» Angular Support in IDEs

-- WebStorm 8-- Sublime-- Use data-ng-* in non-supported environments

» Install Angular via Bower

Page 42: Advanced Tips & Tricks for using Angular JS

5. Development Environment» Logging

-- Avoid using console.log in your controllers

» Use $log instead-- $log can be extended to provide additional logging capabilities

Page 43: Advanced Tips & Tricks for using Angular JS

5. Development Environment» Batarang

-- Chrome extension of AngularJS debugging

Page 44: Advanced Tips & Tricks for using Angular JS
Page 45: Advanced Tips & Tricks for using Angular JS

5. Development Environment» Batarang

-- Chrome extension to enable AngularJS debugging

» AngularJS tab in Chrome dev tools-- Enables scope inspection-- Performance to identify trouble spots-- Service dependency graph

Page 46: Advanced Tips & Tricks for using Angular JS

5. Development Environment» Javascript Debugging

-- Use 'debugger' in any controller to add breakpoint-- Very useful combined with Batarang

Page 47: Advanced Tips & Tricks for using Angular JS

4. Angular-Supported FrameworksHow do I deal with non-AngularJS stuff?

Page 48: Advanced Tips & Tricks for using Angular JS

4. Non-Angular Stuff» Bootstrap

-- UI Bootstrap-- Components written by the AngularJS team

» Examples-- AngularJS version of Alert-- $modal for invoking modals-- Date/time pickers

» http://angular-ui.github.io/bootstrap/

Page 49: Advanced Tips & Tricks for using Angular JS
Page 50: Advanced Tips & Tricks for using Angular JS

4. Non-Angular Stuff» jQuery

-- Use angular.element instead of $() selector-- Search for a jQuery-equivalent instead

» Examples-- html(), text(), val()

» Ask whether you can use directives instead

Page 51: Advanced Tips & Tricks for using Angular JS

4. Non-Angular Stuff» Other JavaScript methods

-- angular.fromJson, angular.toJson, angular.copy, angular.extend-- $timeout, $log, $window, $q, $document-- Third party libraries for date formatting

» Why?-- Angular methods better observe the scope lifecycle-- More predictable results

Page 52: Advanced Tips & Tricks for using Angular JS

3. Separation of ConcernsHow do I make the right choices?

Page 53: Advanced Tips & Tricks for using Angular JS

3. Separation of Concerns» Three golden rules

-- Controllers should never refer to any DOM elements-- Controllers should call services vs. holding too much business logic-- "How do I pass things between controllers?"--- ...probably means that you are doing things wrong

Page 54: Advanced Tips & Tricks for using Angular JS

3. Separation of Concerns» Controller inheritence

-- Is possible to use angular.extend to provide some kind of inheritence on controllers

Page 55: Advanced Tips & Tricks for using Angular JS

2. ScopeWhat do I need to know about $scope?

Page 56: Advanced Tips & Tricks for using Angular JS

2. Scope» Scope is not your model

-- Even though many of the samples show it this way-- Scope should be the glue between your controller and your model (accessed through services)-- Remember, services are singletons

Page 57: Advanced Tips & Tricks for using Angular JS

2. Scope» Scope inheritence

-- Avoid $rootScope, try to use services instead-- Create subscopes when invoking subcontroller from controller--- Can be a little confusing, especially with frameworks (e.g. Bootstrap modal)-- Batarang can be your friend

Page 58: Advanced Tips & Tricks for using Angular JS

1. PerformanceHow do I prevent performance bottlenecks?

Page 59: Advanced Tips & Tricks for using Angular JS

1. Performance» You are not in control of when Angular invokes

functions based on changes to $scope

» A single change in scope (e.g keypress) can call multiple functions

Page 60: Advanced Tips & Tricks for using Angular JS

1. Performance» Most changes to $scope are processed in fractions

of a second-- Faster than the human eye can detect on a page-- However, once you start reaching 1500+ function calls on a scope change, thing's deteriorate-- Deteriorate really quickly

Page 61: Advanced Tips & Tricks for using Angular JS

1. Performance» Using ng-repeat or ng-switch can compound this

Page 62: Advanced Tips & Tricks for using Angular JS

1. Performance<tr ng-repeat="item in items"> <td>{{item.name}}</td> <td>{{item.description}}</td> <td>{{getPrice(item.id)}}</td></tr>

» How many times will getPrice function be called?

Page 63: Advanced Tips & Tricks for using Angular JS

1. Performance» Overcoming this bottleneck

-- Never call $scope functions from within ng-repeat or ng-switch statements-- Use watch collection to calculate everyting when the controller is first invoked

Page 64: Advanced Tips & Tricks for using Angular JS

1. Performance$scope.$watchCollection('items', function (newItems) { for (var i = 0; i < newItems.length; i++) { newItems[i].price = getPrice(newItems.id); } $scope.items = newItems;});

Page 65: Advanced Tips & Tricks for using Angular JS

1. Performance<tr ng-repeat="item in items"> <td>{{item.name}}</td> <td>{{item.description}}</td> <td>{{item.price}}</td></tr>

Page 66: Advanced Tips & Tricks for using Angular JS

The Ten Again

Page 67: Advanced Tips & Tricks for using Angular JS

The Ten Again-- 10. Structure-- 9. Minification-- 8. Directives-- 7. Page Loading-- 6. Internet Explorer-- 5. Development Environment-- 4. Non-Angular Stuff-- 3. Separation of Concerns-- 2. Scope-- 1. Performance

Page 68: Advanced Tips & Tricks for using Angular JS

Thank you!

Page 69: Advanced Tips & Tricks for using Angular JS

Q&A» Simon Guest, Distinguished Engineer, Neudesic LLC

» simonguest.com (@simonguest)

» http://github.com/simonguest/gids

» http://slideshare.net/simonguest

Page 70: Advanced Tips & Tricks for using Angular JS

-- http://www.amazon.com/File-New-Presentation-Developers-Professionals/dp/0615910459