Getting Started with Controllers, Models and Decorators (SFCC)


salesforce commerce cloud
      
          'use strict';
const server = require('server');

server.get('Show', function (req, res, next) {
  res.render('helloWorld');
  next();
});
        
      
          server.get('ErrorNotFound', function (req, res, next) {
  res.setStatusCode(404);
  res.render('error/notFound');
  next();
});
        
      
          'use strict';
const server = require('server');
const cache = require('*/cartridge/scripts/middleware/cache');

server.extend(module.superModule);
server.replace(
  'Show',
  consentTracking.consent,
  cache.applyDefaultCache,
  function (req, res, next) {
    res.render('/home/practiceHome');
    next();
  });
        
      
          server.post('Submit', function (req, res, next) {
  this.on('route:BeforeComplete', function (req, res) {
    let form = server.forms.getForm('practice');
    if (!form.valid) {
      res.setStatusCode(500);
    }
    res.json({ form: server.forms.getForm('practice') });
  });
  next();
});
        
      
          'use strict';
const server = require('server');

server.extend(module.superModule);
server.prepend('Show', function (req, res, next) {
  let viewData = res.getViewData();
  viewData.param = 'Here we are using prepend';
  res.setViewData(viewData);
  next();
});

module.exports = server.exports();
        
      
          function account(currentCustomer, addressModel, orderModel) {
  module.superModule.call(this, currentCustomer, addressModel, orderModel);

  if (currentCustomer && currentCustomer.custom) {
    this.account.practiceField = currentCustomer.custom.practiceField || null;
  }
}

module.exports = account;
        
      
          module.exports = function fullProduct(product, apiProduct, options) {
  decorators.base(product, apiProduct, options.productType);
  decorators.price(product, apiProduct, options.promotions, false, options.optionModel);

  if (options.variationModel) {
    decorators.images(product, options.variationModel, { types: ['large', 'small'], quantity: 'all' });
  } else {
    decorators.images(product, apiProduct, { types: ['large', 'small'], quantity: 'all' });
  }

  decorators.quantity(product, apiProduct, options.quantity);
  decorators.variationAttributes(product, options.variationModel, {
    attributes: '*',
    endPoint: 'Variation'
  });

  decorators.description(product, apiProduct);
  decorators.ratings(product);
        
      
          'use strict';

module.exports = function (object, product, quantity) {
  Object.defineProperty(object, 'isOrderable', {
    enumerable: true,
    value: product.availabilityModel.isOrderable(quantity)
  });
};
        
      
          'use strict';
const assign = require('server/assign');

function updateAccountFields(newAccount, account) {
  module.superModule.updateAddressFields(newAccount, account);
  newAccount.custom.practiceField = account.practiceField;
}

module.exports = assign(module.superModule, {
  updateAccountFields: updateAccountFields
});
        
blog author

Stefan Stanković

Software Developer