2 - Common JS & Controllers
2 - Common JS & Controllers
Agenda:
• Common JS Specifications.
Module Objective: • Common JS Modules.
• SFRA Modules:
• What is a Module?
Given a requirement, create and extend
• Cartridge Path
the functionality of a JavaScript controller
• Global Modules
that leverages models, decorators,
• Directory Structure
factories, or helpers following API best
• Exporting & require Modules
practices and renders a template or
returns a JSON response. • Use of ~ * .
• Cartridge Path
• Middleware Functions
• SFRA Routing
• SFRA Controllers
What is CommonJS
Specification for server-side JavaScript
” The CommonJS module specification is the standard used in server-side JS for working with modules.
Modules are powerful, because they let you encapsulate all sorts of functionality, and expose this functionality to other
JavaScript files, as libraries”
‘’Modules are very cool, because they let you encapsulate all sorts of functionality, and expose this functionality to other
JavaScript files, as libraries.”
• Server module lives in modules folder & server name is reserved for this
module only.
• Changes to server module voids support.
SFRA Modules
Cartridge Path(CP)
• Requirement:
• Cartridges Cartridge _DE & app_storefront_base to be used for German site.
• Cartridges Cartridge _GB & app_storefront_base to be used for UK site.
• Cartridges Cartridge _US & app_storefront_base to be used for US site.
** Only cartridges mentioned in the site’s cartridge path are effective for the given Site.
SFRA Modules
Cartridge Path – Module Lookup
Module Lookup:
• Executing require() triggers a Module lookup operation.
• Digital server searches for the required JS module. This search is influenced by the cartridge path:
● Even though the current cartridge comes second in the precedence order but you may want to load the
file from current cartridge, use:
Use of module.superModule
● Use module.supermodule to refer the parent module.
● Parent & child modules should be on the same relative path.
● Child module will be in the cartridge on the Left hand side in
cartridge path, while the parent Module will be in right one.
● In the child module, provide the definition of the additional
functions or override the ones from parent.
● Use exports in child Module to export it’s own & parent’s public
functions, objects & variables.
SFRA Modules
Global Modules
Package.json in top level directory > main file > independent file > file in modules directory
SFRA Modules
Cartridge Path – DIY Exercise
“Middleware literally means anything you put in the middle of one layer of the
software and another.”
• Controllers use Models, Forms, ISML Templates & APIs to serve customer requests.
• Controllers must conform to the CommonJS module standard.
• A controller's file extension can be either .ds or .js. Controllers must be located in
the controllers folder at the top level of the cartridge.
• Exported methods for controllers must be explicitly made public to be available to
handle storefront requests.
Routes
What is a Route?
“Routing refers to determining how an application responds to a client request to a particular endpoint, which
is a URI (or path).”
• Each route can have one or more handler functions, which are executed when the route is matched.
Use of server.extend(module.superModule)
● module.superModule provides access to parent
module.
● SFRA controllers enables extensibility & reusability.
● There are 3 main methods used to extend or overwrite
a route:
○ append: extends functionality by executing AFTER
the superModule route
○ prepend: extends functionality by executing
BEFORE the superModule route
○ replace: overrides the superModule route
Lets do a Code Walkthrough !!
• Review the Home-Show controller to understand the
Middleware functions used.
• Review the server.js module & other helper modules.
DIY: Hands-on challenge
• Business Requirement:
• You also need enable the shoopers to set their weather preferences in their account.
• High-level approach:
• Extend the profile object to accommodate the weather preference, create a custom attribute.
• Extend the Account controller to persist & retrieve this preference.
MORE DETAILS