Skip to content
Corentin Bègne edited this page Mar 28, 2018 · 3 revisions

This library is manager by the ApiController provide some endpoint access using the Rest class.
The Api is accessible by ActionModel on client side calling api function.

Enpoints

http://localhost/api/[model]+[modeln]/find/
Api#find
All parameters are optionals

{
    conditions:[{ // array of all conditions to check
        type:"", // comparaison type (like, =, <, > ...)
        name:"", // name of the column
        value:"" // value to check
    }],
    fields:"", // list of the selected columns separated by a comma, default *
    order:"", // all mysql order possibilities
    offset:0, // offset to use with limit to get the good range
    limit:10 // Number of desired result, default 20
}

http://localhost/api/[model]+[modeln]/create Api#create
No parameters are required but it will check if all null field are set

{ // all column_name => value
    name:""
}

http://localhost/api/[model]+[modeln]/update
Api#create
The primary key column is required

{
    id:1 // primary key
}

http://localhost/api/[model]+[modeln]/delete http://localhost/api/getType http://localhost/api/complete

Example :

helpers/robot.js [js]

var RobotHelper;
(function(){
    "use strict";
    RobotHelper = function(cb){
        var that = this;
        extendSingleton(RobotHelper);
        this.name = "Robot";        

        require([
            "bower_components/cb-models/action"
        ], loaded);

        function loaded(){
            ActionModel.getInstance(loadedAction);            

            function loadedAction(instance){
                that.action = instance;
                if(isDefined(cb)){
                    cb(that);
                }
            }
        }
    };

    RobotHelper.getInstance = function(cb){
        if(isDefined(cb)){
            getSingleton(RobotHelper, cb);
        } else {
            return getSingleton(RobotHelper);
        }
    };

    // event binded on a form
    RobotHelper.prototype.create = function(element) {
        this.action.apiForm(this.name+"/create", new FormData($(element)[0]), check);

        function check(result){
            if(result.success){
                console.log("Robot created successfully");
            } else {
                console.error("Error on Robot creation", result.error);
            }
        }
    }; 

    // event binded on a form
    RobotHelper.prototype.update = function() {
        this.action.apiForm(this.name+"/update", new FormData($(element)[0]), check);

        function check(result){
            if(result.success){
                console.log("Robot updated successfully");
            } else {
                console.error("Error on Robot update", result.error);
            }
        }
    };          

    // ids must be primary key values
    RobotHelper.prototype.delete = function() {
        this.action.api(this.name+"/delete", {
            ids:[2]
        }, check);

        function check(result){
            if(result.success){
                console.log("Robots deleted successfully");
            } else {
                console.error("Error on Robot delete", result.error);
            }
        }
    };   

    // 
    RobotHelper.prototype.find = function() {
        this.action.api(this.name+"/find", {
            conditions:[{
                type:">",
                name:"ro_id",
                value:2
            }],
            fields:"ro_name", // list of the selected columns separated by a comma, default *
            order:"ro_name asc", // all mysql order possibilities
            offset:0, // offset to use with limit to get the good range
            limit:10
        }, check);

        function check(result){
            if(result.success){
                console.log("Robots successfully found", result.data);
            } else {
                console.error("Error on Robot find", result.error);
            }
        }
    };
})();