Developer Center
JavaScript Library
Saber uses the Datasilk Core JS library used for UI functionality along with Selector, a jQuery replacement library that weighs in at 5kb (gzipped). This documentation is focused on giving developers information on core features of Saber's JavaScript libraries. You can learn more in-depth knowledge about these two libraries by following the links above.
Loading Script Files
When building your vendor plugin for Saber, there are various ways to load your JavaScript files into the web browser.
- Both the Controller and Service classes contain the AddScript(string url, string id = "", string callback = "") method which is a part of the inherited IRequest interface. You can learn more about loading script files from the Saber.Core.IRequest documentation.
- You can load script files for your HTML components from the HtmlComponentModel.Render method since one of the arguments for the Render method references the current IRequest instance. Similar to how the Controller class uses the AddScript(string url, string id = "", string callback = "") method to include a <script> tag at the bottom of the web page, Saber will render a <script> tag at the bottom of the web page when calling the AddScript() method from your HtmlComponentModel.Render method. You can learn more about the HtmlComponentModel class from the IVendorHtmlComponents documentation.
- You can load script files from the IVendorViewRenderer interface since it contains an argument reference for the current IRequest instance. The View Renderer is rendered any time a specified View is being rendered, so if you execute the AddScript() method, it could belong to either a Controller or Service class.
- You can load script files from the IVendorWebsiteSettings interface since it contains an argument reference for the current IRequest instance. From there, you can execute the IRequest.AddScript() method. IVendorWebsiteSettings is executed when the user visits the Website Settings tab within the Saber Editor.
S.ajax.post
This function is used to call web APIs (IVendorService) and includes both OnComplete and OnError callback arguments.
S.ajax.post('ShoppingCart/AddToCart', {productId: id}, (response) => { //process response }, (err) => {//process error});
- For the URL, use your IVendorService class name & method name. For example, 'ShoppingCart/AddToCart' references the AddToCart method located within the ShoppingCart class that inherits IVendorService.
- The second argument takes an object with properties that will be mapped to your IVendorService method arguments.
- The third argument is the OnComplete callback function which contains the responseText as an argument. If you sent a serialized JSON string, set the fifth argument for S.Ajax.Post to true to make the OnComplete callback function use a JSON object as an argument instead of responseText.
- The fourth argument is the OnError callback function which contains the XMLHttpRequest object as an argument. This callback is executed if there was a response with a status code less than 200 or greater than or equal to 400, or if the XHR object raised an error when trying to connect to the remote server.
- The fifth argument is optional and if set to true, will deserialize the responseText as a JSON object in the OnComplete callback function.