25
AngularJS A radically different way of building Single Page Apps by Jivko Petiov

AngularJS A radically different way of building Single Page Apps

Embed Size (px)

DESCRIPTION

AngularJS A radically different way of building Single Page Apps. b y Jivko Petiov. Agenda. Demo - Hello World What is Angular Demo - How a real-world app is made Conclusion – why and when to use it. Demo - Hello World. What is Angular. - PowerPoint PPT Presentation

Citation preview

Page 1: AngularJS A  radically different way of building Single Page  Apps

AngularJS A radically different way of building Single Page Apps

by Jivko Petiov

Page 2: AngularJS A  radically different way of building Single Page  Apps

Agenda

• Demo - Hello World

• What is Angular

• Demo - How a real-world app is made

• Conclusion – why and when to use it

Page 3: AngularJS A  radically different way of building Single Page  Apps

Demo - Hello World

Page 4: AngularJS A  radically different way of building Single Page  Apps

What is Angular

• What is a SPA (Gmail, Github, Hotmail, Trello, Facebook)

• JavaScript Framework for building SPA apps

• “Angular is what HTML should have been, if it has been specifically designed for AJAX apps”

Page 5: AngularJS A  radically different way of building Single Page  Apps

JavaScript Frameworks

• Backbone.js • Ember.js • KnockoutJS • AngularJS• Dojo • YUI • Agility.js • Knockback.j

s • CanJS

• SproutCore • Polymer • Cujo.js• dermis • Spine.js• Ext.js• Sammy.js • JavascriptMV

C• Epitome

• Kendo UI • PureMVC • Olives• PlastronJS • Dijon• rAppid.js • Batman.js• React • Exoskeleton

Page 6: AngularJS A  radically different way of building Single Page  Apps

Architectural Patterns

• MVC• MVP• MVVM• MVA (Model View Adapter)• PAC (Presentation Abstraction Control)• HMVC (Hierarchical Model-View-Controller)

Page 7: AngularJS A  radically different way of building Single Page  Apps

Why so Complicated

Page 8: AngularJS A  radically different way of building Single Page  Apps

Angular Pattern?

MVW

Page 9: AngularJS A  radically different way of building Single Page  Apps

Angular Pattern?

Model View Whatever

MV*

Page 10: AngularJS A  radically different way of building Single Page  Apps

Angular Architecture

Page 11: AngularJS A  radically different way of building Single Page  Apps

Demo Time

Pray to the Demo Gods

Page 12: AngularJS A  radically different way of building Single Page  Apps

Views

• Views = HTML

• Directives are reusable components within the View; They are similar to jQuery Plugins, but much more than that

• Declarative DSL – controversial and yet powerful

Page 13: AngularJS A  radically different way of building Single Page  Apps

Directive Examples

<rating max="5" model=“data.rating" />

<tabs><tab title="Tab 1">…</tab><tab title="Tab 2">…</tab>

</tabs>

<span tooltip="{{model.text}}">…</span>

Page 14: AngularJS A  radically different way of building Single Page  Apps

jQuery vs Angular<div id="tabs"> <ul> <li><a href="#tabs-1">Tab 1</a></li> <li><a href="#tabs-2">Tab 2</a></li> <li><a href="#tabs-3">Tab 3</a></li> </ul> <div id="tabs-1">...</div> <div id="tabs-2">...</div> <div id="tabs-3">...</div></div>

$( "#tabs" ).tabs();

<tabs> <tab title="Tab 1">…</tab> <tab title="Tab 2">…</tab> <tab title="Tab 3">…</tab></tabs>

Page 15: AngularJS A  radically different way of building Single Page  Apps

Controllers

• No Reference to DOM / Views

• The “code-behind” of the view

o What should happen if user does X

o Where do I get X from

Page 16: AngularJS A  radically different way of building Single Page  Apps

Controller Example

app.controller("myController", function($scope, backendService) {

$scope.people = [ { name: “Person 1”, city: “Sofia” }, { name: “Person 2”, city: “Moscow” },{ name: “Person 3”, city: “New York” }

];

$scope.addPerson = function(person) {backendService.addPerson(person);

};}

Page 17: AngularJS A  radically different way of building Single Page  Apps

Models and Scope

• Model = data = JSON

• Scope is container for model, one scope per controller

• $scope.people = [ { name: “Person 1”, city: “Sofia” }, { name: “Person 2”, city: “Moscow” },{ name: “Person 3”, city: “New York” }

]

Page 18: AngularJS A  radically different way of building Single Page  Apps

Services

• Usually no reference to DOM

• Singletons, SRP, Dependency Injection

• How do I do X?

• Server communication, business logic, helpers, modal dialogs, error handling, sharing data between controllers

Page 19: AngularJS A  radically different way of building Single Page  Apps

Service Example

app.factory('myService', function($http, $q) { return { getAllItems: function() { var deferred = $q.defer(); $http.get(“/api/getItems").success(function(data) {

deferred.resolve(data);}).error(function(){

deferred.reject(“Error Message");});

return deferred.promise; } } }

Page 20: AngularJS A  radically different way of building Single Page  Apps

Service Example (continued)

app.controller("myController", function($scope, myService) {

myService.getAllItems().then(function(data) {$scope.items = data;

});}

app.controller("myController2", function($scope, myService) {

$scope.items = myService.getAllItems();}

Page 21: AngularJS A  radically different way of building Single Page  Apps

Filters

• currency• date• filter• json• limitTo

• lowercase• number• orderBy• uppercase

• Simple formatters of data

Page 22: AngularJS A  radically different way of building Single Page  Apps

Filter Example

myApp.filter('capitalize', function() { return function(input, scope) { return input.substring(0,1).toUpperCase() +

input.substring(1); }});

<div>{{person.Name | capitalize }}</div>

Page 23: AngularJS A  radically different way of building Single Page  Apps

Conclusion

• SPA, Data-driven apps, CRUD

• The role of the server-side – HTML vs JSON:

o Don’t send HTML if you end up parsing it client-side to do some calculations over it

o Don’t send JSON if all you do with it is just put it inside the DOM

Page 25: AngularJS A  radically different way of building Single Page  Apps

Thanks to our Sponsors:

Diamond Sponsor:

Gold Sponsors:

Swag Sponsors: Media Partners:

Technological Partners:

Silver Sponsors:

Bronze Partners: