Angular js - 4developers 12 kwietnia 2013

44
AngularJs 4Developers 12 kwietnia 2013

description

WIdeo i notatki do prezentacji: http://marcin-wosinek.github.io/blog/prezentacja/2013/04/12/AngularJs-4developers-Warszawa.html

Transcript of Angular js - 4developers 12 kwietnia 2013

Page 1: Angular js - 4developers 12 kwietnia 2013

AngularJs4Developers 12 kwietnia 2013

Page 2: Angular js - 4developers 12 kwietnia 2013

AngularJs4Developers 12 kwietnia 2013

Page 3: Angular js - 4developers 12 kwietnia 2013

Kim jestem?● Marcin Wosinek● 5 lat doświadczenia w IT

- WebDev: Javascript- C# dev: UnitTests

● Aktualnie js kontraktor w Poznaniu

Page 4: Angular js - 4developers 12 kwietnia 2013

Wy?

Page 5: Angular js - 4developers 12 kwietnia 2013

Temat

Page 6: Angular js - 4developers 12 kwietnia 2013

Nowa rzeczywistość

Page 7: Angular js - 4developers 12 kwietnia 2013

Tradycyjna architektura stron

Serwer

KLIENT

html z danymi request

Page 8: Angular js - 4developers 12 kwietnia 2013

Podejście aplikacyjne

Serwer

KLIENT

js template restdane

Page 9: Angular js - 4developers 12 kwietnia 2013

Komunikacja z backendem{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }}

Page 10: Angular js - 4developers 12 kwietnia 2013

Zmiany

Page 11: Angular js - 4developers 12 kwietnia 2013

Wyzwania - testowalnośćfunction PasswordCtrl() { var msg = $('.ex1 span'); var input = $('.ex1 input'); var strength; this.grade = function() { msg.removeClass(strength); var pwd = input.val(); password.text(pwd); if (pwd.length > 8) { strength = 'strong'; } else { strength = 'weak'; } msg .addClass(strength) .text(strength);

Page 12: Angular js - 4developers 12 kwietnia 2013

Wyzwania - boilerplate

initialize: function () { this.allCheckbox = this.$('#toggle-all')[0]; this.$input = this.$('#new-todo'); this.$footer = this.$('#footer'); this.$main = this.$('#main');

this.listenTo(app.Todos ,'add',this.addOne); this.listenTo(app.Todos, 'reset', this.addAll); this.listenTo(app.Todos, 'change:completed', this.filterOne); this.listenTo(app.Todos, 'filter', this.filterAll); this.listenTo(app.Todos, 'all', this.render);

app.Todos.fetch();}

Page 13: Angular js - 4developers 12 kwietnia 2013

AngularJs

Page 14: Angular js - 4developers 12 kwietnia 2013

Architektura MV*

ViewModel

View Model

Generowane przez serwisy

$scope

Angularowy html

AngularJs core

Kontroler

Page 15: Angular js - 4developers 12 kwietnia 2013

Widok<ul> <li ng-repeat="todo in todos"> <span></span> </li></ul>

<form ng-submit="addTodo()"> <input ng-model="todoText" /> <input type="submit" value="add" /></form>

Page 16: Angular js - 4developers 12 kwietnia 2013

Filtry<ul> <li ng-repeat="project in projects | filter:search | orderBy:'name'"> <a href=""></a>: </li></ul>

Page 17: Angular js - 4developers 12 kwietnia 2013

Kontrolery

Page 19: Angular js - 4developers 12 kwietnia 2013

Proste obiekty js// Backboneapp.Todo = Backbone.Model.extend({ defaults: { title: '', completed: false }});

// EmberTodos.Todo = DS.Model.extend({ title: DS.attr('string'), isCompleted: DS.attr('boolean') });

// Angulartodos.push({ title: $scope.newTodo, completed: false });

Page 20: Angular js - 4developers 12 kwietnia 2013

Two ways binding

Page 21: Angular js - 4developers 12 kwietnia 2013

Wsparcie dla formularzaprezentacja<!-- Html updated by angular --><form name="exampleForm" class="ng-pristine ng-invalid ng-invalid-required">

Required field: <input ng-model="required" required="" class="ng-pristine ng-invalid ng-invalid-required"></label> <br>

Email field: <input ng-model="email" type="email" class="ng-pristine ng-valid ng-valid-email"></label> <br>

<button ng-disabled="!exampleForm.$valid" disabled="disabled">Submit</button></form>

Page 22: Angular js - 4developers 12 kwietnia 2013

Wstrzykiwanie zależnościfunction HelloCtrl($scope, $window, $log) { $scope.message = 'Display me in view'; $window.alert('Use window via $window service - that improves testability'); $log.log('We can even test console log calls - thats to $log wrapper');}

Page 23: Angular js - 4developers 12 kwietnia 2013

Serwisy// Services that persists and retrieves ToDos from// localStoragetodomvc.factory('todoStorage', function () { var STORAGE_ID = 'todos-angularjs';

return { get: function () { return JSON .parse(localStorage.getItem(STORAGE_ID) || '[]'); },

put: function (todos) { localStorage .setItem(STORAGE_ID, JSON.stringify(todos)); }};});

Page 24: Angular js - 4developers 12 kwietnia 2013

Ścieżki - $routeProviderangular.module('phonecat', []) .config(['$routeProvider', function($routeProvider, $locationProvider) { // $locationProvider.html5Mode(true); $routeProvider .when('/phones', { templateUrl: 'partials/phone-list.html', controller: PhoneListCtrl }) .when('/phones/:phoneId', { templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl }) .otherwise({ redirectTo: '/phones' });}]);

Page 25: Angular js - 4developers 12 kwietnia 2013

Komunikacja z backendem - $resourcemyApp.factory('ProductService', function($resource) { var ProductResource = $resource('/product/:productId')

, ProductService = {};

ProductService.getItem = function (index) { return ProductResource.get({productId: index});}

ProductService.addItem = function (item) { ProductResource.save({}, item));}

return ProductService;

});

function ProductCtrl($scope, ProductService) { // Take products form ProductService and put it on $scope}

Page 26: Angular js - 4developers 12 kwietnia 2013

Directives<ANY class="ng-show: {expression};"><ANY ng-show="{expression}"><ANY class="ng-hide: {expression};"><ANY ng-hide="{expression}">

<ng-view> <any ng-view>

<ANY ng-class="{expression}"> <ANY class="ng-class: {expression};">

<ANY ng-switch="expression"> <ANY ng-switch-when="matchValue1">...</ANY> <ANY ng-switch-when="matchValue2">...</ANY> ... <ANY ng-switch-default>...</ANY></ANY>

Page 27: Angular js - 4developers 12 kwietnia 2013

Yeoman

Page 28: Angular js - 4developers 12 kwietnia 2013

Karma (Testacular)

Page 29: Angular js - 4developers 12 kwietnia 2013

Nawyki

Pisanie callbacków

Page 30: Angular js - 4developers 12 kwietnia 2013

Nawyki

bindowanie

Page 31: Angular js - 4developers 12 kwietnia 2013

Nawyki

Zmienianie DOM w kontrolerze

Page 32: Angular js - 4developers 12 kwietnia 2013

Gotchas - pisanie directivesangular.module('blink', []) .directive('blink', function() { return { restrict: 'E', link: function(scope, elm, attr) { setInterval(function() { elm.toggle(); }, parseInt(attr.interval, 10) || 1000); } }; });

Page 33: Angular js - 4developers 12 kwietnia 2013

Gotchas - ng-model w ng-repeat

// Gotcha!<ul> <li ng-repeat="item in list"> <input ng-model="item" /> </li></ul>

// Work as expected<ul> <li ng-repeat="item in list"> <input ng-model="item.name" /> </li></ul>

Page 34: Angular js - 4developers 12 kwietnia 2013

Gotchas - minimalizowanie kodusyngularApp.controller('ProductCtrl', function($scope,

ProductApi) {

// easy but minification unfriendly

});

syngularApp.controller('ProductCtrl', ['$scope',

'ProductApi', function($scope, ProductApi) {

// minification resistant

}]);

Page 35: Angular js - 4developers 12 kwietnia 2013

Gotchas -

$resource

Page 36: Angular js - 4developers 12 kwietnia 2013

Gotchas - filtry działające tylko na tablicach

filterorderBy

Page 37: Angular js - 4developers 12 kwietnia 2013

Gotchas -

e2e?

Page 38: Angular js - 4developers 12 kwietnia 2013

Gotchas - aktualizowanie scope spoza frameworka

$digest$apply

Page 39: Angular js - 4developers 12 kwietnia 2013

Gotchas - $ w nazwach serwisów

$

Page 40: Angular js - 4developers 12 kwietnia 2013

Pytania

?

Page 41: Angular js - 4developers 12 kwietnia 2013

Materiały

● http://angularjs.org/● http://egghead.io/● http://www.youtube.com/user/angularjs

Page 42: Angular js - 4developers 12 kwietnia 2013

Podsumowanie

Page 43: Angular js - 4developers 12 kwietnia 2013

Kontakt

[email protected]● @MarcinWosinek● linki + notatki:http://bit.ly/4Devs-AngularJs

Page 44: Angular js - 4developers 12 kwietnia 2013

Podziękowania● zdjęcie z publicznością: http://www.flickr.com/photos/dougbelshaw/5604047370/in/photostream● logo BackboneJs: https://github.com/documentcloud/backbone/blob/master/docs/images/backbone.● Two ways binding: http://docs.angularjs.org/guide/dev_guide.templates.databinding● http://karma-runner.github.io/0.8/index.html● http://yeoman.io/