1

I'm trying BaaSbox, a free Backend as a Service. But it has no out-of-the-box Javascript support I can use right away (yet, only iOS and Android)

I'm having trouble sending the right curl command from javascript, anyone happen to know a good resource or a simple working $.ajax template? I've tried a few examples from stackoverflow, but none of them specifically aimed at BaaSbox.

I've tried following the Java instructions on their site here. Just making a simple login work, but I keep getting the wrong responses from the server.

Or on the other hand, anyone know a good, free alternative to BaaSbox? I just want to be able to install it on my own server, no paid plans or whatever.

LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
TrySpace
  • 2,233
  • 8
  • 35
  • 62

1 Answers1

2

in the download page there is a preliminary version of the JS SDK (added few days ago). The documentation is on the way, however in the zip file you can find a simple example.

For example to perform a signup:

//set the BaasBox parameters: these operations initialize the SDK
BaasBox.setEndPoint("http://localhost:9000"); //this is the address of your BaasBox instance
BaasBox.appcode = "1234567890"; //this is your instance AppCode 

//register a new user
BaasBox.createUser("user", "pass", function (res, error) {              
    if (res)  console.log("res is ", res);
    else      console.log("err is ", error);
});

Now you can login into BaasBox

//perform a login
$("#login").click(function() {
    BaasBox.login("user", "pass", function (res, error) {
        if (res) {
                        console.log("res is ", res);
                        //login ok, do something here.....
                 } else {
                        console.log("err is ", error);
                        //login ko, do something else here....
                 }
    });

Once the user is logged in he can load the Documents belonging to a Collection (the SDK automatically manages the Session Token for you):

BaasBox.loadCollection("catalogue", function (res, error) { //catalogue is the name of the Collection                   
        if (res) {
            $.each (res, function (i, item) {
                console.log("item " + item.id);  //.id is a field of the Document   
            });     
        } else {            
            console.log("error: " + error);             
        }       
});

However under the hood the SDK uses JQuery. So you can inspect it to know how to user $.ajax to call BaasBox.

For example the creatUser() method (signup) is:

    createUser: function (user, pass, cb) {

        var url = BaasBox.endPoint + '/user'

        var req = $.ajax({
            url: url,
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                username: user,
                password: pass
            }),
            success: function (res) {

                var roles = [];

                $(res.data.user.roles).each(function(idx,r){
                    roles.push(r.name);
                })

                setCurrentUser({"username" : res.data.user.name, 
                                "token" : res.data['X-BB-SESSION'], 
                                "roles": roles});

                var u = getCurrentUser()
                cb(u,null);
            },
            error: function (e) {
                cb(null,JSON.parse(e.responseText))
            }
        });

    } 
Giastfader
  • 136
  • 2