The document provides an overview of common tasks that can be performed using JavaScript controllers in Demandware like importing objects, declaring functions, handling forms, writing to the browser, transactions, and redirects. It also discusses dealing with query parameters, giving control to ISML templates, and invoking other scripts.
The document provides an overview of common tasks that can be performed using JavaScript controllers in Demandware like importing objects, declaring functions, handling forms, writing to the browser, transactions, and redirects. It also discusses dealing with query parameters, giving control to ISML templates, and invoking other scripts.
Import an Object Displaying a Form (Equivalent of ICN node)
var ISML = require('dw/template/ISML'); ISML.renderTemplate( 'editpreferences.isml', var guard = require('storefront_controllers/cartridge/scripts/guard'); { ContinueURL : dw.web.URLUtils.https('JEditPreferences- Function Declaration and Exposure HandleForm') function start() { } } ); exports.Start = guard.ensure([ 'get'], start); exports.Start = guard.ensure([ 'get', 'https', 'loggedIn' ], start); Submit Button Dealing with Query Parameters var submitButton = request.triggeredFormAction.formId; var parameterId = request.httpParameterMap.pid.stringValue
Writing to Browser Directly
Handling Forms response.getWriter().println(“Hello from controllers”); /* assuming forms/default/preferences.xml is the metadata */ var preferencesForm = session.forms.preferences; Explicit Transactions preferencesForm.clearFormElement(); var Transaction = require('dw/system/Transaction); preferencesForm.copyFrom(customer.profile); Transaction.begin(); preferencesForm.copyTo(customer.profile); Transaction.commit(); preferencesForm.interestElectronics.value; Transaction.rollback(); preferencesForm.invalidateFormElement();
Redirecting to Another Pipeline
Giving Control to ISML (Equivalent of Interaction node) var URLUtils = require('dw/web/URLUtils'); ISML.renderTemplate( response.redirect(URLUtils.https('Account-Show')); 'productnotfound.isml', { Invoking a Script (js or ds) message:'product'+parameterId+'found' var myModel = require('~/cartridge/scripts/MyModel'); } var co = myModel.doJobForMe(takeThisObject); );