Using the REST extension - JavaScript
Procedure
What to do next
Creating a service instance first
Procedure
Defining the operations' implementation first
Procedure
Using client-side operations
You can use a REST service to trigger solicit and notify operations from within a JavaScript module.
Before you begin
sparkl.Service
constructor.Procedure
- Create a notify or solicit operation in the Editor.
-
Reference the REST-provisioned service in the operation's
clients
attribute. -
Depending on the type of the operation, use either:
- The
notify
method.importScripts("http://localhost:8000/rest/sparkl.Service.js") const restService = new sparkl.Service( "ws://localhost:8000", "Scratch/Discounts/REST"); // Invoke Start notify operation. It outputs only a FLAG field. restService.notify({ notify: "Mix/Impl/Alerts/Start" });
Thenotify
method takes a dictionary as its argument. It specifies:- The path to the notify in the user tree. The path is resolved relative to the REST service.
- The optional
data
dictionary, which declares the fields the notify outputs and the values of the fields. Not mandatory if the operation only outputs FLAG fields.
- The
solicit
method.importScripts("http://localhost:8000/rest/sparkl.Service.js") const tinyService = new sparkl.Service( "ws://localhost:8000", "Scratch/TinyTable/Mix/REST"); /** Callback function used by the Delete solicit. * If the delete operation fails, prints the value of the * error response field. */ var checkDelete = result => { if (result.response == "Error") { console.log(result.data.error); } }; // Invoke Delete solicit with the ID field, with a value of 3. tinyService.solicit({ solicit: "API/Delete", data: { id: 3 }}, checkDelete); };
Thesolicit
method takes a dictionary as its argument, and an optional callback function. The dictionary specifies:- The path to the solicit in the user tree. The path is resolved relative to the REST service.
- The optional
data
dictionary, which declares the fields the solicit outputs and the values of the fields. Not mandatory if the operation only outputs FLAG fields.
- The