Lesson 1
2. Widgets controller
In the Service Portal Framework, a widget is a reusable component that performs a specific function (like displaying a catalog item, knowledge article, or chart). Each widget has a controller that handles its logic and behavior.
- The client controller receives user input from the HTML template (e.g., button clicks, form submissions).
- It then processes this input, potentially performing calculations, validations, or other logic.
- For instance, it might update data displayed in the widget or prepare data to be sent to the server.
- The client controller uses
c.server.get()to retrieve data from the server-side script, often passing an object as input to the server. - It then handles the promise returned by
c.server.get(), processing the data received from the server. - It uses
c.datato pass data to the HTML template for rendering. - When the user interacts with the widget and changes data, the client controller can use
c.server.update()to send updated data back to the server.
- The client controller utilizes
c.server.get()andc.server.update()to communicate with the server. - These methods send requests to the server-side script and handle the responses.
c.server.get()is used to fetch data from the server, whilec.server.update()is used to send updated data to the server.
- The client controller updates the
c.dataobject with the data it receives from the server or processes locally. - This updated
c.datais then used by the HTML template to render the widget’s content. - The client controller can also call functions defined in the HTML template using
c.func().
-
cvariable:In Service Portal widgets, the
cvariable (often aliased asthis) is used to access the client controller’s scope. It provides access to the widget’s properties and methods. -
$scopevariable:While
cis preferred,$scopeis also available and can be used, especially when dealing with multiple scopes or needing to target a specific scope. -
dataobject:The
dataobject is used to pass data between the client controller and the HTML template. The server script populates thedataobject, and the client controller can modify it before passing it to the template. -
inputobject:The
inputobject is used to pass data from the client controller to the server script, often viac.server.get()orc.server.update(). -
c.server.get():This method sends a request to the server-side script and returns a promise that resolves with the data.
-
c.server.update():This method sends updated data to the server-side script.
-
$rootScope.$broadcast():This method is used for cross-widget communication, broadcasting an event that can be listened to by other widgets.
