Views for SuiteCommerce Extensions: Utilizing SCView

Views in SuiteCommerce extensions handle templates, context data, and user interface events through the SCView class, streamlining functionality.

·2 min read·1 views·View Oracle Docs

TL;DR Opening

Views in SuiteCommerce extensions are crucial as they dictate how templates are rendered, manage context data, and respond to user interface events. Utilizing the SCView class, introduced in SuiteCommerce 2020.2, developers can streamline functionality while implementing essential business logic in their custom extensions.

How Do Views Work in an Extension?

In SuiteCommerce, views serve as the bridge between the application logic and the user interface. They are primarily responsible for:

  • Specifying which template to render.
  • Passing context data to the templates for dynamic content handling.
  • Listening and reacting to user interface events.

Utilizing the SCView Class

Developers use the SCView component from the Extensibility API to create views in their extensions. Being an abstract class, SCView requires you to include it as a dependency in your extension's module rather than instantiating it directly. The important methods to override in your view subclass include:

  • getContext(): Used to provide context data for the template. If no specific context data is available, an empty object should be returned.
  • getEvents(): Allows you to define event listeners and their corresponding handlers.

Example of a Basic View

Here is an example of a basic view utilizing the SCView class:

javascript
1"text-purple-400">define(
2 'NetSuite.MyExtension.MainView',
3 [
4 'SCView',
5 'netsuite_myextension_mainview.tpl'
6 ],
7 "text-purple-400">function(SCViewComponent, netsuite_myextension_mainview_tpl) {
8 'use strict';
9
10 "text-purple-400">var SCView = SCViewComponent.SCView;
11
12 "text-purple-400">function MyExtensionMainView() {
13 SCView.call("text-purple-400">this);
14 "text-purple-400">this.template = netsuite_myextension_mainview_tpl;
15 }
16
17 MyExtensionMainView.prototype.getContext = "text-purple-400">function() {
18 "text-purple-400">return {
19 quantityintransit: '10',
20 quantityonhand: '25'
21 };
22 };
23
24 MyExtensionMainView.prototype.getEvents = "text-purple-400">function() {
25 "text-purple-400">return {
26 'click #getExtendedStockInfo': 'getExtendedStockInfo'
27 };
28 };
29
30 MyExtensionMainView.prototype.getExtendedStockInfo = "text-purple-400">function() {
31 // code goes here
32 };
33
34 "text-purple-400">return MyExtensionMainView;
35 }
36);
  • Template Specification: Assign the template using this.template within the constructor.
  • Accessing Context Data: In templates, leverage Handlebars expressions, like this:
    html
    <div>{{quantityintransit}}</div>

Event Listening in Views

To set up event listeners, the getEvents() method must return an object. Each key represents an event, while the corresponding value holds the function to execute when the event is triggered. For example:

javascript
MyExtensionMainView.prototype.getEvents = "text-purple-400">function() {
"text-purple-400">return {
'click #getExtendedStockInfo': 'getExtendedStockInfo'
};
};

In this instance, clicking an element with the ID getExtendedStockInfo will execute the getExtendedStockInfo method.

Conclusion

Views are integral to extending the functionality of SuiteCommerce applications. By leveraging the SCView class and effectively managing context data and events, developers can enhance user experiences and control application behavior.

Key Takeaways

  • Views dictate template rendering and interface event handling in SuiteCommerce extensions.
  • Use the SCView class for defining views starting from SuiteCommerce 2020.2.
  • Override the getContext() and getEvents() methods to provide dynamic data and define UI interaction.
  • Templates can utilize context data accessed through Handlebars.

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

Frequently Asked Questions (4)

Does the SCView class apply to all SuiteCommerce implementations?
The SCView class applies to SuiteCommerce extensions as of SuiteCommerce 2020.2. It is used to streamline functionality in custom extensions.
What methods are crucial to override when using the SCView class?
When using the SCView class, it is crucial to override the `getContext()` and `getEvents()` methods. These methods provide dynamic data for templates and define user interface interactions, respectively.
How do I define a custom template in a view using the SCView class?
In a view using the SCView class, a custom template is defined by setting the `this.template` property in the view's constructor with the template module you want to use.
Can I instantiate the SCView class directly for my SuiteCommerce extension?
No, you cannot instantiate the SCView class directly because it is abstract. You must include it as a dependency and extend it when creating your own view classes.
Source: Views 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 →