Models for SuiteCommerce Extensions: SCModel Usage

Leverage SCModel to define data handling in SuiteCommerce extensions. Learn how to create models that validate and fetch data efficiently.

·3 min read·View Oracle Docs

TL;DR

A model in SuiteCommerce extensions manages data handling, such as validation and retrieval. Utilizing the SCModel class, developers can create custom models that integrate seamlessly into the application interface.

What is an SCModel?

In SuiteCommerce, a model allows developers to define how data is validated, saved, or fetched from the server within an extension. When a view requires data, it will typically accept a model to display the information appropriately on the web interface.

Creating a Model Using SCModel

To develop a model, utilize the SCModel class found within the SCModel component. This is part of the extensibility API available since SuiteCommerce 2020.1. If you're working with older versions of SuiteCommerce Advanced, you should employ Backbone's extend() instead.

Note: The SCModel class is unique and does not behave as a traditional component class like others in the extensibility API. For instance, you won't create an instance of SCModel with the getComponent() method. Instead, you declare it as a dependency when defining your model.

Example of a Basic Model

Below is an illustrative example of a basic model using the SCModel class.

javascript
1"text-purple-400">define(
2 'Vendor.MyExtension.Module.Model',
3 [
4 'SCModel',
5 'Utils'
6 ],
7 "text-purple-400">function(SCModelModule, Utils) {
8 'use strict';
9
10 "text-purple-400">var SCModel = SCModelModule.SCModel;
11
12 "text-purple-400">function VendorMyExtensionModuleModel () {
13 SCModel.call("text-purple-400">this);
14
15 "text-purple-400">this.urlRoot = "text-purple-400">function() {
16 "text-purple-400">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 "text-purple-400">return VendorMyExtensionModuleModel;
24 }
25);

Breakdown of the Model Code

  1. Module Definition: The define() statement is first used to specify a unique name for your module and a list of its dependencies. Since we are building on SCModel, this is required to be included.
  2. Constructor Function: The constructor function initializes the model logic. When initializing, it's mandatory to invoke the parent SCModel constructor to ensure the model inherits proper properties.
  3. Setting urlRoot: The urlRoot property establishes the endpoint for data retrieval. It must be a function returning a string.
  4. Prototype Inheritance: Utilize Object.create() to set up prototypal inheritance from SCModel, ensuring your custom model inherits the functionalities of SCModel while resetting the constructor property appropriately.

Conclusion

The SCModel class provides a structured way to define and manage data within SuiteCommerce extensions. Thorough understanding and proper implementation can significantly enhance functionality, ensuring data is well-handled and efficiently retrieved.

Who This Affects

  • Developers: Those building SuiteCommerce extensions will find this information crucial for effective data management.
  • Administrators: Understanding how models work helps in managing extensions and their functionalities effectively.

Key Takeaways

  • Models are essential for handling data in SuiteCommerce extensions.
  • Use the SCModel class for data management in versions from 2020.1 onward.
  • Properly configure the urlRoot to ensure correct data retrieval.

Source: This article is based on Oracle's official NetSuite documentation.

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 →