Fast Path To B2C Commerce Developer Certification - Module 3 - Models, ISML & Client-Side JS
Fast Path To B2C Commerce Developer Certification - Module 3 - Models, ISML & Client-Side JS
Developer Certification
Module 3
Models, ISML & Working with
Client Side Javascript
Module 3.1: Models, Decorators, Extending Models (30 mins)
• Objective
• By the end of this module you will be able to define what a model is, what a
decorator is and how it is used to extend a model.
• This module will cover:
• What a model is?
• What is a decorator?
• How to extend a decorator
• What is this functionality and why does it matter for implementations?
• SFRA data is managed by models
• Separating business logic from data is a best practice
Module 3.1: What is a model?
• Objective
• By the end of this module you will be able to understand the best practices around
ISML usage in SFRA
• This module will cover:
• What is ISML and templates?
• Best Practices on ISML usage in SFRA
• Demo ISML templates and tags
• What is this functionality and why does it matter for implementations?
• Easier loop management
• ISML template to render a page
• Reusability of templates
Module 3.2: What is ISML and templates?
There is an internal ban on certain ISML tags. You can still use them, but here are the caveats:
• iscache: use instead the cache.js middleware methods in controllers:
server.get('Show', cache.applyPromotionSensitiveCache, …)
• isset: use it only to set a variable used by an isinclude tag, not for complex expressions.
<isset name="store" value="${pdict.store}" scope="page"/>
<isinclude template="storeLocator/storeDetails" />
• ismodule: use the single custom tag defined in modules.isml to render content assets. Any other
ismodule has become a script that you invoke.
Module 3.2: Best Practices on ISML usage in SFRA (cont.)
• iscookie: use the function getCookie() in cookie.js. There is no equivalet setCookie() method. Use:
window.localStorage.setItem('previousSid', previousSessionID);
The following are still in active use, and usually used together:
• isactivedatahead, isactivedatacontext and isobject: use them to collect active data. See
documentation.
• isif, iselseif, iselse: manage conditional logic. Example containing a loop and conditional logic:
Module 3.2: ISML tags used in SFRA (cont.)
• isslot: allows the insertion of a content slot in an template. Content slots are:
• Areas of a template that the merchant customizes via Business Manager
• Requires a rendering template that you create
• To learn how to use content slots, refer to the Learning Path > Grow your Expertise > Fast Path to
certification
• isprint: use it to print strings, formatted date and times. There is extensive documentation on this tag:
<td class="quantity"><isprint value="${tier.quantity}" style="INTEGER" /></td>
• You should localize all strings used in templates using Resource Bundles:
• templates/resources folder to locate the
• Localize strings using files with a locale suffix: account_fr_FR.properties (again, language_REGION)
• Use the dw.util.Resource api to render localized strings:
${Resource.msg('msg.no.saved.addresses','address',null)}
• The msgf method allows you to pass parameters to the localized string:
${Resource.msgf('label.product.ratings', 'common', null, product.rating)}
Module 3.2: Demo
Passing data via setViewData and using the new model in ISML
• Objective
• By the end of this module you will be able to override a client-side JavaScript file, compile
it using npm scripts using package.json and webpack.config.js and debug the file on the
browser
• This module will cover:
• Modifying the a client side script to show variant availability
• Using package.json to compile the client-side JavaScript
• Using webpack.config.js to compile the client-side in non-minified mode to allow for browser
debugging
• What is this functionality and why does it matter for implementations?
• In SFRA, you use node to compile CSS and JavaScript for the client-side
• Client-side behaviour that happens after page load is often changed
Module 3.3: Client-side JavaScript in SFRA
Dynamic behaviour on your storefront needs client-side code
This file can contain a lot of metadata about your project. But mostly it will be used for two
things:
● Managing dependencies of your project
● Scripts, that helps in generating builds, running tests and other stuff in regards to your
project
● Define a base path to located storefront-reference-architecture folder
SFRA contains a fairly comprehensive package.json, but you own project only needs a few
scripts:
● compile:js → compile client-side JavaScript
● compile:scss → generate css files based on scss
● uploadCartridge → uploads cartridges defined in dw.json
● watch → watches the folder for changes, and automatically uploads them
Module 3.3: package.json for the sfra_demo project
Notice the base path: this is a relative path to the SFRA base directory
Module 3.3: Using webpack.config.js to compile JavaScript in
non-minified mode
Its main purpose is to bundle JavaScript files for usage in a browser.
mode: ‘none’ → compile for debugging using Developer Tools on the browser
mode: ‘production’ → compile in minified mode for production
Module 3.3: Overriding a base client-side script on your
cartridge
Override client-side code from the base when you need new front-end functionality
When we extended the availability model, we were able to get the new data passed in as JSON.
However, the ISML page does not refresh when we changed variants, therefore the variant’s ATS is not showing.
We must override the base client-side code such that the the function that handles the availability message update uses
the new ATS message passed in the model.
The analysis required to find the right method to override, and the client-side JavaScript code implementation is beyond
the scope of this webinar. But stay tuned for a complementary webinar!
Your code can refer to the base SFRA code as follows:
var base = require('base/product/base');
First “base” refers to the base path (SFRA base cartridge), second “base” refers to the base.js script.
Then the new functionality can override the base as follows:
base.updateAvailability = updateAvailability;
module.exports = base;
Module 3.3: Building SCSS and client-side JS
In this demo we’ll override the client-side so that the proper ATS message will
show:
● Use a new file to override the updateAvailability method from the base
● Make sure webpack.config.js is set up for non-minified compilation
● On the command line:
○ npm install to build the node_modules
○ npm run compile:js to compile the JS
○ npm run uploadCartridge (only if your dw.json file is setup correctly)
● On the browser:
○ Refresh the SFRA page
○ Open the Developer Tools window
○ Put a breakpoint on the new updateAvailability method
○ Step thru the code to see if the right message is created
○ Show the generated page with the new ATS message
Time to Test your Knowledge!
Module 3.3 - All Questions at the end