Building scalable applications with AngularJS and modern applications infrastructure Based on real life stories.

Slides:



Advertisements
Similar presentations
Implementing Tableau Server in an Enterprise Environment
Advertisements

Become a Panels Rockstar. Audience Survey Have you tried Panels? Are you a …. –Themer / Designer? –Developer / Code Junkie? –Information Architect?
Validation in Angular 1.3 And other angular tidbits.
GruntJS The JavaScript Task Runner. What is Grunt? A build tool for JavaScript Uses Node to run A Gruntfile is actually just a JavaScript file Therefore,
Web Applications Development Using Coldbox Platform Eddie Johnston.
Dhananjay Microsoft MVP
ASP.NET Programming with C# and SQL Server First Edition
LHCbPR V2 Sasha Mazurov, Amine Ben Hammou, Ben Couturier 5th LHCb Computing Workshop
Development of mobile applications using PhoneGap and HTML 5
UNIT-V The MVC architecture and Struts Framework.
What Are My Choices for Building a Modern Web UI?
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQueryAngularJS AngularJS is a client-side JavaScript Framework for adding interactivity to HTML.
Introduction Marko Marić, Danulabs
Introduction to ArcGIS API for JavaScript
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
Putting Performance Best Practices Together to Create the Perfect SPA Chris Love2Dev.com.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Trunica Inc. 500 East Kennedy Blvd #300 Tampa, FL Cross Platform Mobile Apps With Cordova and Visual Studio 2015 © Copyright 2015.
Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.
JavaScript Unit Test by MinHo Kim (Dexter Developer Guide)
ANGULARJS A Gentle Introduction John Madison.NET User Group.
Bundles, Minification Andres Käver, IT Kolledž
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Chapter 1 Ionic Framework (overview) Front-end framework for developing hybrid mobile apps with HTML5 Yeunyong Kantanet School of Information and Communication.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Introduction to ASP.NET development. Background ASP released in 1996 ASP supported for a minimum 10 years from Windows 8 release ASP.Net 1.0 released.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
/16 Final Project Report By Facializer Team Final Project Report Eagle, Leo, Bessie, Five, Evan Dan, Kyle, Ben, Caleb.
AngularJS. What is AngularJS  Javascript Framework  MVC  for Rich Web Application Development  by Google.
AngularJS and SharePoint
AngularJS Best Practices High Quality SPA Applications SoftUni Team Technical Trainers Software University
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Ukázková aplikace je ke stažení na wug
Node.js Modules Header Mastering Node.js, Part 2 Eric W. Greene
DHTML.
The Share Widget Library
WWU Hackathon May 6 & 7.
Extra Course
z/Ware 2.0 Technical Overview
Play Framework: Introduction
AngularJS A Gentle Introduction John
Build Better Apps with MEAN.
SharePoint-Hosted Apps and JavaScript
Angularjs Interview Questions and Answers By Hope Tutors.
Top Reasons to Choose Angular. Angular is well known for developing robust and adaptable Single Page Applications (SPA). The Application structure is.
Introduction to JavaScript
MVC Framework, in general.
Displaying Form Validation Info
MEAN stack L. Grewe.
Step by Step - AngularJS
A second look at JavaScript
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
Introduction to AngularJS
Modern web applications
Refactoring Legacy AngularJS
Modern web applications
Introduction to JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
ASP.NET MVC Web Development
How to debug a website using IE F12 tools
Web Client Side Technologies Raneem Qaddoura
CS5220 Advanced Topics in Web Programming Angular – Services
05 | An Introduction to AngularJS
Blazor A new framework for browser-based .NET apps Ryan Nowak
Presentation transcript:

Building scalable applications with AngularJS and modern applications infrastructure Based on real life stories

Andrey Alpert linkedin.com/in/andreyalpert

The way it was… Mixed client server leads to increased complexity Presentation logic will require a lot of interactions with the server Not a real application, just another website

The way it should be ClientServerData MV* JS framework, Business logic, Mobile-ready layout Data storage. Treated like a black box. Thin-Server, REST, Event

The stack of technologies DRY 1 Declarative 2 Designers friendly 5 Dependency injection 4 Data binding 3

The stack of technologies UI Bootstrap 1 UI Router 2 … and others 5 NG Grid 4 UI Utils 3

Seed projects are sprouting like weeds They all suck

Organizing your app FilesLogicCode

BROS DON’T LET BROS ORGANIZE FILES BY TYPE – the angularjs bro code

Organizing by type Organize by services, controllers, views, etc. 1 One module per directory. 2 Directories become bloated. 4 Poor control over your DI configuration. 3

Folders-by-Feature Structure Organize by component / feature. 1 Directory structure mirrors app structure. 2 Can create angular modules that more accurately reflect injection dependencies. 4 Easier to extract / share components. 3

Folders-by-Feature Structure All files related to a feature live together (html, css, tests). Application-level files live directly under app/ Each section / page of the app has its own folder under app/ Things used throughout the app live under common/ Files related to the app live outside of app/ super-dooper-project/ |- src/ | |- app/ | | |- | |- assets/ | | |- | |- common/ | | |- components/ | | |- services/ | |- less/ | | |- main.less |- bower_components/ | |- bootstrap_components/ | |- angular-bootstrap/ | |- bootstrap/ |- karma/ |-.bowerrc |- bower.json |- build.config.js |- Gruntfile.js |- module.prefix |- module.suffix |- package.json 12435

Application layout Modules should mirror URL Submodules live near main module Submodules listed as dependencies to main module src/ |- app/ | |- user/ | | |- create/ | | |- search/ | | |- user.js | | |- user.ctrl.js | | |- user.less | | |- user.spec.js | | |- user.tpl.html by Josh Miller angular.module(‘app.user', [‘app.user.create’, ‘app.user.search’]); angular.module(‘app.user.create’, []);

Organizing your logic

Angular talks about… Services are app-wide injected singletons. Controllers are bridge between template and the rest of your application logic. Directives are encapsulation of some widget or DOM element behavior. Filters are simple output formatting functions.

That's great! Now you know exactly how to break up your code

UNTIL… That's great! Now you know exactly how to break up your code

The problem with controllers Common functionality List pages, properties pages, etc. 1 Very complicated views can end up having multi-thousand line controllers. 2 How can you break up logic that is going to be used in many controllers? 3

Business Models It is the very heart of your JavaScript application 1 It captures the behavior of the application in terms of its problem domain, independent of the user interface It describes a particular entity in the programming world in terms of data and state It encapsulates your application’s logic and data, provides an API to access and manipulate that data

Business Models angular.module('app', ['ngRoute']); module.factory(‘UserModel’, function (BaseModel) { var UserModel = BaseModel.$extend({ $initialize: function (firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } }); UserModel.prototype.getName = function () { return this.firstName + ' ' + this.lastName; }; return UserModel; }); module.controller('UserController', function ($scope, UserModel) { $scope.user= new UserModel('John', 'Doe'); }); {{ user.getName() }}

Collections A collection represents a group of models known as items 1 Collections are used to store, retrieve and manipulate data 2 Collections are wrappers above dynamic arrays and represent higher level of abstraction. 3

Creating Collection angular.module('app', ['ngRoute']); module.factory(‘UserCollection’, function (BaseCollection, UserCollection) { var UserCollection = BaseCollection.$extend({ $model: UserModel }); UserCollection.prototype.getNames = function () { var names = []; this.$getItems().forEach(function(item){ names.push(item.getName()); }); return names; }; return UserCollection; }); module.controller('UserController', function ($scope, UserCollection) { $scope.userCollection = new UserCollection; $scope.userCollection.$add('John', ‘Doe'); }); {{ name }}

Base Collection The BaseCollection class provides the basic interface to create a Collection class of your own. 1 Commonly used methods should be stored in BaseCollection $add $reset $getItems … 2 BaseCollection proxies to Underscore.js to provide iteration functions $forEach $last $find … 3

Resources A resource lets you interact with different data sources 1 In specific cases it can be a reusable proxy to HTTP/JSON endpoints. 2 Resources are injected into models and collections to provide the ability to send/retrieve data 3 module.service(‘UserResource’, function ($http) { this.list = function() { return $http.get(‘/some/endpoint/url’); }; this.update = function() { ………… }; this.delete = function() { ………… }; });

Organizing your code

Technical debt Tests will be in the next release Code entropy: “if touch that code everything will break” Docs? My code is state of art! TODO/FIXME statements Let’s just copy/paste for now Can be found in any project Do NOT let it grow QUALITY

Code style Readability Good names Tabs/Spaces convention Clear logic Docs and comments jshint+stylish plato code painter editorconfig jscs eslint These are your friends Tools

Single responsibility File per each component 1 Gives the most control over how the injector is configured Much easier to extract code into shared components When testing, only have to load specific module under test angular.module('app', ['ngRoute']); angular.module(‘app’).controller('SomeController', SomeController); function SomeController() { } angular.module(‘app’).factory('someFactory', SomeFactory); function SomeFactory() { }

IIFE Wrap components in an Immediately Invoked Function Expression 1 An IIFE removes variables from the global scope. Protects us from having collisions of variables and many global variables. (function() { 'use strict'; angular.module(‘app’).controller('SomeController', SomeController); function SomeController() { } })();

Resolve promises for your controllers A controller may require data before it loads. That data may come from a promise via a custom factory or $http. Using a route resolve allows the promise to resolve before the controller logic executes, so it might take action based on that data from the promise. (function() { 'use strict'; angular.module(‘app’).config(myConfig); function myConfig($routeProvider) { $routeProvider.when('/avengers', { templateUrl: 'avengers.html', controller: 'AvengersCtrl', controllerAs: 'av', resolve: { resolvedMovies: function(MoviesSCollection) { return MoviesCollection.$loadItems(); } }); } })(); (function() { 'use strict'; angular.module(‘app’).controller(‘AvengersCtrl’, Avengers); function Avengers(resolvedMovies) { var ctrl = this; ctrl.moviesList = resolvedMovies; } })(); Really good for testing as you can mock injectable data

RED (Fail) GREEN (Pass) REFACTOR 1. Write a test that fails 2. Make only enough code for it to pass 3. Improve code quality REPEAT PROCESS

Test driven development TDD/BDD Better code understanding Release faster Motivation Reliability Long (hours) Medium (minutes) Fast (seconds) End 2 End API Services Database Headless Smoke tests unit tests Till first failed Remote Local. Stubs+Mocks Safe refactoring

Test driven development TDD/BDD Better code understanding Release faster Motivation Reliability Fast (seconds) it('should have Avengers controller', function() { //TODO }); it('should find 1 Avenger when filtered by name', function() { //TODO }); it('should have 10 Avengers', function() {} //TODO (mock data?) }); it('should return Avengers via XHR', function() {} //TODO ($httpBackend?) }); // and so on

Сode coverage Not tested area of application Dead code detection Acceptance threshold 70-90% Testing quality Reports Tools Istanbul JSCoverage Blanket coverage > 80% is AWESOME coverals.io codeclimate History and stats service

TDD with… + Cool for E2E tests I’m your test runner. The best one. These two guys are GOOD.

Laziness is the mother of INVENTION

What tasks to automate Source Concatenate Uglify SourceMaps Watch LiveReload Rebuild Serve Preprocess LESS SASS Compass Test Karma Mocha Coverage Assets Templates HTML processing Images optimization Custom ChangeLog Notifications console.debug

Task runner: grunt FILE BASED Good for file operations like copy/move/save. Configuration is over the code 1 TONS OF PLUGINS Many of the tasks you need are already available as Grunt Plugins, and new plugins are published every day. 2 grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { options: { banner: '/*! */\n' }, build: { src: 'src/.js', dest: 'build/.min.js' } });

The streaming build system: gulp Fast (seconds) EASY TO USE By preferring code over configuration, gulp keeps simple things simple and makes complex tasks manageable. 1 STREAM BASED Much more faster then Grunt for file-content processing operations 2 var gulp = require('gulp'); gulp.task('default', function() { // place code for your default task here });

SUMMARY

THANKS.QUESTIONS?