Rendering HTML Data Using Backbone.js in SuiteCommerce

Rendering HTML in SuiteCommerce utilizes Backbone.js views, templates, and contexts to structure data effectively.

·2 min read·View Oracle Docs

The rendering process for HTML in SuiteCommerce involves Backbone.js views, which play a fundamental role in managing UI elements and data integration. By defining templates, contexts, and utilizing the Handlebars.js library, developers can create dynamic, interactive web pages tailored to custom requirements.

How Does View Architecture Work?

Within the Backbone.js framework, views listen to user interface events and coordinate with the necessary data provided by the associated module. There are two primary ways to initialize a view:

  • When a router initializes it, a model or collection containing the required data is also passed.
  • In other cases, the view includes all required models or collections as dependencies.

Important Note: If customizing SuiteCommerce Advanced (SCA) for versions 2020.2 and later, use SCView instead of Backbone.View for enhanced functionality.

Structure of Backbone Views

All Backbone.Views are Backbone.CompositeViews by default, allowing for child views, which modularize the application. Each parent view must declare its child views within a childViews property, as illustrated below:

javascript
1childViews: {
2 'Buttons': {
3 'Wishlist': {
4 'childViewIndex': 10,
5 'childViewConstructor': function() {
6 return new WishlistView();
7 }
8 },
9 'SaveForLater': {
10 'childViewIndex': 20,
11 'childViewConstructor': SomeView
12 }
13 }
14}

Rendering HTML with Templates

The process of rendering the HTML is managed by the templating engine and Backbone.js framework. A view must define:

  • A template file included as a dependency.
  • A template property within the view body.
  • A getContext method to return all data attributes required for the template.

The Gulp.js tooling compiles these components into the application when SCA is built.

Example of a Template Context Using Handlebars.js:

html
1<div class="case-list-results-container">
2 {{#if hasCases}}
3 <table class="recordviews-table">
4 ...
5 </table>
6 {{else}}
7 <p class="case-list-empty">{{translate 'No cases were found'}}</p>
8 {{/if}}
9</div>

Key Functions for Custom Views

To manage child views effectively, developers can employ methods such as addChildViews(), which allows adding to both view classes and instances. Additionally, when initializing a view, plugins can be added, with specific operations executed before and after the initialization, providing greater control over view behavior.

Backward Compatibility For compatibility with earlier SCA versions, the CompositeView.add() method acts as a no-operation function to prevent breaking custom implementation when integrated with previous releases.

Conclusion

Understanding how to render data from views using Backbone.js and templates is crucial for developers looking to enhance user interactions and display in SuiteCommerce. Proficient use of these features can significantly improve application structure and performance.

Frequently Asked Questions (4)

Do I need to use SCView instead of Backbone.View in SuiteCommerce Advanced 2020.2 and later?
Yes, for SCA 2020.2 and later, it is recommended to use SCView instead of Backbone.View to leverage modern capabilities.
How can I declare child views in a parent view within SuiteCommerce?
Child views are declared using the childViews property of the parent view. Each child view entry should specify a childViewIndex and a childViewConstructor to determine rendering order and construction.
What methods and properties are essential for template rendering in Backbone.js within SuiteCommerce?
Each view should include a define method for the template file, a template property within the view, and a getContext method to supply the necessary data attributes for the template.
Is it possible to add child views dynamically after the initial declaration in SuiteCommerce?
Yes, child views can be added later using the addChildViews() method, either in the view class or an instance.
Source: Rendering Data From the View 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 →