Loading Script Files
- 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.
If you'd like to execute a script when the Saber Editor loads, include the file editor.js in your vendor plugin folder. Saber will automatically compile all editor.js files from all vendors into /wwwroot/editor/js/vendors-editor.js, which is loaded when the user loads Saber's Editor.
This function is used to call web APIs (IVendorService) and includes both OnComplete and OnError callback arguments.
- 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.