SlideShare a Scribd company logo
Integrating Angular js & three.js
Josh Staples 
@cubicleDowns 
blog.tempt3d.com 
github.com/cubicleDowns/ng-three-viewer
WHO? Software Engineer @ napofearth.com 
“Explore Music Visually” PRIVATE BETA (#ngmeetup)
TODAY 
1. (v1) Angular & Three.js 3D Model Viewer 
2. (v2) “Controller as” & 
prototypal Angular components 
3. Grunt & Closure compilation approaches 
4. (v3*) Scoped scene graph
WHY? 
Great pattern(s) with low barrier of entry 
& high productivity 
All web devs can easily be 3D web devs 
Isolate THREE code from Angular UI/UX code!
THREE WHAT? 
threejs.org three features 
● Renderers 
● Scene Graph 
● Cameras 
● Animation 
● Lights 
● Materials 
● Shaders 
● Objects 
● Geometry 
● Loaders 
● Utilities 
● Export/Import 
● Support 
● Examples 
● WebVR 
● DIYVR 
Started by Mr. Doob on April 23, 2010
ANGULAR WHO?
Demo v1 
http://localhost:8000/v1/dist/
CONTROLLER 
AppController 
CONTROLLER 
FileLoaderController 
UI Elements and Controllers 
DIRECTIVE INCLUDE 
file-loader.html 
DIRECTIVE INCLUDE 
about.html 
DIRECTIVE INCLUDE 
chrome.html 
DIRECTIVE INCLUDE 
toolbar.html
CONTROLLER 
FileLoaderController 
Dependency Injections Across Angular Components 
CONTROLLER 
AppController 
DIRECTIVE 
select 
SERVICE 
StorageService 
SERVICE 
MessageBus 
FACTORY 
Viewer
Viewer.factory('ViewerFactory', … ) { 
init() 
home = new Viewer.Scene() 
animate () 
render () 
makeSelection () 
loadOBJMTL () 
loadGLTF () 
loadOBJ () 
loadJSON () 
scale () 
rotate () 
/** Translate the model along an axis 
* @param {number} x 
* @param {number} y 
* @param {number} z */ 
translate (x,y,z) 
home.wrangler.currentModel.position.set(x, y, z); 
CONTROLLER 
AppController 
● init () 
● rotate () 
● scale () 
● translate () 
SERVICE 
MessageBus 
DIRECTIVE 
select 
● makeSelection () 
CONTROLLER 
FileLoaderController 
● loadOBJMTL () 
● loadGLTF () 
● loadOBJ () 
● loadJSON () 
Viewer Factory Interface
Viewer Factory Architecture 
Viewer Factory Singleton 
function init(params) { 
home = new Viewer.Scene(params); 
animate(); 
} 
Viewer.Scene() 
this.scene 
THREE.Scene() 
this.renderer 
THREE.WebGLRenderer() 
this.wrangler 
Viewer.Wrangler() 
function animate () { 
requestAnimationFrame(animate); 
render(); 
} 
this.setup 
Viewer.Setup() 
this.cameras 
Viewer.Cameras() 
this.controls 
THREE.OrbitControls() 
this.raycaster 
THREE.Raycaster()
USE CASE - User Click, Intersect 3D Model, Return Model Information 
Angular Directive, <canvas select> 
elem.on(tap, function(e) 
x = e.gesture.center.x; 
y = e.gesture.center.y; 
// creating NDC coordinates for ray intersection. 
mouseDown.x = (x / width) * 2 - 1; 
mouseDown.y = -(y / height) * 2 + 1; 
ViewerFactory.makeSelection(mouseDown); 
Viewer Factory, makeSelection 
makeSelection(mouse): 
Angular Controller/Factory 
$scope.$on(‘objectSelected’, function () { 
// Do Something. 
}); 
var vector = new THREE.Vector3( mouse.x, mouse.y, 1).unproject(home.cameras.liveCam); 
home.raycaster.set(home.cameras.liveCam.position, vector.sub(home.cameras.liveCam.position).normalize()); 
var intersected = home.raycaster.intersectObjects(home.wrangler.currentModel, true); 
MessageBus.trigger('objectSelected', intersected[0])
MOST PROFITABLE MOVIE?
MOST PROFITABLE MOVIE? 
THE SEQUEL!
STARRING 
“Controller as” 
& 
as ctrl 
Annotations 
as SNAFU
Sequel (v2) 
http://localhost:8000/v2/dist/
Controller as 
<div id="file-loader" ng-controller="FileLoaderController as loader" ng-show=”loader.visible”> 
<input type="text" ng-model="loader.data.obj" placeholder="obj file url"> 
<input type="text" ng-model="loader.data.name" placeholder="unique name"> 
<button ng-click="otherLoader.loadOBJMTL()">Load OBJ/MTL</button> 
<button ng-click="loader.loadSampleOBJMTL()">SAMPLE OBJ-MTL</button> 
Controller 
level scope 
:) 
<div id="file-loader" ng-controller="FileLoaderController" ng-show=”visible”> 
- - - 
<input type="text" ng-model="data.obj" placeholder="obj file url"> 
<input type="text" ng-model="data.mtl" placeholder="mtl file url"> 
<button ng-click="loadOBJMTL()">Load OBJ/MTL</button> 
<button ng-click="loadSampleOBJMTL()">SAMPLE OBJ-MTL</button> 
nearest scope 
:(
Service, !Factory 
Viewer.ViewerService 
.prototype 
init 
listeners 
animate 
render 
makeSelection 
loadOBJMTL 
loadOBJ 
loadGLTF 
loadJSON 
rotate 
translate 
scale 
● No More Factories 
○ closure pattern 
● Instead, prototypal Service 
○ ‘new’ and this 
○ .bind() for callbacks 
● Saves Memory, Time, Searches (sorry) 
● Single Pattern For Everything! 
● IMHO, the best way to code JS
/** @ngInject */ 
Viewer.ViewerService = function($timeout, MessageBus){ 
this.timeout = $timeout; 
this.MessageBus = MessageBus; 
}; 
Viewer.ViewerService.prototype.init = function (params){ 
this.home = new Viewer.Scene(params); 
this.MessageBus.trigger(‘app-ready’); 
animate(); 
}; 
Viewer.factory('ViewerFactory', ['MessageBus', function (MessageBus) 
function init () {} 
function makeSelection() {} 
return { 
init: init, 
makeSelection: makeSelection 
} 
closure style, 
ng-annotate 
: 
prototypal 
:) 
Prototypal Angular
CAonnntorotallteior nass 
/** Service which initiates the THREE.js scene and 
* provides methods to interact with that scene 
* 
* @param {angular.$timeout} $timeout 
* @param {!Viewer.MessageBus} MessageBus 
* @constructor 
* @ngInject 
*/ 
Viewer.ViewerService = function($timeout, MessageBus){ 
this.timeout = $timeout; 
this.MessageBus = MessageBus; 
}; 
/** 
* Translate the model along an axis 
* @param {number} x 
* @param {number} y 
* @param {number} z 
*/ 
Viewer.ViewerService.prototype.translate = function(x, y, z){ 
this.home.wrangler.currentModel.position.set(x, y, z) 
}; 
/** 
* @param {number} s 
*/ 
Viewer.ViewerService.prototype.scale = function(s) { 
this.home.wrangler.currentModel.scale.set(s, s, s); 
}; 
The Closure Compiler can use data type 
information about JavaScript variables to provide 
enhanced optimization and warnings.
APP INIT (app.js) 
angular.module('ThreeViewer', ['ngHammer', 'ngRoute', 'LocalStorageModule']) 
.config(['localStorageServiceProvider',function(localStorageServiceProvider){ 
…. 
.config(['$locationProvider', function($locationProvider) { 
…. 
$locationProvider.html5Mode(true); 
.config(['$routeProvider', function($routeProvider){ 
angular.module('ThreeViewer', ['ngRoute', 'LocalStorageModule']) 
.config(ThreeViewer.ConfigLocation) 
…. 
.directive('select', ['ViewerService', ThreeViewer.SelectDirective.factory]) 
…. 
.filter('forceInt', ThreeViewer.ForceInt.factory) 
…. 
.service('ViewerService', [MessageBus', ThreeViewer.ViewerService]) 
…. 
.controller('AppController', ['$scope', 'ViewerService', ThreeViewer.AppController]); 
v2 
:) 
v1 
:
uglify: { 
ng3: { 
options: { 
compress: { 
drop_console: true 
}, 
sourceMap: true, 
}, 
files: { 
'dist/app.min.js': ['<%= concat.ng3.dest %>'] 
} 
} 
}, 
command: 'java -jar closure/compiler.jar ' + 
'--compilation_level SIMPLE_OPTIMIZATIONS' + 
'--language_in ECMASCRIPT5_STRICT ' + 
'--angular_pass ' + 
'--externs closure/externs/angular-1.3.js ' + 
'--externs closure/externs/three.js ' + 
'--generate_exports ' + 
'--manage_closure_dependencies ' + 
'--js closure/library/base.js ' + 
'--js <%= app %> ' + 
'--js <%= ng %> ' + 
'--js <%= three %> ' + 
'--js_output_file dist/app.min.js' 
Minify or Closure Compilation? 
Closure Compiler 
● type checking 
● ngInject 
● goog.provide / require 
Grunt ng-annotate 
● uglify 
● ng-annotate
NOGN Eto A TPHPR, ETEWO PATTERNS 
V1 
● Most Common Angular Pattern 
● Grunt uglify / minify 
● Factories 
● Services 
● Filters 
● Directives 
● Init controllers from DOM 
V2 
● Prototypal Pattern for Everything! 
● Bridge to Angular 2.0 
● Controller as (local scope) 
● Closure Compilation 
○ type checking 
○ -- angular_pass 
○ dependency chains 
○ minification 
● App.js Initialization 
● No closure pattern (factories)
JNOGIN t oU TSH! REE Mobile Developer - Backend Guru 
napofearth.com/jobs UI/UX Designer - QA #ngmeetup

More Related Content

What's hot (20)

PDF
Workshop 12: AngularJS Parte I
Visual Engineering
 
PDF
AngularJS Basics with Example
Sergey Bolshchikov
 
PPTX
AngularJS Internal
Eyal Vardi
 
PDF
준비하세요 Angular js 2.0
Jeado Ko
 
PPTX
AngularJS Architecture
Eyal Vardi
 
PPTX
Practical AngularJS
Wei Ru
 
KEY
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads France
 
PDF
What's new in iOS9
CocoaHeads France
 
PPTX
Angular js
Behind D Walls
 
PPTX
Angularjs Performance
Steven Lambert
 
KEY
AngularJS for designers and developers
Kai Koenig
 
PPTX
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
PDF
AngularJS Basic Training
Cornel Stefanache
 
PPTX
AngularJS $Provide Service
Eyal Vardi
 
PPTX
AngularJS Animations
Eyal Vardi
 
PPTX
The AngularJS way
Boyan Mihaylov
 
PDF
Angular js routing options
Nir Kaufman
 
PDF
Workshop 9: BackboneJS y patrones MVC
Visual Engineering
 
PDF
MVC-RS par Grégoire Lhotelier
CocoaHeads France
 
PPTX
Intro to AngularJs
SolTech, Inc.
 
Workshop 12: AngularJS Parte I
Visual Engineering
 
AngularJS Basics with Example
Sergey Bolshchikov
 
AngularJS Internal
Eyal Vardi
 
준비하세요 Angular js 2.0
Jeado Ko
 
AngularJS Architecture
Eyal Vardi
 
Practical AngularJS
Wei Ru
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads France
 
What's new in iOS9
CocoaHeads France
 
Angular js
Behind D Walls
 
Angularjs Performance
Steven Lambert
 
AngularJS for designers and developers
Kai Koenig
 
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
AngularJS Basic Training
Cornel Stefanache
 
AngularJS $Provide Service
Eyal Vardi
 
AngularJS Animations
Eyal Vardi
 
The AngularJS way
Boyan Mihaylov
 
Angular js routing options
Nir Kaufman
 
Workshop 9: BackboneJS y patrones MVC
Visual Engineering
 
MVC-RS par Grégoire Lhotelier
CocoaHeads France
 
Intro to AngularJs
SolTech, Inc.
 

Viewers also liked (20)

PDF
Angular 2のRenderer
Yosuke Onoue
 
PDF
About WinJS
Osamu Monoe
 
PDF
Intro to Three.js
Kentucky JavaScript Users Group
 
PDF
A-Frame: VR for Web Developers
Kevin Ngo
 
PPTX
WebGL and three.js - Web 3D Graphics
PSTechSerbia
 
PDF
Build the Virtual Reality Web with A-Frame
Mozilla VR
 
PDF
Introduction to WebGL and WebVR
Daosheng Mu
 
PDF
The next frontier: WebGL and WebVR
Codemotion
 
PPTX
WebVR
Arthur Schwaiger
 
PDF
WebVR - MobileTechCon Berlin 2016
Carsten Sandtner
 
PDF
An Introduction to WebVR – or How to make your user sick in 60 seconds
GeilDanke
 
PDF
Bringing Virtual Reality to the Web: VR, WebGL and CSS – Together At Last!
FITC
 
PDF
Virtually Anyone
Tony Parisi
 
PDF
Foundations of the Immersive Web
Tony Parisi
 
PPTX
WebGL, HTML5 and How the Mobile Web Was Won
Tony Parisi
 
PPTX
Hacking Reality: Browser-Based VR with HTML5
Tony Parisi
 
PPTX
Powering the VR/AR Ecosystem 2017-01-17
Tony Parisi
 
PPTX
WebGL: The Next Generation
Tony Parisi
 
PPTX
An Introduction to Web VR January 2015
Tony Parisi
 
PDF
The Immersive Web
Tony Parisi
 
Angular 2のRenderer
Yosuke Onoue
 
About WinJS
Osamu Monoe
 
A-Frame: VR for Web Developers
Kevin Ngo
 
WebGL and three.js - Web 3D Graphics
PSTechSerbia
 
Build the Virtual Reality Web with A-Frame
Mozilla VR
 
Introduction to WebGL and WebVR
Daosheng Mu
 
The next frontier: WebGL and WebVR
Codemotion
 
WebVR - MobileTechCon Berlin 2016
Carsten Sandtner
 
An Introduction to WebVR – or How to make your user sick in 60 seconds
GeilDanke
 
Bringing Virtual Reality to the Web: VR, WebGL and CSS – Together At Last!
FITC
 
Virtually Anyone
Tony Parisi
 
Foundations of the Immersive Web
Tony Parisi
 
WebGL, HTML5 and How the Mobile Web Was Won
Tony Parisi
 
Hacking Reality: Browser-Based VR with HTML5
Tony Parisi
 
Powering the VR/AR Ecosystem 2017-01-17
Tony Parisi
 
WebGL: The Next Generation
Tony Parisi
 
An Introduction to Web VR January 2015
Tony Parisi
 
The Immersive Web
Tony Parisi
 
Ad

Similar to Integrating Angular js & three.js (8)

PDF
Three.js basics
Vasilika Klimova
 
PDF
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Verold
 
ODP
Introduction to threejs
Gareth Marland
 
PDF
Begin three.js.key
Yi-Fan Liao
 
PDF
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
JavaScript Meetup HCMC
 
PDF
WebGL 3D player
Vasilika Klimova
 
PDF
3D everywhere
Vasilika Klimova
 
PDF
Augmented reality in web rtc browser
ALTANAI BISHT
 
Three.js basics
Vasilika Klimova
 
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Verold
 
Introduction to threejs
Gareth Marland
 
Begin three.js.key
Yi-Fan Liao
 
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
JavaScript Meetup HCMC
 
WebGL 3D player
Vasilika Klimova
 
3D everywhere
Vasilika Klimova
 
Augmented reality in web rtc browser
ALTANAI BISHT
 
Ad

Recently uploaded (20)

PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PDF
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PDF
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PDF
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PPTX
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
PDF
Best Software Development at Best Prices
softechies7
 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PPTX
Agentforce – TDX 2025 Hackathon Achievement
GetOnCRM Solutions
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
PDF
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Best Software Development at Best Prices
softechies7
 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
Which Hiring Management Tools Offer the Best ROI?
HireME
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Agentforce – TDX 2025 Hackathon Achievement
GetOnCRM Solutions
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 

Integrating Angular js & three.js

  • 2. Josh Staples @cubicleDowns blog.tempt3d.com github.com/cubicleDowns/ng-three-viewer
  • 3. WHO? Software Engineer @ napofearth.com “Explore Music Visually” PRIVATE BETA (#ngmeetup)
  • 4. TODAY 1. (v1) Angular & Three.js 3D Model Viewer 2. (v2) “Controller as” & prototypal Angular components 3. Grunt & Closure compilation approaches 4. (v3*) Scoped scene graph
  • 5. WHY? Great pattern(s) with low barrier of entry & high productivity All web devs can easily be 3D web devs Isolate THREE code from Angular UI/UX code!
  • 6. THREE WHAT? threejs.org three features ● Renderers ● Scene Graph ● Cameras ● Animation ● Lights ● Materials ● Shaders ● Objects ● Geometry ● Loaders ● Utilities ● Export/Import ● Support ● Examples ● WebVR ● DIYVR Started by Mr. Doob on April 23, 2010
  • 9. CONTROLLER AppController CONTROLLER FileLoaderController UI Elements and Controllers DIRECTIVE INCLUDE file-loader.html DIRECTIVE INCLUDE about.html DIRECTIVE INCLUDE chrome.html DIRECTIVE INCLUDE toolbar.html
  • 10. CONTROLLER FileLoaderController Dependency Injections Across Angular Components CONTROLLER AppController DIRECTIVE select SERVICE StorageService SERVICE MessageBus FACTORY Viewer
  • 11. Viewer.factory('ViewerFactory', … ) { init() home = new Viewer.Scene() animate () render () makeSelection () loadOBJMTL () loadGLTF () loadOBJ () loadJSON () scale () rotate () /** Translate the model along an axis * @param {number} x * @param {number} y * @param {number} z */ translate (x,y,z) home.wrangler.currentModel.position.set(x, y, z); CONTROLLER AppController ● init () ● rotate () ● scale () ● translate () SERVICE MessageBus DIRECTIVE select ● makeSelection () CONTROLLER FileLoaderController ● loadOBJMTL () ● loadGLTF () ● loadOBJ () ● loadJSON () Viewer Factory Interface
  • 12. Viewer Factory Architecture Viewer Factory Singleton function init(params) { home = new Viewer.Scene(params); animate(); } Viewer.Scene() this.scene THREE.Scene() this.renderer THREE.WebGLRenderer() this.wrangler Viewer.Wrangler() function animate () { requestAnimationFrame(animate); render(); } this.setup Viewer.Setup() this.cameras Viewer.Cameras() this.controls THREE.OrbitControls() this.raycaster THREE.Raycaster()
  • 13. USE CASE - User Click, Intersect 3D Model, Return Model Information Angular Directive, <canvas select> elem.on(tap, function(e) x = e.gesture.center.x; y = e.gesture.center.y; // creating NDC coordinates for ray intersection. mouseDown.x = (x / width) * 2 - 1; mouseDown.y = -(y / height) * 2 + 1; ViewerFactory.makeSelection(mouseDown); Viewer Factory, makeSelection makeSelection(mouse): Angular Controller/Factory $scope.$on(‘objectSelected’, function () { // Do Something. }); var vector = new THREE.Vector3( mouse.x, mouse.y, 1).unproject(home.cameras.liveCam); home.raycaster.set(home.cameras.liveCam.position, vector.sub(home.cameras.liveCam.position).normalize()); var intersected = home.raycaster.intersectObjects(home.wrangler.currentModel, true); MessageBus.trigger('objectSelected', intersected[0])
  • 15. MOST PROFITABLE MOVIE? THE SEQUEL!
  • 16. STARRING “Controller as” & as ctrl Annotations as SNAFU
  • 18. Controller as <div id="file-loader" ng-controller="FileLoaderController as loader" ng-show=”loader.visible”> <input type="text" ng-model="loader.data.obj" placeholder="obj file url"> <input type="text" ng-model="loader.data.name" placeholder="unique name"> <button ng-click="otherLoader.loadOBJMTL()">Load OBJ/MTL</button> <button ng-click="loader.loadSampleOBJMTL()">SAMPLE OBJ-MTL</button> Controller level scope :) <div id="file-loader" ng-controller="FileLoaderController" ng-show=”visible”> - - - <input type="text" ng-model="data.obj" placeholder="obj file url"> <input type="text" ng-model="data.mtl" placeholder="mtl file url"> <button ng-click="loadOBJMTL()">Load OBJ/MTL</button> <button ng-click="loadSampleOBJMTL()">SAMPLE OBJ-MTL</button> nearest scope :(
  • 19. Service, !Factory Viewer.ViewerService .prototype init listeners animate render makeSelection loadOBJMTL loadOBJ loadGLTF loadJSON rotate translate scale ● No More Factories ○ closure pattern ● Instead, prototypal Service ○ ‘new’ and this ○ .bind() for callbacks ● Saves Memory, Time, Searches (sorry) ● Single Pattern For Everything! ● IMHO, the best way to code JS
  • 20. /** @ngInject */ Viewer.ViewerService = function($timeout, MessageBus){ this.timeout = $timeout; this.MessageBus = MessageBus; }; Viewer.ViewerService.prototype.init = function (params){ this.home = new Viewer.Scene(params); this.MessageBus.trigger(‘app-ready’); animate(); }; Viewer.factory('ViewerFactory', ['MessageBus', function (MessageBus) function init () {} function makeSelection() {} return { init: init, makeSelection: makeSelection } closure style, ng-annotate : prototypal :) Prototypal Angular
  • 21. CAonnntorotallteior nass /** Service which initiates the THREE.js scene and * provides methods to interact with that scene * * @param {angular.$timeout} $timeout * @param {!Viewer.MessageBus} MessageBus * @constructor * @ngInject */ Viewer.ViewerService = function($timeout, MessageBus){ this.timeout = $timeout; this.MessageBus = MessageBus; }; /** * Translate the model along an axis * @param {number} x * @param {number} y * @param {number} z */ Viewer.ViewerService.prototype.translate = function(x, y, z){ this.home.wrangler.currentModel.position.set(x, y, z) }; /** * @param {number} s */ Viewer.ViewerService.prototype.scale = function(s) { this.home.wrangler.currentModel.scale.set(s, s, s); }; The Closure Compiler can use data type information about JavaScript variables to provide enhanced optimization and warnings.
  • 22. APP INIT (app.js) angular.module('ThreeViewer', ['ngHammer', 'ngRoute', 'LocalStorageModule']) .config(['localStorageServiceProvider',function(localStorageServiceProvider){ …. .config(['$locationProvider', function($locationProvider) { …. $locationProvider.html5Mode(true); .config(['$routeProvider', function($routeProvider){ angular.module('ThreeViewer', ['ngRoute', 'LocalStorageModule']) .config(ThreeViewer.ConfigLocation) …. .directive('select', ['ViewerService', ThreeViewer.SelectDirective.factory]) …. .filter('forceInt', ThreeViewer.ForceInt.factory) …. .service('ViewerService', [MessageBus', ThreeViewer.ViewerService]) …. .controller('AppController', ['$scope', 'ViewerService', ThreeViewer.AppController]); v2 :) v1 :
  • 23. uglify: { ng3: { options: { compress: { drop_console: true }, sourceMap: true, }, files: { 'dist/app.min.js': ['<%= concat.ng3.dest %>'] } } }, command: 'java -jar closure/compiler.jar ' + '--compilation_level SIMPLE_OPTIMIZATIONS' + '--language_in ECMASCRIPT5_STRICT ' + '--angular_pass ' + '--externs closure/externs/angular-1.3.js ' + '--externs closure/externs/three.js ' + '--generate_exports ' + '--manage_closure_dependencies ' + '--js closure/library/base.js ' + '--js <%= app %> ' + '--js <%= ng %> ' + '--js <%= three %> ' + '--js_output_file dist/app.min.js' Minify or Closure Compilation? Closure Compiler ● type checking ● ngInject ● goog.provide / require Grunt ng-annotate ● uglify ● ng-annotate
  • 24. NOGN Eto A TPHPR, ETEWO PATTERNS V1 ● Most Common Angular Pattern ● Grunt uglify / minify ● Factories ● Services ● Filters ● Directives ● Init controllers from DOM V2 ● Prototypal Pattern for Everything! ● Bridge to Angular 2.0 ● Controller as (local scope) ● Closure Compilation ○ type checking ○ -- angular_pass ○ dependency chains ○ minification ● App.js Initialization ● No closure pattern (factories)
  • 25. JNOGIN t oU TSH! REE Mobile Developer - Backend Guru napofearth.com/jobs UI/UX Designer - QA #ngmeetup