Cert++
Close

Choosing models, decorators, factories or helpers for controller logic

Key Takeaway

When a controller needs B2C Commerce script API data shaped for a template, pass the object to a model (ViewModel) that builds a plain JSON object. Then render the template with that data, and keep business logic out of the template.

In Storefront Reference Architecture (SFRA), a controller handles a storefront request but doesn't hold all the logic itself. The controller gets data from the B2C Commerce script API and passes it to a model (ViewModel), which turns the data into a plain JSON object. For shared tasks, the controller calls reusable scripts such as helpers. Then it renders an ISML template or returns JSON. Knowing which piece owns which job keeps code reusable and makes SFRA upgrades easier.

How SFRA divides the work

SFRA uses a variant of Model-View-Controller. Controllers handle input from the user, create ViewModels and render pages. ViewModels request data from B2C Commerce, convert script API objects into plain JSON objects and apply business logic.

In the cartridge, the controllers folder holds the application's business logic. The models folder gets data from the server and provides it as JSON objects. Controllers create and update models. The models provide the JSON object layer for the application.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given a requirement, create and extend the functionality of a JavaScript controller that leverages models, decorators, factories, or helpers following API best practices, and renders a template or returns a JSON response

0/11

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers.