AngularJS Best Practices High Quality SPA Applications SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
Advertisements

AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Methods Writing and using methods, overloads, ref, out SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Design Patterns: Structural Design Patterns
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Entity Framework Performance SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
JavaScript Basics Course Introduction Svetlin Nakov Technical Trainer Software University
ASP.NET MVC Architecture Layouts, Filters, Sections, Helpers, Partial Views, Areas… SoftUni Team Technical Trainers Software University
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
CSS Transitions and Animations Animated HTML Elements SoftUni Team Technical Trainers Software University
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
High-Quality Code: Course Introduction Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Static Members Static Variables & Methods SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
AngularJS Best Practices High Quality SPA Applications SoftUni Team Technical Trainers Software University
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Services & Dependency Injection
Introduction to MVC SoftUni Team Introduction to MVC
Application Architecture, Redux
Mocking tools for easier unit testing
Browser Routing Design Patterns in JS
Entity Framework: Code First
MVC Architecture. Routing
Creating React Components with JSX and Babel
Entity Framework: Relations
ASP.NET MVC Introduction
Best practices and architecture
Extending functionality using Collections
Making big SPA applications
Introduction to TypeScript & Angular
JavaScript Frameworks & AngularJS
Presentation transcript:

AngularJS Best Practices High Quality SPA Applications SoftUni Team Technical Trainers Software University

Table of Contents 1.File & Folder Organization 2.Organizing Modules 3.Configuring and Running Apps 4.Naming Conventions 5.Controllers, Services, Directives Roles 6.Dealing with Scope 7.Communication between components 8.Breaking page into components 2

File & Folder Organization How to structure Angular code? 3

4  Angular Seed  /components  viersion-directive.js  interpolate-filter.js  /view1  View1.html  View2.js  /view2  app.css  app.js  index.html File Organization – Angular Seed  By Type  /js  /controllers  /services  /filters  /directives  app.js  /partials  /styles  index.html

5  By Feature  /authentication  /ads  publicAdsCtrl.js  adsDataSvc.js  publicAds.html  /profile  /styles  index.html File Organization – Angular Seed  By Feature then Type  /authentication  /ads  /controllers  publicAdsCtrl.js  /services  adsDataSvc.js  /partials  publicAds.html  /styles  index.html

Dealing with Angular Modules What are modules and how to use it? 6

7  Modules are NOT containers for the objects in your application  Modules does NOT provide a sort of namespacing  Angular is creating an injector  The injector is the namespace for the objects  There is only one injector for a given Angular application  Only one object of a given name can exist in the injector Angular Modules

8 Angular Modules - Example angular.module('app1', ['app2', 'app3']); angular.module('app2', []); angular.module('app3', []); angular.module('app2').controller('Ctrl1‘, function($scope) { $scope.name = 'Ctrl1 in App2'; $scope.name = 'Ctrl1 in App2';}); angular.module('app3').controller('Ctrl1‘, function($scope) { $scope.name = 'Ctrl1 in App3'; $scope.name = 'Ctrl1 in App3';}); {{name}} {{name}} </body> Prints: Ctrl1 in App3

9 Angular Injector Module1 Module2 Ctrl1 Ctrl2 Injector Service1 Directive1 Ctrl3 Ctrl4 Service2Directive2

10  One module  Two modules  Multiple modules Organizing Modules SubModule1SubModule2 MainModule CommonModule MainModule Module SubModule3

Configuring and Running Applications Angular.run & Angular.config

12  Angular.config()  Configuration blocks get executed during the provider registrations and configuration phase (pre-init). Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.  Angular.run()  Run blocks get executed after the injector is created (post-init) and are used to kickstart the application. Everything can be injected into the run blocks because Angular has already bootstrapped the dependency injection. Configuring and running applications

13  app.config()  app.run()  directive's compile functions (if they are found in the DOM)  app.controller()  directive's link functions (again, if found) Angular execution order

Naming Conventions 14

15  Controllers  schedule or scheduleCtrl  Services  schedule or scheduleSvc  Filters  rating or ratingFilter  Directives  smallSchedule or smallScheduleDrct or small-schedule  Partials  schedule - schedule.html or scheduleDisplay.html Naming Conventions

Components Responsibilities Design guidelines for different components 16

17  Controllers  Setup the Scope  View Interaction  Coordinate view and model  Services  Handle non-view logic  Communicate with the server  Hold data and state Components Responsibilities  Directives  Manipulate DOM  Receive view events  Views  Display the application  Declare bindings & directives

18  Your components should do only what is responsible for  Services  Single Responsibility Principle  Cohesive  Loosely Coupled  Good Interface  Testable Design guidelines

19  Controllers  Coordinate view and model  Directives Purposes  Widgets  DOM Events  Functionality Design guidelines

Dealing with the Scope 20

 Angular always starts with the Root Scope  All scopes in application are in hierarchy  All scopes belong to exactly one element  Relationship types  Shared scope  Inherited scope  Isolated scope Scope Overview

AngularJS Batarang – Chrome Extension Live Demo 22

Creating different scopes Live Demo Sharing ScopeInheriting Scope Isolating Scope

 Inherited Scope  Everything defined in parent scope is available in child scopes  Communicating with Events  Communicating with Services Communication Between Components $scope.$on('categoryClicked‘, function(event, category) { }); $rootScope.$broadcast('category:clicked', category); $scope.clicked = function(category) { categorySvc.clicked(category) } $scope.categories = categorySvc.getFilteredCategories();

25  Inline controllers – ngController  ngInclude & ngController  Directives Breaking Page Into Components … … <side-bar></side-bar> app.directive('navBar', function() { return { return { restrict: 'E', templateUrl: 'sidebar.html', controller: 'sidebarCtrl' restrict: 'E', templateUrl: 'sidebar.html', controller: 'sidebarCtrl' }}); }});

26  Flash Of Uncompiled Content  Avoiding FOUC  ngCloak – Put it only at elements with bindings (not at body )  ngBind – Add in bind elements (don’t leave them empty)  Waiting image Avoiding FOUC in Views

27  Writing valid HTML can be important  Use only element and attribute forms of directives  Use data prefix if you are using attribute form of directives Writing Valid HTML with AngularJS </body>

28  Manually minsafe  Use array to declare your dependencies  Use ngMin – Utility that minsafe your code 1. Install ngmin 2. Minsafe you code 3. Minify your minsaved code Minification npm install ngmin -g ngmin app.js app.save.js app.controller('mainCtrl', ['$scope', 'mySvc', function($scope, mySvc) { $scope.data = mySvc.data; $scope.data = mySvc.data;}]);

? ? ? ? ? ? ? ? ? AngularJS Best Practices

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International" licenseCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International 30  Attribution: this work may contain portions from  "SPA with AngularJS" course by Telerik Academy under CC-BY-NC-SA licenseSPA with AngularJSCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg