SCModel Implementation for SuiteCommerce Extensions

SCModel allows SuiteCommerce developers to define data management, ensuring effective validation and retrieval processes within applications.

·2 min read·View Oracle Docs

Creating models within SuiteCommerce extensions is fundamental for how data is handled within your application, particularly in aspects like validation, persistence, and server interaction. Models are a crucial part of the architecture, enabling views to access and render data efficiently.

To define a model in your extension, you can use the SCModel class, introduced in SuiteCommerce 2020.1. For previous versions of SuiteCommerce Advanced, you would utilize Backbone's extend() method for model creation.

What is the SCModel Class?

The SCModel class offers an extensibility API for managing data models. Unlike other component classes in the extensibility API, SCModel does not require instance creation via the getComponent() method; rather, it is treated as a dependency when defining your model.

Example of a Basic SCModel Definition

The following example illustrates how to create a simple model using the SCModel class:

javascript
1define(
2 'Vendor.MyExtension.Module.Model',
3 [
4 'SCModel',
5 'Utils'
6 ],
7 function(SCModelModule, Utils) {
8 'use strict';
9
10 var SCModel = SCModelModule.SCModel;
11
12 function VendorMyExtensionModuleModel () {
13 SCModel.call(this);
14
15 this.urlRoot = function() {
16 return Utils.getAbsoluteUrl(getExtensionAssetsPath('services/Module.Service.ss'));
17 };
18 }
19
20 VendorMyExtensionModuleModel.prototype = Object.create(SCModel.prototype);
21 VendorMyExtensionModuleModel.prototype.constructor = VendorMyExtensionModuleModel;
22
23 return VendorMyExtensionModuleModel;
24 }
25);

In this code:

  • The module is defined with a unique name and dependencies, including SCModel and Utils.
  • A constructor is created to initialize a new model. This constructor calls the parent class constructor via SCModel.call(this).
  • Here, the urlRoot property must be defined as a function returning a URL string. This is crucial for ensuring the model can fetch and save data appropriately.

Inheriting from SCModel

When you create your custom model,

javascript
VendorMyExtensionModuleModel.prototype = Object.create(SCModel.prototype);

uses Object.create() to set the prototype chain, ensuring that your custom model inherits the properties and methods of SCModel. This is vital for making full use of the SCModel capabilities.

You also need to reset the constructor property of your model,

javascript
VendorMyExtensionModuleModel.prototype.constructor = VendorMyExtensionModuleModel;

This correct assignment ensures that your model's constructor is recognized correctly in the prototype chain.

By following this structured approach, you can effectively manage data models in your SuiteCommerce extensions, facilitating better performance and maintainability.

Key Points

  • Use SCModel for SuiteCommerce versions 2020.1 and later; use Backbone's extend() for earlier versions.
  • Define the urlRoot as a function to enable data fetching.
  • Utilize prototypal inheritance to ensure proper functionality derived from SCModel.

Related Topics

Employing these principles will help you declare models effectively in the SuiteCommerce ecosystem, yielding better data management and an enhanced user experience.

Frequently Asked Questions (4)

Is the SCModel class applicable to all versions of SuiteCommerce?
The SCModel class is part of the extensibility API available since SuiteCommerce 2020.1. For older versions, like SuiteCommerce Advanced, developers should use Backbone's extend() instead.
How should I define dependencies when creating a model using SCModel?
When creating a model using SCModel, dependencies are defined in the define() statement, specifying SCModel and any required utilities.
Can I use getComponent() to instantiate SCModel as I do with other SuiteCommerce components?
No, unlike traditional component classes, you cannot create an instance of SCModel with the getComponent() method. It must be declared as a dependency when defining your model.
How is the urlRoot property configured in an SCModel?
The urlRoot property in an SCModel is configured as a function returning a string that specifies the endpoint for data retrieval. This is crucial for setting up the model's data access logic.
Source: Models in an Extension Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in Commerce

View all Commerce articles →