SlideShare uma empresa Scribd logo
1 de 46
Baixar para ler offline
Require.js Ground Up!
Asynchronous Module Dependency Injector
Dr. SYED AWASE KHIRNI
1
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
Works with IE6+, Firefox2+, Safari 3.2+, Chrome 3+, Opera 10+
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
2
Encapsulation
A language mechanism for
restricting access to some
of the Object’s
components
Protect variable from
unwanted changes
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
3
JavaScript is prototype-based
Convention in JavaScript
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
4
Customer
_order
_gender
_cartSummary
Public access for everyone
Privileged Method
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
5
Customer
Public access for everyone
order
gender
cartSummary
RequireJS
• An Asynchronous JavaScript module and file
loader, optimized for web browser usage.
• Adhering to SOLID Principles – Single
responsibility, the code is separated into
modules, dependencies need to be configured
when loading files.
• RequireJS creates code on demand and adds it to
the page header as an script tag (using
head.appendChild()) taking care of all its
dependencies (loading them as well in the proper
order).
6Copyright © Territorial Prescience Research I P Ltd, 2007-2015
Problem Statement
• Increase in code complexity for enterprise class
applications.
• Web applications serving multi-modal devices
• Assembly gets harder
• Compliance with SOLID Principles
• Developers wants discrete JS files/modules
• Increase web application performance with fewer
HTTP calls for resources using bundling and
minification
• Encapsulation of data
Copyright © Territorial Prescience Research I P Ltd, 2007-2015 7
Traditional Approach
• Slow
– Many HTTP requests to the server for fetching
resources
– Blocking render
• Manual Dependencies
• Lack Encapsulation
Copyright © Territorial Prescience Research I P Ltd, 2007-2015 8
Solution
• Some sort of #include/import/require for dependency
injection
• Ability to load nested dependencies asynchronously
• Ease of use for developer backed by an optimization tool
that helps deployment.
• Transform Websites into Web Apps
– More
– More Complexity
– Ease of Development
• Adhering to Design Principles
– Separate into reusable components
– Avoid global namespace pollution
Copyright © Territorial Prescience Research I P Ltd, 2007-2015 9
Module Pattern Locally Scoped Object Literal
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
10
Where do modules live?
• Global? Or Local? Are they reusable?
Module is a resource
Modules may require some dependencies
Module Pattern
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
11
Customer
gender
order
Exports
cartSummary
Stacked locally scoped object
literal Revealing Module Pattern
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
12
• public pointers to methods inside the
Module’s scope are revealed.
Module Pattern
Advantage
• Easy to implement
• Keep private data safe
Disadvantage
• Hard to inherit
• Lots of modules
• Complex dependency
injection
• One file per module
• Lots of files
– Performance issue
– Asynchronous loading
required
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
13
Augmentation
• One of the limitations of the
module pattern so far is that
the entire module must be in
one file.
• In large code-base, modules
are extensible components.
• Augment modules, provides
the functionality to import the
module, and add or extend the
functionality and then export
it.
• Tight Augmentation implies a
set loading order, but allows
overrides.
• Complex dependency
– load modules by order
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
14
Extending an existing module
TightAugmentation
Loose Augmentation
• In the above Augmentation
scenario, initial module
creation to be first and then
the augmnetation has to
happen second in a specified
order.
• Alternatively, it doesn’t always
happen in the same order, to
improve performance,
javascript allows to load scripts
asynchronously.
• We can create flexibile multi-
part modules that can load
themselves in any order with
loose augmentation.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
15
Extending an existing module
3 RequireJS API
• Define() : define and register dependencies
– Define () must return a Object which is cached in a
registry
• Require() :callback function invoked when all defines()
have completed
– Acts as intialization or root of the dependency tree
– Starts the cascade of dependency checks and script
loading
– Starts the cascade of define() triggers
• Config() :configure source paths, shims and aliases
– Requirejs.config() – configuration or aliases and shims
Copyright © Territorial Prescience Research I P Ltd, 2007-2015 16
Sub-modules
• It is to create a sub-set
within an existing
module.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
17
CommonJS
• A module ecosystem for
Javascript:
– Require() method
– Exports object
• Created for javascript
on the server
• Doesn’t work well in the
browser natively
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
18
Asynchronous Module Definition (AMD)
• A javascript specification that defines an API for
defining code modules and their dependencies
and loading them asynchronously if desired.
• Created to work directly in browsers
• It specifies a mechanism for defining modules
where the module and its dependencies can be
asynchronously loaded.
• RequireJS is an implementation of this
specification.
Copyright © Territorial Prescience Research I P Ltd, 2007-2015 19
AMD
• Asynchronous
– Don’t block browser
– Parallel download and
interpretation
– Callbacks everywhere
• Loaded via script tag
injection (not XHR +eval)
– Debugging
– X-Domain
• Each module says which
modules it needs.
• The module’s “factory” is
called after all of those
modules are loaded.
• Efficient Loading
– Flexible development life
cycle
– Useful for a few or external
resources
• Production/Deployment
– Easy packaging
– Single file or multiple
packages
– Command line tools to
resolve dependencies at build
time.
– Inline text resources like
template files
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
20
AMD
• Predecessors
– DOJO
– LABJS
– COMMONJS
• Current
– Jquery 1.7
– Dojo 1.7
– MooTools 2.0
– EmbedJS
– Backbone/underscore will
• AMD Drawback
– No Standards
– Lots of up-front work
– No semantic versioning
– Heavyweight tools
(RequireJS)
• Alternative to AMD
– Browserify
• Simple syntax: require
• Great if you are into Node+
npm
• Intended for bundling, not
so much for async module
loading.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
21
Using AMD
Copyright © Territorial Prescience Research I P Ltd, 2007-2015 22
RequireJS
• AMD Implementation
by James Burke
• Asynchronous resource
loader
• Call anytime
• Trace nested
dependencies
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
23
• Avoid polluting the
global namespace
• Optimization tool for
JS/CSS
RequireJS (Loading Scripts)
• Creates script tags on
demand and adds them
to the head.
• Non-blocking page
rendering approach for
better web experience
• Works after page load
• Not constrained by
same origin policy
• Need to know the
dependencies upfront
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
24
RequireJS Module Pattern
• Allows the dependencies to be
loaded as fast as possible, even
out of order, but evaluated in the
correct dependency order, and
since global variables are not
created. It makes it possible to
load multiple versions of a
module in a page.
• Dependency management
– Dependencies can be nested
– Load once, use in multiple
locations
– Plugins for text dependencies, load
order, etc
– Compatible with JavaScript
protypal inheritance
• Extension of widely used JS
module pattern
• Minimal boilerplate code for
developers to adopt
• Defines a well-scoped object that
avoids polluting the global
namespace.
• Explicitly lists its dependencies
• Receives dependencies as
arguments to the function that
defines the module
• Does not need globals to refer to
other modules
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
25
RequireJS Optimizer
• It includes an
optimization tool to
help deployment
– Combine modules into
fewer files for
deployment
– Concatenate and minify
JS files
– Shallow file exclusion for
tweaking or debugging
individual JS files
• Concatenate
CSS@import files
• Minify CSS files
• File systems vs URLs
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
26
Using require() vs define()
Require()
• Used to run immediate
functionalities
• Require calls use the requireJS
baseURL(prepending it) in all
cases but
– When query string parameters
are present
– When absolute path are
present
– A protocol like http or https is
contained in the url
– When the path ends with .js (js
extensions)
Define()
• Used to define modules for use in
multiple locations.
• Reusable modules are usually defined
• Used for module definitions. A module
can be basically any kind of object and
is not available in the global
namespace.
• Defining modules using requireJS is that
no global objects are used.
• Modules are loaded (potentially) in
different order as they are requested,
but they are evaluated in the same
order, so it is possible to load multiple
version of the same module in the
same page.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
27
Managing the Order of Dependent
Files
• RequireJS uses AMD for
loading files. Each
dependent module will
start loading through
asynchronous requests in
the given order.
• Even though the file order
is considered, it is not
guaranteed that the first
file is loaded before the
second file due to the
asynchronous nature.
• requireJS uses shim
config to define the
sequence of files which
need to be loaded in
correct order.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
28
Specifying Configuration
• RequireJS comes with a
lot of configuration
options to easily create
different configurations
for different situations
– Development
– Production/deployment
– testing
• Yeoman generators
generally set this up for
us.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
29
Configuration Options for requireJS
Option Description
baseUrl Base path appended to all modules loaded via requireJS, default path of the HTML page
that loads requireJS, in case the “data-main” attribute is used, the default is the path of
this attribute.
Paths Path mapping for modules, relative to the baseUrl
Shim Mechanism to support the configuration and usage of non-AMD libraries with requireJS.
It defines their dependencies, exports them using their global variables or calls their no
conflict functions explicitly.
Map Different paths for some modules, useful in case the same application needs to use
different versions of the same module for different purposes and needs to share their ids
in order to use the same code for different situations.
Config A way of passing configuration to the modules. This is done by using the special module ‘
module’ and its function module.config()
urlArgs Query parameters that are added to all resources loaded via require JS. Very useful for
cache busting during development, to be used with care on production
waitSeconds Number of seconds before throwing a timeout, default 7 seconds, 0 means no time out
will be thrown.
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
30
Configuration file
Copyright © Territorial Prescience Research
I P Ltd, 2007-2015
31
Requirejs.config
© 2012-2016 TPRI Syed Awase 32
Requirejs.config
© 2012-2016 TPRI Syed Awase 33
View
© 2012-2016 TPRI Syed Awase 34
Example2
© 2012-2016 TPRI Syed Awase 35
Counter.js
Example 2
© 2012-2016 TPRI Syed Awase 36
Test1.js
Test2.js
Example 2: Using RequireJs
© 2012-2016 TPRI Syed Awase 37
Main.js
Example2: View
© 2012-2016 TPRI Syed Awase 38
Example 3:viewModel
© 2012-2016 TPRI Syed Awase 39
Example3:requirejs.config
© 2012-2016 TPRI Syed Awase 40
Example3:View
© 2012-2016 TPRI Syed Awase 41
Example
4:accountModule.js/CartModule.js
© 2012-2016 TPRI Syed Awase 42
Example4: requirejs.config
© 2012-2016 TPRI Syed Awase 43
Example4:View
© 2012-2016 TPRI Syed Awase 44
r.js- the RequireJS Optimizer
• Build and Deploy RequireJS Apps
– Deploy uncompressed
– Deploy concatenate and uglified
• Plugins available for
– Grunt (grunt-requirejs)
– Gulp(gulp-requirejs)
– Broccoli (broccoli-requirejs)
© 2012-2016 TPRI Syed Awase 45
sak@sycliq.com
sak@territorialprescience.com
Contact Us
Thank You
We also provide Code Driven Open House Trainings
For code driven trainings for Technology Firms
Reach out to us +91-9035433124
We are hardcore
Technologists/Architects/Programmers
trainings are offered by Dr. Syed Awase
Current Offerings
• AngularJS 1.5.x
• Typescript /CoffeeScript /Dart/
• D3.JS/NVD3, HighCharts
• AngularJS 2 (with NodeJS)
• KnockOutJS (with NodeJS)
• BackBoneJS (with NodeJS)
• Ember JS / Ext JS (with NodeJS)
• Raspberry Pi
• Responsive Web Design with Bootstrap, Google
Material Design and KendoUI, Zurb Foundation
• C# ASP.NET MVC , WEB API, WPF
• JAVA , SPRING, HIBERNATE
• Python , Django
• R Statistical Programming
• Android Programming
• Python/Django
• Ruby on Rails
• NoSQL (MongoDB – Essentials & Advanced )
• GIS Tools, SAFE FME, ArcGIS
INDIA
HYDERABAD | BANGALORE | CHENNAI | PUNE
OVERSEAS
SINGAPORE | MALAYSIA | DUBAI | EUROPE |
NORTH AMERICA
46© Syed Awase 2015-16 - TPRI

Mais conteúdo relacionado

Mais procurados

Introduction to ASP.NET 5
Introduction to ASP.NET 5Introduction to ASP.NET 5
Introduction to ASP.NET 5mbaric
 
In The Trenches With Tomster, Upgrading Ember.js & Ember Data
In The Trenches With Tomster, Upgrading Ember.js & Ember DataIn The Trenches With Tomster, Upgrading Ember.js & Ember Data
In The Trenches With Tomster, Upgrading Ember.js & Ember DataStacy London
 
Laravel introduction
Laravel introductionLaravel introduction
Laravel introductionSimon Funk
 
Building Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsBuilding Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsSrdjan Strbanovic
 
Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)
Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)
Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)Arrow Consulting & Design
 
ASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsShahed Chowdhuri
 
Digpen 7: Why choose Laravel?
Digpen 7: Why choose Laravel?Digpen 7: Why choose Laravel?
Digpen 7: Why choose Laravel?John Blackmore
 
PHP konferencija - Microsoft
PHP konferencija - MicrosoftPHP konferencija - Microsoft
PHP konferencija - Microsoftnusmas
 
Load Balancing, Failover and Scalability with ColdFusion
Load Balancing, Failover and Scalability with ColdFusionLoad Balancing, Failover and Scalability with ColdFusion
Load Balancing, Failover and Scalability with ColdFusionColdFusionConference
 
PHP on IBM i Tutorial
PHP on IBM i TutorialPHP on IBM i Tutorial
PHP on IBM i TutorialZendCon
 
Super Fast Application development with Mura CMS
Super Fast Application development with Mura CMSSuper Fast Application development with Mura CMS
Super Fast Application development with Mura CMSColdFusionConference
 
OWIN (Open Web Interface for .NET)
OWIN (Open Web Interface for .NET)OWIN (Open Web Interface for .NET)
OWIN (Open Web Interface for .NET)Folio3 Software
 
Zend Core on IBM i - Security Considerations
Zend Core on IBM i - Security ConsiderationsZend Core on IBM i - Security Considerations
Zend Core on IBM i - Security ConsiderationsZendCon
 
ASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsShahed Chowdhuri
 
DB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iDB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iAlan Seiden
 
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework CoreAsp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework Coremohamed elshafey
 

Mais procurados (20)

Introduction to ASP.NET 5
Introduction to ASP.NET 5Introduction to ASP.NET 5
Introduction to ASP.NET 5
 
Restful API's with ColdFusion
Restful API's with ColdFusionRestful API's with ColdFusion
Restful API's with ColdFusion
 
In The Trenches With Tomster, Upgrading Ember.js & Ember Data
In The Trenches With Tomster, Upgrading Ember.js & Ember DataIn The Trenches With Tomster, Upgrading Ember.js & Ember Data
In The Trenches With Tomster, Upgrading Ember.js & Ember Data
 
Laravel introduction
Laravel introductionLaravel introduction
Laravel introduction
 
Fluxible
FluxibleFluxible
Fluxible
 
Building Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsBuilding Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJs
 
Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)
Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)
Getting Started with ASP.NET Core 1.0 (formerly ASP.NET 5)
 
ASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web Apps
 
Digpen 7: Why choose Laravel?
Digpen 7: Why choose Laravel?Digpen 7: Why choose Laravel?
Digpen 7: Why choose Laravel?
 
PHP konferencija - Microsoft
PHP konferencija - MicrosoftPHP konferencija - Microsoft
PHP konferencija - Microsoft
 
Load Balancing, Failover and Scalability with ColdFusion
Load Balancing, Failover and Scalability with ColdFusionLoad Balancing, Failover and Scalability with ColdFusion
Load Balancing, Failover and Scalability with ColdFusion
 
PHP on IBM i Tutorial
PHP on IBM i TutorialPHP on IBM i Tutorial
PHP on IBM i Tutorial
 
Super Fast Application development with Mura CMS
Super Fast Application development with Mura CMSSuper Fast Application development with Mura CMS
Super Fast Application development with Mura CMS
 
OWIN (Open Web Interface for .NET)
OWIN (Open Web Interface for .NET)OWIN (Open Web Interface for .NET)
OWIN (Open Web Interface for .NET)
 
Introduction to OWIN
Introduction to OWINIntroduction to OWIN
Introduction to OWIN
 
Getting started with PHP on IBM i
Getting started with PHP on IBM iGetting started with PHP on IBM i
Getting started with PHP on IBM i
 
Zend Core on IBM i - Security Considerations
Zend Core on IBM i - Security ConsiderationsZend Core on IBM i - Security Considerations
Zend Core on IBM i - Security Considerations
 
ASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web Apps
 
DB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iDB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM i
 
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework CoreAsp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework Core
 

Destaque

Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVMAsynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVMHugh Anderson
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises우영 주
 
Async Module Definition via RequireJS
Async Module Definition via RequireJSAsync Module Definition via RequireJS
Async Module Definition via RequireJSDevOWL Meetup
 
Node.js의 도입과 활용
Node.js의 도입과 활용Node.js의 도입과 활용
Node.js의 도입과 활용Jin wook
 
Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)xMartin12
 

Destaque (7)

Module System of JavaScript
Module System of JavaScriptModule System of JavaScript
Module System of JavaScript
 
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVMAsynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Async Module Definition via RequireJS
Async Module Definition via RequireJSAsync Module Definition via RequireJS
Async Module Definition via RequireJS
 
Node.js의 도입과 활용
Node.js의 도입과 활용Node.js의 도입과 활용
Node.js의 도입과 활용
 
Node.js 기본
Node.js 기본Node.js 기본
Node.js 기본
 
Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)
 

Semelhante a Require js training

AngularJS - Architecture decisions in a large project 
AngularJS - Architecture decisionsin a large project AngularJS - Architecture decisionsin a large project 
AngularJS - Architecture decisions in a large project Elad Hirsch
 
OpenProdoc Overview
OpenProdoc OverviewOpenProdoc Overview
OpenProdoc Overviewjhierrot
 
Introduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureIntroduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureColin Mackay
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Mack Hardy
 
Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter
Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter
Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter MuleSoft
 
Asp.NETZERO - A Workshop Presentation by Citytech Software
Asp.NETZERO - A Workshop Presentation by Citytech SoftwareAsp.NETZERO - A Workshop Presentation by Citytech Software
Asp.NETZERO - A Workshop Presentation by Citytech SoftwareRitwik Das
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS Ganesh Kondal
 
Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Henry S
 
Google appenginejava.ppt
Google appenginejava.pptGoogle appenginejava.ppt
Google appenginejava.pptYoung Alista
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesVagif Abilov
 
MySQL & Oracle Linux Keynote at Open Source India 2014
MySQL & Oracle Linux Keynote at Open Source India 2014MySQL & Oracle Linux Keynote at Open Source India 2014
MySQL & Oracle Linux Keynote at Open Source India 2014Sanjay Manwani
 
MyFaces CODI and JBoss Seam3 become Apache DeltaSpike
MyFaces CODI and JBoss Seam3 become Apache DeltaSpikeMyFaces CODI and JBoss Seam3 become Apache DeltaSpike
MyFaces CODI and JBoss Seam3 become Apache DeltaSpikeos890
 
Migrate Oracle WebLogic Applications onto a Containerized Cloud Data Center
Migrate Oracle WebLogic Applications onto a Containerized Cloud Data CenterMigrate Oracle WebLogic Applications onto a Containerized Cloud Data Center
Migrate Oracle WebLogic Applications onto a Containerized Cloud Data CenterJingnan Zhou
 
Rapid With Spring Roo
Rapid With Spring RooRapid With Spring Roo
Rapid With Spring RooMorten Lileng
 
Docker for the enterprise
Docker for the enterpriseDocker for the enterprise
Docker for the enterpriseBert Poller
 
Rocking the enterprise with Ruby - RubyKaigi 2010
Rocking the enterprise with Ruby - RubyKaigi 2010Rocking the enterprise with Ruby - RubyKaigi 2010
Rocking the enterprise with Ruby - RubyKaigi 2010releasebeta
 

Semelhante a Require js training (20)

AngularJS - Architecture decisions in a large project 
AngularJS - Architecture decisionsin a large project AngularJS - Architecture decisionsin a large project 
AngularJS - Architecture decisions in a large project 
 
OpenProdoc Overview
OpenProdoc OverviewOpenProdoc Overview
OpenProdoc Overview
 
toolkit
toolkittoolkit
toolkit
 
Introduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureIntroduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azure
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter
Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter
Tips and Tricks for the Advanced Mule Developer with Tesla and Twitter
 
Asp.NETZERO - A Workshop Presentation by Citytech Software
Asp.NETZERO - A Workshop Presentation by Citytech SoftwareAsp.NETZERO - A Workshop Presentation by Citytech Software
Asp.NETZERO - A Workshop Presentation by Citytech Software
 
Hybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbaiHybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbai
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS
 
Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1
 
Google appenginejava.ppt
Google appenginejava.pptGoogle appenginejava.ppt
Google appenginejava.ppt
 
Mini-Training: NancyFX
Mini-Training: NancyFXMini-Training: NancyFX
Mini-Training: NancyFX
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class Libraries
 
MySQL & Oracle Linux Keynote at Open Source India 2014
MySQL & Oracle Linux Keynote at Open Source India 2014MySQL & Oracle Linux Keynote at Open Source India 2014
MySQL & Oracle Linux Keynote at Open Source India 2014
 
MyFaces CODI and JBoss Seam3 become Apache DeltaSpike
MyFaces CODI and JBoss Seam3 become Apache DeltaSpikeMyFaces CODI and JBoss Seam3 become Apache DeltaSpike
MyFaces CODI and JBoss Seam3 become Apache DeltaSpike
 
Migrate Oracle WebLogic Applications onto a Containerized Cloud Data Center
Migrate Oracle WebLogic Applications onto a Containerized Cloud Data CenterMigrate Oracle WebLogic Applications onto a Containerized Cloud Data Center
Migrate Oracle WebLogic Applications onto a Containerized Cloud Data Center
 
Rapid With Spring Roo
Rapid With Spring RooRapid With Spring Roo
Rapid With Spring Roo
 
Docker for the enterprise
Docker for the enterpriseDocker for the enterprise
Docker for the enterprise
 
Rocking the enterprise with Ruby - RubyKaigi 2010
Rocking the enterprise with Ruby - RubyKaigi 2010Rocking the enterprise with Ruby - RubyKaigi 2010
Rocking the enterprise with Ruby - RubyKaigi 2010
 

Mais de Dr. Awase Khirni Syed

Mais de Dr. Awase Khirni Syed (8)

Powershell training material
Powershell training materialPowershell training material
Powershell training material
 
Es2015 training material-syedawase
Es2015 training material-syedawaseEs2015 training material-syedawase
Es2015 training material-syedawase
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
Knockout js
Knockout jsKnockout js
Knockout js
 
Mongo db model relationships with documents
Mongo db model relationships with documentsMongo db model relationships with documents
Mongo db model relationships with documents
 
Mongo db groundup-0-nosql-intro-syedawasekhirni
Mongo db groundup-0-nosql-intro-syedawasekhirniMongo db groundup-0-nosql-intro-syedawasekhirni
Mongo db groundup-0-nosql-intro-syedawasekhirni
 
C# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTC# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENT
 
SycliQ-AgribusinessInfographik001
SycliQ-AgribusinessInfographik001SycliQ-AgribusinessInfographik001
SycliQ-AgribusinessInfographik001
 

Último

Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 

Último (20)

Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 

Require js training

  • 1. Require.js Ground Up! Asynchronous Module Dependency Injector Dr. SYED AWASE KHIRNI 1 Copyright © Territorial Prescience Research I P Ltd, 2007-2015 Works with IE6+, Firefox2+, Safari 3.2+, Chrome 3+, Opera 10+
  • 2. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 2 Encapsulation A language mechanism for restricting access to some of the Object’s components Protect variable from unwanted changes
  • 3. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 3 JavaScript is prototype-based
  • 4. Convention in JavaScript Copyright © Territorial Prescience Research I P Ltd, 2007-2015 4 Customer _order _gender _cartSummary Public access for everyone
  • 5. Privileged Method Copyright © Territorial Prescience Research I P Ltd, 2007-2015 5 Customer Public access for everyone order gender cartSummary
  • 6. RequireJS • An Asynchronous JavaScript module and file loader, optimized for web browser usage. • Adhering to SOLID Principles – Single responsibility, the code is separated into modules, dependencies need to be configured when loading files. • RequireJS creates code on demand and adds it to the page header as an script tag (using head.appendChild()) taking care of all its dependencies (loading them as well in the proper order). 6Copyright © Territorial Prescience Research I P Ltd, 2007-2015
  • 7. Problem Statement • Increase in code complexity for enterprise class applications. • Web applications serving multi-modal devices • Assembly gets harder • Compliance with SOLID Principles • Developers wants discrete JS files/modules • Increase web application performance with fewer HTTP calls for resources using bundling and minification • Encapsulation of data Copyright © Territorial Prescience Research I P Ltd, 2007-2015 7
  • 8. Traditional Approach • Slow – Many HTTP requests to the server for fetching resources – Blocking render • Manual Dependencies • Lack Encapsulation Copyright © Territorial Prescience Research I P Ltd, 2007-2015 8
  • 9. Solution • Some sort of #include/import/require for dependency injection • Ability to load nested dependencies asynchronously • Ease of use for developer backed by an optimization tool that helps deployment. • Transform Websites into Web Apps – More – More Complexity – Ease of Development • Adhering to Design Principles – Separate into reusable components – Avoid global namespace pollution Copyright © Territorial Prescience Research I P Ltd, 2007-2015 9
  • 10. Module Pattern Locally Scoped Object Literal Copyright © Territorial Prescience Research I P Ltd, 2007-2015 10 Where do modules live? • Global? Or Local? Are they reusable? Module is a resource Modules may require some dependencies
  • 11. Module Pattern Copyright © Territorial Prescience Research I P Ltd, 2007-2015 11 Customer gender order Exports cartSummary
  • 12. Stacked locally scoped object literal Revealing Module Pattern Copyright © Territorial Prescience Research I P Ltd, 2007-2015 12 • public pointers to methods inside the Module’s scope are revealed.
  • 13. Module Pattern Advantage • Easy to implement • Keep private data safe Disadvantage • Hard to inherit • Lots of modules • Complex dependency injection • One file per module • Lots of files – Performance issue – Asynchronous loading required Copyright © Territorial Prescience Research I P Ltd, 2007-2015 13
  • 14. Augmentation • One of the limitations of the module pattern so far is that the entire module must be in one file. • In large code-base, modules are extensible components. • Augment modules, provides the functionality to import the module, and add or extend the functionality and then export it. • Tight Augmentation implies a set loading order, but allows overrides. • Complex dependency – load modules by order Copyright © Territorial Prescience Research I P Ltd, 2007-2015 14 Extending an existing module TightAugmentation
  • 15. Loose Augmentation • In the above Augmentation scenario, initial module creation to be first and then the augmnetation has to happen second in a specified order. • Alternatively, it doesn’t always happen in the same order, to improve performance, javascript allows to load scripts asynchronously. • We can create flexibile multi- part modules that can load themselves in any order with loose augmentation. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 15 Extending an existing module
  • 16. 3 RequireJS API • Define() : define and register dependencies – Define () must return a Object which is cached in a registry • Require() :callback function invoked when all defines() have completed – Acts as intialization or root of the dependency tree – Starts the cascade of dependency checks and script loading – Starts the cascade of define() triggers • Config() :configure source paths, shims and aliases – Requirejs.config() – configuration or aliases and shims Copyright © Territorial Prescience Research I P Ltd, 2007-2015 16
  • 17. Sub-modules • It is to create a sub-set within an existing module. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 17
  • 18. CommonJS • A module ecosystem for Javascript: – Require() method – Exports object • Created for javascript on the server • Doesn’t work well in the browser natively Copyright © Territorial Prescience Research I P Ltd, 2007-2015 18
  • 19. Asynchronous Module Definition (AMD) • A javascript specification that defines an API for defining code modules and their dependencies and loading them asynchronously if desired. • Created to work directly in browsers • It specifies a mechanism for defining modules where the module and its dependencies can be asynchronously loaded. • RequireJS is an implementation of this specification. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 19
  • 20. AMD • Asynchronous – Don’t block browser – Parallel download and interpretation – Callbacks everywhere • Loaded via script tag injection (not XHR +eval) – Debugging – X-Domain • Each module says which modules it needs. • The module’s “factory” is called after all of those modules are loaded. • Efficient Loading – Flexible development life cycle – Useful for a few or external resources • Production/Deployment – Easy packaging – Single file or multiple packages – Command line tools to resolve dependencies at build time. – Inline text resources like template files Copyright © Territorial Prescience Research I P Ltd, 2007-2015 20
  • 21. AMD • Predecessors – DOJO – LABJS – COMMONJS • Current – Jquery 1.7 – Dojo 1.7 – MooTools 2.0 – EmbedJS – Backbone/underscore will • AMD Drawback – No Standards – Lots of up-front work – No semantic versioning – Heavyweight tools (RequireJS) • Alternative to AMD – Browserify • Simple syntax: require • Great if you are into Node+ npm • Intended for bundling, not so much for async module loading. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 21
  • 22. Using AMD Copyright © Territorial Prescience Research I P Ltd, 2007-2015 22
  • 23. RequireJS • AMD Implementation by James Burke • Asynchronous resource loader • Call anytime • Trace nested dependencies Copyright © Territorial Prescience Research I P Ltd, 2007-2015 23 • Avoid polluting the global namespace • Optimization tool for JS/CSS
  • 24. RequireJS (Loading Scripts) • Creates script tags on demand and adds them to the head. • Non-blocking page rendering approach for better web experience • Works after page load • Not constrained by same origin policy • Need to know the dependencies upfront Copyright © Territorial Prescience Research I P Ltd, 2007-2015 24
  • 25. RequireJS Module Pattern • Allows the dependencies to be loaded as fast as possible, even out of order, but evaluated in the correct dependency order, and since global variables are not created. It makes it possible to load multiple versions of a module in a page. • Dependency management – Dependencies can be nested – Load once, use in multiple locations – Plugins for text dependencies, load order, etc – Compatible with JavaScript protypal inheritance • Extension of widely used JS module pattern • Minimal boilerplate code for developers to adopt • Defines a well-scoped object that avoids polluting the global namespace. • Explicitly lists its dependencies • Receives dependencies as arguments to the function that defines the module • Does not need globals to refer to other modules Copyright © Territorial Prescience Research I P Ltd, 2007-2015 25
  • 26. RequireJS Optimizer • It includes an optimization tool to help deployment – Combine modules into fewer files for deployment – Concatenate and minify JS files – Shallow file exclusion for tweaking or debugging individual JS files • Concatenate CSS@import files • Minify CSS files • File systems vs URLs Copyright © Territorial Prescience Research I P Ltd, 2007-2015 26
  • 27. Using require() vs define() Require() • Used to run immediate functionalities • Require calls use the requireJS baseURL(prepending it) in all cases but – When query string parameters are present – When absolute path are present – A protocol like http or https is contained in the url – When the path ends with .js (js extensions) Define() • Used to define modules for use in multiple locations. • Reusable modules are usually defined • Used for module definitions. A module can be basically any kind of object and is not available in the global namespace. • Defining modules using requireJS is that no global objects are used. • Modules are loaded (potentially) in different order as they are requested, but they are evaluated in the same order, so it is possible to load multiple version of the same module in the same page. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 27
  • 28. Managing the Order of Dependent Files • RequireJS uses AMD for loading files. Each dependent module will start loading through asynchronous requests in the given order. • Even though the file order is considered, it is not guaranteed that the first file is loaded before the second file due to the asynchronous nature. • requireJS uses shim config to define the sequence of files which need to be loaded in correct order. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 28
  • 29. Specifying Configuration • RequireJS comes with a lot of configuration options to easily create different configurations for different situations – Development – Production/deployment – testing • Yeoman generators generally set this up for us. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 29
  • 30. Configuration Options for requireJS Option Description baseUrl Base path appended to all modules loaded via requireJS, default path of the HTML page that loads requireJS, in case the “data-main” attribute is used, the default is the path of this attribute. Paths Path mapping for modules, relative to the baseUrl Shim Mechanism to support the configuration and usage of non-AMD libraries with requireJS. It defines their dependencies, exports them using their global variables or calls their no conflict functions explicitly. Map Different paths for some modules, useful in case the same application needs to use different versions of the same module for different purposes and needs to share their ids in order to use the same code for different situations. Config A way of passing configuration to the modules. This is done by using the special module ‘ module’ and its function module.config() urlArgs Query parameters that are added to all resources loaded via require JS. Very useful for cache busting during development, to be used with care on production waitSeconds Number of seconds before throwing a timeout, default 7 seconds, 0 means no time out will be thrown. Copyright © Territorial Prescience Research I P Ltd, 2007-2015 30
  • 31. Configuration file Copyright © Territorial Prescience Research I P Ltd, 2007-2015 31
  • 34. View © 2012-2016 TPRI Syed Awase 34
  • 35. Example2 © 2012-2016 TPRI Syed Awase 35 Counter.js
  • 36. Example 2 © 2012-2016 TPRI Syed Awase 36 Test1.js Test2.js
  • 37. Example 2: Using RequireJs © 2012-2016 TPRI Syed Awase 37 Main.js
  • 38. Example2: View © 2012-2016 TPRI Syed Awase 38
  • 39. Example 3:viewModel © 2012-2016 TPRI Syed Awase 39
  • 45. r.js- the RequireJS Optimizer • Build and Deploy RequireJS Apps – Deploy uncompressed – Deploy concatenate and uglified • Plugins available for – Grunt (grunt-requirejs) – Gulp(gulp-requirejs) – Broccoli (broccoli-requirejs) © 2012-2016 TPRI Syed Awase 45
  • 46. sak@sycliq.com sak@territorialprescience.com Contact Us Thank You We also provide Code Driven Open House Trainings For code driven trainings for Technology Firms Reach out to us +91-9035433124 We are hardcore Technologists/Architects/Programmers trainings are offered by Dr. Syed Awase Current Offerings • AngularJS 1.5.x • Typescript /CoffeeScript /Dart/ • D3.JS/NVD3, HighCharts • AngularJS 2 (with NodeJS) • KnockOutJS (with NodeJS) • BackBoneJS (with NodeJS) • Ember JS / Ext JS (with NodeJS) • Raspberry Pi • Responsive Web Design with Bootstrap, Google Material Design and KendoUI, Zurb Foundation • C# ASP.NET MVC , WEB API, WPF • JAVA , SPRING, HIBERNATE • Python , Django • R Statistical Programming • Android Programming • Python/Django • Ruby on Rails • NoSQL (MongoDB – Essentials & Advanced ) • GIS Tools, SAFE FME, ArcGIS INDIA HYDERABAD | BANGALORE | CHENNAI | PUNE OVERSEAS SINGAPORE | MALAYSIA | DUBAI | EUROPE | NORTH AMERICA 46© Syed Awase 2015-16 - TPRI