Question

I want two separate files one for ajax-calls (API, jquery) and one for the code for visualizing the data (jquery, knockoutjs). The API has one function for the ajax-calls, to which few parameters will be passed, including the success- and error-handlers. Reason for that: I want to define the success-handler (which should be able to be defined "individually" - depending on the service which is called) in the file which contains the client-side code.

In API-file:

  var GET_ALL_WORDS = '/dict/rest/dictionary';//get

   ...

    /** Parameters:
    * GET/POST
    * URL
    * data: word record - json OR searchedWord, single word- String OR null
    * successHandler - function handling returned data (json)
    * error-handler - general handler for errors, defined in this file
    **/

  function dictRPCcall(method, url, data, successHandler, failedHandler) {
    console.log("in dictRPCcall");
        $.ajax({
            type: method,
            contentType: "application/json",
            data: data,
            url: url,
            success: successHandler,
            error: failedHandler,
            dataType: "json"
        });
    }

   //functions that will be called by client-side.

    function getWordsRPCcall(successHandler) {
    console.log("in WordsRPCcall")
        dictRPCcall(GET, GET_ALL_WORDS, null, successHandler, failedREST);
        console.log("afer ajax-call in WordsRPCcall")
    }

    function insertWordRecordRPCcall(wordRecord, successHandler) {
        dictRPCcall(POST, INSERT_WORD_RECORD, wordRecord, successHandler, failedREST);
    }
...

In client-js-file:

    function successHandler(data, textStatus, jqXHR ){
    //handling responded data from Server
    console.log("successHandler " + "textstatus:" +textStatus + "data: " + data);
    }

    ...calling getWordRPCcall and passing in the success-handler ....

Questions:

  • Is it possible to define the success-handler in another file than the ajax-call is?
  • How has success-handler function access to the jqXHR-object and the other parameters returned and passed to the callback-function of the ajax-call?
  • If it is not possible - How can I manage to "dynamicize" the success-handler?
  • Am I totally wrong to try it like that and just not good or something like that?

Please let me know if the question was not good and some information needs to be added. So long, Meru

Was it helpful?

Solution

changing

success: successHandler,

to:

  success: function(data, textStatus, jqXHR) { 
      successHandler(data, textStatus, jqXHR); 
   },

This makes the call and passes over those arguments.


We are assuming here that the client js containing the function successHandler is loaded and available.

edit/update:

actually success: successHandler, passes over the params, as it is the function ( duh me ) - all the same, leaving this here, as, maybe explains what is going on more literally

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top