new <abstract> SCModel()

Description

The SCModel component lets you define models in SuiteCommerce extensions. It offers similar functionality to Backbone.Model, with methods and properties to work specifically with SuiteCommerce. Similar to other components in the extensibility API, such as SCFormView and SCCollectionView, you must define your model in a file with the AMD module specification.

The SCModel component provides an abstract class called SCModel. To use SCModel, create a child class object that extends from it. To inherit from the parent class, immediately call the parent constructor with call() in your child class object.

Important: In extensions on SuiteCommerce 2020.1 and later, use SCModel instead of Backbone.Model.

MySCModel.Model.js

			define(
				'MySCModel.Model',
				[
					'SCModel', 
					'Utils'
				], 
				function(SCModelComponent, Utils){
					'use strict';
			
					var SCModel = SCModelComponent.SCModel;
			
					function MySCModel() {
						SCModel.call(this);
			
						// Define properties of the model.
						this.urlRoot = function() {
							return Utils.getAbsoluteUrl(
								getExtensionAssetsPath("services/MainModule.Service.ss")
							)		 	 
						}
					}
			
					MySCModel.prototype = Object.create(SCModel.prototype);
			
					MySCModel.prototype.constructor = MySCModel;
			
					// Return the AMD constructor.
					return MySCModel;
				}
			);
			
Details

SuiteCommerce 2020.1

Members


<private> _changing

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> _pending

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> _previousAttributes

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> attributes

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> changed

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> cid

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> cidPrefix

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> collection

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> defaults

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> id

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<protected> idAttribute

Description

Lets you use an arbitrary attribute (instead of the default id attribute) to store a model's identifier. The default value of idAttribute is 'internalid', which is the unique identifier of most records in NetSuite.


<private> operationIds

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.


<private> validation

Description

An internal property. Do not override or use this property. If you override this property, your SuiteCommerce website might not work as expected.

Methods


<private> _validate()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> addOperationId()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> changedAttributes()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> clear()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> clone()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> deepCopy()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


destroy() → {false|Deferred}

Description

Destroys (deletes) the model on the server.

Returns

Returns false if the model is new. Otherwise, it returns a Deferred object.


<private> escape()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


fetch() → {Deferred}

Description

Gets model data from the server.

			var product = new ProductModel();
			
			product.fetch().done(function() {
				// ...
			});
			
Returns

Returns a Deferred object.


get( attributeName ) → {*}

Description

Allows you to get the value of a model attribute.

			var product = new ProductModel();
			var productPrice = product.get("price");
			
Parameters
Name Type Description
attributeName string

The name of the attribute.

Returns

The attribute value. Its type is the same type as the model attribute type.


<private> getLatestOperationIds()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> getOperationIds()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


getValidationErrors() → {null|Object}

Description

Gets the errors, if any, after validation of the model. Errors are returned in an object in which model attributes are the keys and errors are the values of those keys.

You must create the validation rules by overriding the getValidationRules() method.

You can call this method after setting an attribute on the model with set(). Whenever you set attributes on the model, the attributes are validated according to the validation rules in getValidationRules().

			var review = new ReviewModel();
			
			review.set('reviewername').done(function() {
				var errors = review.getValidationErrors();
			
				if (errors !== null) {
					// Do something with the errors
					// ...
				}
			});
			
Returns

Returns null if there are no errors. Otherwise, it returns an object in which model attributes are the keys and errors are the values of those keys.


<protected> getValidationRules() → {Object}

Description

Lets you define validation rules for the model. You define validation rules by returning an object that contains the rules. Each key in the object is a field name; the corresponding value is an array of validation rules. A rule is a function (anonymous or named) that accepts the field value and its name. The function should return an error message as a string if validation fails. Otherwise, it must return a falsy value ('', undefined, 0, and so on).

Validation rules are evaluated whenever set() or save() is called on the model.

The following example shows validation rules for two fields: recipient and recipientphone. A single rule is defined for recipient, whereas two rules are defined for recipientphone.

			MySCModel.prototype.getValidationRules = function() {
				return {
					recipient: [
						function(value, name) {
							if (typeof value === 'undefined' || value.length === 0) {
								return 'You must enter a recipient name.';	
							}
						}
					],
					recipientphone: [
						function(value, name) {
							if (typeof value === 'undefined' || value.length === 0) {
								return 'You must enter a recipient phone number.';	
							}
						},
						function(value, name) {
							if (value.length >= 12) {
								return 'Phone number must be less than 12 digits.';
							}
						}
					]
				}
			}
			

If the model does not require validation, you don't need to implement getValidationRules(). Alternatively, you can simply return an empty string.

			MySCModel.prototype.getValidationRules = function() {
				return '';
			}
			
Returns

An object that contains the validation rules for the model attributes.


has( attributeName ) → {Boolean}

Description

Returns true if the attribute has a value (it is not null or undefined).

			var review = new ReviewModel();
			
			if (review.has('reviewername')) {
				// ...
			}
			
Parameters
Name Type Description
attributeName

The name of the attribute.

Returns

hasChanged( attributeName ) → {Boolean}

Description

Returns true if an attribute has changed since it was last set (with set()).

			var review = new ReviewModel();
			
			if (review.hasChanged('rating')) {
				// ...
			}
			
Parameters
Name Type Description
attributeName

The name of the attribute.

Returns

<private> initialize()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


isNew() → {Boolean}

Description

Returns true if the model is new (has never been saved to the server).

Returns

<private> isValid()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> preinitialize()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> previous()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> previousAttributes()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


save()

Description

Saves the model to the server. Before data is sent to the server, it validates the model attributes with getValidationRules(). If there are no errors, it saves to the server; otherwise, it returns false if the validation fails. Always validates regardless of whether attributes were passed.

			var review = new ReviewModel();
			
			review.set('reviewername', 'J. Smith');
			result = review.save(); // result can be a promise or false
			

set( attributeName, value )

Description

Sets an attribute of the model. When an attribute is set, it triggers validation for the model as defined in getValidationRules(). You can use getValidationErrors() to check for validation errors.

Parameters
Name Type Description
attributeName string

The name of the attribute.

value *

The value of the attribute.


<private> sync()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


<private> unset()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.


url() → {String}

Description

A property that returns the URL of the model with an additional parameter called internalid. The URL includes the URL root, as specified by urlRoot(), and the model ID as the value of internalid. url is called by default when you save or fetch from the server. The value of url must be a function that returns a string.

Returns

url The URL of the model including the parameter 'internalid' with the ID of the current model as its value.


<protected> urlRoot() → {String}

Description

Specifies the root URL of models. By default, the value is an empty string, so you must override urlRoot if you want to be able to save or fetch data from the server. The value of urlRoot must be a function that returns a string and should point to the appropriate resource on the server for the model.

			function MySCModel() {
				SCModel.call(this);
			
				this.urlRoot = function() {
					return Utils.getAbsoluteUrl(
						getExtensionAssetsPath("services/MainModule.Service.ss")
					)
				}	
			}
			
Returns

The url of the service that will handle the fetch and save operations of the model.


<private> validate()

Description

An internal method. Do not override this method. If you override this method, your SuiteCommerce website might not work as expected.