How to execute script from clientΒΆ

You can use server method to send a request to the server to execute a function defined in the server module of an item.

En the example below we create the btn button that is a JQuery object. Then we use its click method to attach a function that calls the server method of the item to run the calculate function defined in the server module of the item.

The code in the client module:

function on_view_form_created(item) {
    var btn = item.add_view_button('Calculate', {type: 'primary'});
    btn.click(function() {
        item.server('calulate', [1, 2, 3], function(result, error) {
          if (error) {
            item.alert_error(error);
          }
          else {
            console.log(result);
          }
        })
    });
}

The code in the server module:

def calculate(item, a, b, c):
    return a + b + c