Client JS SDK

While ServiceM8 add-ons are executed server-side, once rendered add-ons are able to interact with the ServiceM8 platform client side by including the ServiceM8 Client JS SDK in your rendered HTML event responses.

To expose the JavaScript API to your app, simply include the sdk.js at the top of your add-on’s <head> element. It has no dependencies but plays well with other libraries like jQuery.

<head>
  <script src="https://platform.servicem8.com/sdk/1.0/sdk.js"></script>
  ...
</head>

The SDK should be initialized by calling the init() function immediately after it is included.

<head>
  <script src="https://platform.servicem8.com/sdk/1.0/sdk.js"></script>
  <script type="text/javascript">
    var client = SMClient.init();
  </script>
  ...
</head>

Resizing Windows

If your add-on event has been launched in a popup window (Job actions, Client actions, etc), then you can resize the popup window as required.

Method

client.resizeWindow(width, height);

Arguments

Width: The width of the popup window
Height: The height of the popup window

Returns

A Promises/A+ conformant promise object.
##Example

<script type="text/javascript">
    var client = SMClient.init();
    client.resizeWindow(450, 600).then(function(result) {
        console.log("Add-on Window Resized");
    });
 </script>

Closing Windows

If your add-on event has been launched in a popup window (Job actions, Client actions, etc), then you can close/destroy the popup window as required.
##Method

client.closeWindow();

Returns

A Promises/A+ conformant promise object.
##Example

<script type="text/javascript">
    var client = SMClient.init();
    client.closeWindow();
 </script>

Refreshing Data

Once your add-on has been rendered into the ServiceM8 UI, you may need to refresh your content, or perform additional server-side processing. The invoke function allows you to pass requests from client side back to your server side function / web service.
##Method

client.invoke(eventName, eventParams);

Arguments

EventName: The event name you wish to invoke server-side
EventParams: An object containing key/values you wish to pass to your server-side event
##Returns
A Promises/A+ conformant promise object.
##Example

<script type="text/javascript">
    var client = SMClient.init();
    client.invoke("Add-on_Custom_event_here", {
			'emailTo': 'test@example.com'
    }).then(function(result) {
        console.log("Server-side event invoked with result " + result);
    });
 </script>