Page 1 of 1

Setting and executing a service call from javascript with Mapping, beforeSend, onSuccess, and onError callbacks

Posted: Mon Oct 12, 2015 5:26 pm
by Jon Haider

Hey guys,

This is just a question that I've been trying to find some information on and can't seem to locate it anywhere. I want to be able to call a service using code service.execute({});/code, but I want to be able to manipulate some of the callbacks from the javascript code. I found that we can do this:
pre
service.execute({
success: function(e) {
//Success handler here
},
error: function(e) {
//Error handler here
},
complete: function(e) {
//Complete handler here
},
});
/pre

but I've also seen Yurii post this notation:

pre
uploadService2.execute({
'allowDataModification': false,
'processData': false,
'body': {data: data},
'cache': false,
'success': onSuccess
});
/pre

I have 3 questions:
1- Should the success be in quotes, as shown in Yurii's response, or not, as in the previous one?
2- What would we use for the "Before Send" handler?
3- How would we set the data mapping from this javascript?

I've looked at the documentation here, but there is no information on how to create a code snippet that is equivalent to using the Appery's mapping feature (maybe it's just the same as code"body"{data:data}/code?), and no information on setting the "Before Send" handler function.


Setting and executing a service call from javascript with Mapping, beforeSend, onSuccess, and onError callbacks

Posted: Wed Oct 14, 2015 8:11 am
by Serhii Kulibaba

Hello,

1) No, it shouldn't be in quotes
2) You have to add "data" parameter, which send request parameters. It is the same as "before send" event: https://getsatisfaction.com/apperyio/...
3) You have to use one of two variants: mapping or execution JS.


Setting and executing a service call from javascript with Mapping, beforeSend, onSuccess, and onError callbacks

Posted: Thu Oct 15, 2015 2:35 am
by Jon Haider

Thank you Sergiy for the response. Just to clarify:

  1. Great, that's clear.

  2. Is there an example where someone used the "data" parameter (just to see what the code should look like)? I can't find anything in the documentation either. Or would it simply be like:
    pre
    service.execute({
    data: function(e){
    //Before Send handler here
    },
    success: function(e) {
    //Success handler here
    },
    error: function(e) {
    //Error handler here
    },
    complete: function(e) {
    //Complete handler here
    },
    });
    /pre

  3. Lastly, I know we can use the mapping functionality, but I would like to know HOW to do the JS execution of the mapping. Is there a sample code for this? In other words, if I have a service that looks like:
    Image
    As an example, how would we create the equivalent using:
    pre
    restservice1.execute({
    data: function(e){//is the use of "data" correct here?
    /What is the JS execution for mapping here, if suppose we are mapping a LSV named "UserName" to the request parameter "username"?/
    },
    success: function(e) {
    /What is the JS execution for mapping here, if suppose we are mapping the response parameter "Status" to the LSV named "userStatus"?/
    },
    error: function(e) {
    //Error handler here
    }
    });
    /pre

    Thank you again for the clarification and reference!


Setting and executing a service call from javascript with Mapping, beforeSend, onSuccess, and onError callbacks

Posted: Thu Oct 15, 2015 6:59 am
by Jon Haider

I think I got it. I created a simple server code script, added it as a service, and created No mapping and no javascript where the service was added to the page. Then when I call for the execution of the service, I define the "data" mapping, as well as what it should do on success and on error. Here's my example:

pre
function testButtonClick(){
var onSuccessHandler = function(data){if(data){alert(data.replied);}};
var onErrorHandler = function(data){if(data){alert(JSON.stringify(data));}};
sentReplied.execute({
data:{"sent":localStorage.getItem("username")},
success:onSuccessHandler,
error: onErrorHandler
});
}
/pre

And it works great.

Thanks again Sergiy for the help.


Setting and executing a service call from javascript with Mapping, beforeSend, onSuccess, and onError callbacks

Posted: Thu Oct 15, 2015 7:17 am
by Illya Stepanov

Thanks for the update, Jon!