DALIM DOCUMENTATION

Using the API inside a plugin

Written by Aurelien Knapp | Aug 25, 2023 2:03:43 PM

Introduction

Here are the requirements to follow, whenever a plugin has to deal with the API:

  • The functions getLoadModelMessage() and getStoreModelMessage MUST be implemented.
  • The model is not managed by the system, so you have to manage it inside the plugin.
  • To make the save button available the method loadModel() has to be explicitly called.
  • To manage the serverResponse you have to implement the serverUpdate() method.

 

Example


var APIDemo = {
    buildComponent: function() {
        // Fetches data from the API
        this.loadModel();
 
 
        // Creates an input element
        var input = document.createElement('input');
        input.value = 'DEFAULT VALUE';
        input.onchange = function() {
            // On change, the system est explicitely informed about the modification
            this.setHasChanged();
        }.bind(this);
 
        // Returns the HTML element
        this.input = input;
        return input;
    },
    serverUpdate: function(response) {
        // If the server returns from the job.get query, set the value of the description into the // input field.
        if (response.method == 'job.get' && response.result != null) {
            this.input.value = response.result.description;
        } else if (response.method == 'job.edit' && response.result != null) {
            console.log(response.result.status);
        }
    },
    update: function() {
        // Nothing here to keep the example simple.
    },
    getLoadModelMessage: function() {
        // Fetch the job from the API.
        var id = this.getModelProperty('ID');
        var params = {
            id: 1,
            method: 'job.get',
            params: {
                ID: id
            }
        };
        return params;
    },
    getStoreModelMessage: function() {
        // If the save button is triggered, the value is saved with the API.
        var id = this.getModelProperty('ID');
        var params = {
            id: 2,
            method: 'job.edit',
            params: {
                ID: id,
                description: this.input.value
            }
        };
        return params;
    }
};