Angular.js - JS Camp UKraine 2013

48
AngularJS Superheroic JavaScript Framework Friday, February 22, 13

description

My talk about Angular.js at JS Camp Ukraine Feb 22, 2013, why Angular.JS is valuable

Transcript of Angular.js - JS Camp UKraine 2013

Page 1: Angular.js - JS Camp UKraine 2013

AngularJSSuperheroic JavaScript Framework

Friday, February 22, 13

Page 2: Angular.js - JS Camp UKraine 2013

Maksym Klymyshyn

CTO at GVMachines Inc.(Zakaz.ua)

@maxmaxmaxmax

Friday, February 22, 13

Page 3: Angular.js - JS Camp UKraine 2013

Background

• Lead both-end developer @oDesk Inc.

• Front-end consultant @Helios

• OpenSource contributor

• Conferences organizer

Friday, February 22, 13

Page 4: Angular.js - JS Camp UKraine 2013

Now

• Doesn’t work with front-end 6+ month

• Never used AngularJS on production

• Understand VALUE of AngularJS for business

Friday, February 22, 13

Page 5: Angular.js - JS Camp UKraine 2013

Friday, February 22, 13

Page 6: Angular.js - JS Camp UKraine 2013

Toc

• What is AngularJS

• Technical details

• Numbers and value for business owners

Friday, February 22, 13

Page 7: Angular.js - JS Camp UKraine 2013

AngularJS?

Friday, February 22, 13

Page 8: Angular.js - JS Camp UKraine 2013

What is it?

MVC framework by Googlebuilt around belief that

declarative programming for building UIs,

while imperative programming is excellent for expressing business logic

Friday, February 22, 13

Page 9: Angular.js - JS Camp UKraine 2013

Author

• Originally written by Misko Hevery

• Created at Google as internal project

Friday, February 22, 13

Page 10: Angular.js - JS Camp UKraine 2013

Key points• 2-way data binding

• dependency injection

• directives (custom tags and attrs)

• Form validation

• RESTful

• Binders, watches, events etc.

Friday, February 22, 13

Page 11: Angular.js - JS Camp UKraine 2013

Difference

• Lack of boilerplate code

• Easy-to-create reusable components

• Dead simple templates, no way to put business logic

• Simple support of UI consistency

• All-in-one solution

Friday, February 22, 13

Page 12: Angular.js - JS Camp UKraine 2013

It fits well

• CRUD applications

• CRMs, Admin-panels

• Single-page apps

• Push-notification-based apps

• Mobile apps

Friday, February 22, 13

Page 13: Angular.js - JS Camp UKraine 2013

That won’t fly

On Jun 8, 4:34 am, ganaraj p r <[email protected]> wrote: > There should be some generic set of apps> for which angular would be > considered the wrong choice? > > Anyone figured out what set this would be?

Good: Gmail Bad: Wikipedia

Friday, February 22, 13

Page 14: Angular.js - JS Camp UKraine 2013

Definitely not the first toolto develop a game

Friday, February 22, 13

Page 15: Angular.js - JS Camp UKraine 2013

Technical details

Friday, February 22, 13

Page 16: Angular.js - JS Camp UKraine 2013

Bootstrap

$ yeoman init angular $ lsGruntfile.js! ! test apptestacular.conf.js package.json

Friday, February 22, 13

Page 17: Angular.js - JS Camp UKraine 2013

Bootstrap

app/viewsapp/scripts/vendorapp/scripts/controllers/app/scripts/app.js

app/index.html

Structure

Server$ yeoman server

Friday, February 22, 13

Page 18: Angular.js - JS Camp UKraine 2013

Friday, February 22, 13

Page 19: Angular.js - JS Camp UKraine 2013

Architecture

Friday, February 22, 13

Page 20: Angular.js - JS Camp UKraine 2013

Architecture

Friday, February 22, 13

Page 21: Angular.js - JS Camp UKraine 2013

Typical app

Friday, February 22, 13

Page 22: Angular.js - JS Camp UKraine 2013

Major parts

• Scope

• Model

• View

• Controller

• Directive

• Filters

• Module

• Injector

• Services

Friday, February 22, 13

Page 23: Angular.js - JS Camp UKraine 2013

Scope

• Provide context for expressions

• Scopes are hierarchical nested

• Isolated scopes (for widgets)

• Watches

The scope detecting changes to the model section and provides the execution context

for expressions.

Friday, February 22, 13

Page 24: Angular.js - JS Camp UKraine 2013

Scope

Friday, February 22, 13

Page 25: Angular.js - JS Camp UKraine 2013

Model

Model is view’s data.No requirements. No restrictions

Friday, February 22, 13

Page 26: Angular.js - JS Camp UKraine 2013

Model

$scope.data = { title: “Test title”, id: 1, content: “Some content”};

<div id=”{{ data.id }}”> <h1>{{ data.title }}</h1> <p>{{ data.content }}</p></div>

Controller:

View:

Friday, February 22, 13

Page 27: Angular.js - JS Camp UKraine 2013

A W E S O M E

Friday, February 22, 13

Page 28: Angular.js - JS Camp UKraine 2013

View

• View looks like template

• It’s declarative

• Very close to HTML markup

• It handle directives

• Update DOM partially

Friday, February 22, 13

Page 29: Angular.js - JS Camp UKraine 2013

View

<div ng-repeat=”p in content”> <p>{{ p }}</p></div>

$scope.content = [ “paragraph 1”, “paragraph 2”, “paragraph 3”];

Friday, February 22, 13

Page 30: Angular.js - JS Camp UKraine 2013

View

Friday, February 22, 13

Page 31: Angular.js - JS Camp UKraine 2013

Controller

• Manage application behavior

• Support of many views, for example desktop and mobile versions with different views but same controller

JavaScript code behind the view

Friday, February 22, 13

Page 32: Angular.js - JS Camp UKraine 2013

Controller

MyModule.controller('MainCtrl', [ "$scope", function($scope) {

$scope.name = "Max"; $scope.content = [ "paragraph 1", "paragraph 2", "paragraph 3" ];}]);

Friday, February 22, 13

Page 33: Angular.js - JS Camp UKraine 2013

Directive

• Provide ability to extend HTML

• Custom tags, attributes or classes

• Bi-directional binding of nested scopes

• Async directives processing

Directive is a behavior orDOM transformation

Friday, February 22, 13

Page 34: Angular.js - JS Camp UKraine 2013

Directive

MyApp.directive('message', function factory() { return { template: '<div class="msg" ng-transclude>’ + ‘</div>', replace: true, // replace original tag transclude: true, // put content to template // Element, possible Attr, Class, coMment restrict: 'E' }; });

Friday, February 22, 13

Page 35: Angular.js - JS Camp UKraine 2013

Filters

• Very useful with locales

• Number formatters, text highlighting and so on

Perform data transformation

Friday, February 22, 13

Page 36: Angular.js - JS Camp UKraine 2013

Filters

MyApp.filter('title', function factory() { return function (input) { var ch = input.substring(0, 1); var rest = input.substring(1, input.length); return ch.toUpperCase () + rest; }; });

Friday, February 22, 13

Page 37: Angular.js - JS Camp UKraine 2013

ModuleServices, directives, filters,

and configuration

var MyApp = angular.module('MyApp', []) .config([ '$routeProvider', function($routeProvider) { $routeProvider.when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }).otherwise({ redirectTo: '/' }); }]);

Friday, February 22, 13

Page 38: Angular.js - JS Camp UKraine 2013

InjectorService locator,

Dependency Injection pattern

Friday, February 22, 13

Page 39: Angular.js - JS Camp UKraine 2013

Why? Minification$ yeoman build...# Error: Unknown provider: aProvider <- a

... 'MainCtrl', ["$scope", function($scope) { ...

... 'MainCtrl', function($scope) { ...

Wrong

Correct

Friday, February 22, 13

Page 40: Angular.js - JS Camp UKraine 2013

Services

• $compile - dynamic views compilation from string

• $cookies - read/write cookies

• $locatoin - work with browser’s location

• $resource - factory to work with third-party services/REST

Friday, February 22, 13

Page 41: Angular.js - JS Camp UKraine 2013

Toolchain

• End-to-end - e2e testing, similar to Jasmine

• Yeoman - dev & build server, projects skeleton

• Batarang - plugin for Chrome

• Testicular - tests framework

Friday, February 22, 13

Page 42: Angular.js - JS Camp UKraine 2013

Third-party apps

• Angular-UI

• Anglebars.js

• jQuery mobile + Angular.js

• ngGrid

Friday, February 22, 13

Page 43: Angular.js - JS Camp UKraine 2013

Show me the money

Friday, February 22, 13

Page 44: Angular.js - JS Camp UKraine 2013

Rapid prototyping

• No boilerplate code === less code to maintain

• Simple tests suite === more motivation to write more tests

• Two-way data binding === easy to develop push-based services, like PBX<->CRM, chats, notifications etc.

Friday, February 22, 13

Page 45: Angular.js - JS Camp UKraine 2013

Structured by design• Well structured codebase by design

• Easy-to-understand templates

• Fast and maintainable prototypes may become fast hypothesis check and robust solution. Recommended for startups

Friday, February 22, 13

Page 46: Angular.js - JS Camp UKraine 2013

Community

• 7001 followers on github

• 1158 forks!

• 1619 closed issues

• 391 open issues

• 9371 followers on twitter

• 316 videos on YouTube

• 127 presentations on SlideShare

Friday, February 22, 13

Page 47: Angular.js - JS Camp UKraine 2013

Text

AWESOME

Friday, February 22, 13

Page 48: Angular.js - JS Camp UKraine 2013

Thanks. Questions?

Friday, February 22, 13