introducing angularjsintroducing angularjs márton salomváry @salomvary [email protected]...

36

Upload: others

Post on 30-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)
Page 2: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Introducing AngularJS

Márton Salomváry

@salomvary

[email protected]

Page 3: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

What is AngularJS

A JavaScript application framework:

● created by Google (engineers)● been around since 2009● stable (1.0.7)● no dependencies (can use jQuery though)

Page 4: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Why AngularJS?

Isn't Backbonegood enough?

Page 5: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Grouping JS frameworks

● low level / DOM jQuery, Prototype

● MVC Backbone, Angular

● full fledged YUI, ExtJS

Page 6: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

What is MVC?

● Model - transient/persisted data

● View - HTML template

● Controller - glue, business logic

Aim: single page applications.

Page 7: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Another grouping of MVC frameworks

By me, for now :)

● no data binding

● has data binding

Page 8: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

What is data binding?

Page 9: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

...but first, how is life without data binding?

Page 10: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

A Handlebars template

{{#each sounds}}<div> <h2>{{title}}</h2> <button class=”play-button”>▶</button></div>{{/each}}

Page 11: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

A Backbone View

events: { 'click .play-button’: 'play' }, initialize: function() { this.model.on('change:foo', this.render, this) this.$('.something').on('keyup', ...) }, play: function() { this.model.set(...) // or manipulate DOM, etc. }

Page 12: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Data binding =☺

<div ng-repeat="sound in sounds"> <h2>{{ sound.title }}</h2> <button ng-click="play()">▶</button></div>

Page 13: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

What happens?

Model changes -> DOM changes● including loop inserts/removes● including fancy manipulations eg. visibility● only the relevant DOM nodes

User interactions -> model changes● model property update● function call

● simple and powerful expression language

Page 14: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

The MAGICis called

DIRTY CHECKING

Page 15: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

"Manual" lifecycle

factory('socket', function($rootScope) { var connection = new WebSocket('ws://localhost') // ...

connection.onmessage = function(message) { $rootScope.$apply(function () { handler.call(null, message.data) }) }})

Page 16: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Using the socket service

socket.onMessage(function(data) { $scope.foo = ... //use data to update $scope})

Updates the DOM automagically afterwards.

Page 17: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Modules

Page 18: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Defining modules

angular.module('app', []).

controller('Playlist', function($scope, $http, socket) { // controller implementation}).

directive('player', function() { //... }).

filter('toUpperCase', function() { //... })

Page 19: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Alternative (ugly) module syntax

Has magic, but breaks when minified:

controller('AnotherCtrl', function($window) { // ...})

Safe to minify:

controller('AnotherCtrl', ['$window', function(the$window) { // ...}])

Page 20: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Modules don't do

● script fetching

● lazy loading

..and there is the minification problem

Page 21: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Dependency Injection

aka. Inversion of Control

● code "asks" for dependencies

● "injector" looks up and passes them in

● easy to swap implementation

● useful for testing/mocking

Page 22: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

With code

controller('Playlist', function ($window, $scope) { // at some point... $window.alert(‘Playlist is over!’)})

// test var mock = { alert: jasmine.createSpy() }, $scope = {}$provide.value('$window', mock)$controller(‘Playlist’, { $scope: $scope })

expect(mock.alert.callCount).toEqual(1)

Page 23: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Directives

Page 24: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Directives

“Directives are a way to teach HTML new tricks”● hook on

○ attribute○ class name○ tag○ comment

● built-in directives, eg. ng-repeat=""● custom directives, eg. <player>

Page 25: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Powerful and complicated myModule.directive('directiveName', function factory(injectables) { var directiveDefinitionObject = { priority: 0, template: '<div></div>', templateUrl: 'directive.html', replace: false, transclude: false, restrict: 'A', scope: false, controller: ["$scope", "$element", "$attrs", "$transclude", "otherInjectables", function($scope, $element, $attrs, $transclude, otherInjectables) { ... }], compile: function compile(tElement, tAttrs, transclude) { return { pre: function preLink(scope, iElement, iAttrs, controller) { ... }, post: function postLink(scope, iElement, iAttrs, controller) { ... } } }, link: function postLink(scope, iElement, iAttrs) { ... } }; return directiveDefinitionObject; });

Page 26: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Controllers and Models

Page 27: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

The view

<div ng-controller="Playlist"> <div ng-repeat="sound in sounds"> <h2>{{ sound.title }}</h2> <button ng-click="play()">▶</button> </div></div>

Page 28: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Controllers and models

controller('Playlist', function ($scope) { $scope.sounds = [ { title: 'One Fine Sound', ... }, { title: 'Whale Song', ... } ]

$scope.play = function() { ... }})

"In Angular, a model is any data that is reachable as a property of an angular Scope object."

MODEL

Page 29: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Scope inheritance

● Organized hierarchically - following the DOM

● Using prototypical inheritance

● Scopes are created

○ for controllers

○ by directives

● Demo: playlist, tricky inheritance

Page 30: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

What else?

Page 31: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Built-in services

● $http, $resource

● $route

● $location

● $timeout

● $log

● $cacheFactory

Page 32: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Summary

Page 33: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

The good parts

● “full stack”

● data binding

● dependency injection

● project structure (angular-seed)

● $scope vs this

Page 34: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

The bad parts

“There are tons of things should be mentioned in the docs lol. I guess this is the kinda the fun part of learning

AngularJS, just like treasure hunting.” (StackOverflow)

● documentation (example)

● complexity (directives!)

● minification vs. module syntax

● too much logic in templates

Page 35: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Alternatives (for data binding)

● Ember.js - even more sophisticated

● Knockout - ugly template syntax

● Epoxy.JS - for Backbone

Page 36: Introducing AngularJSIntroducing AngularJS Márton Salomváry @salomvary salomvary@soundcloud.com What is AngularJS A JavaScript application framework: created by Google (engineers)

Thank you

Márton Salomváry

@salomvary

[email protected]

soundcloud.com/jobs