Nice template to use for multiple xmlhttprequests used by different processes

I wonder what a good template to use when you can have multiple xmlhttprequests that are part of different processes like (login validation, tooltip fetching and display, subrecords display / detail opening).

Your input in my code is still more than welcome, so there are some helpful articles for reference on asynchronous process handling.

Here is what I am so far trying to use the mediator and trying to define the sequence of events that are triggered by the mediator and initiated by the worker for a specific process

var mediator={
    events:[],
// bind functions to events, optionally once only if this function doesn't
// need to handle the event during the lifetime of the application
    addListener:function(name,processor,onceOnly){
        if(!mediator.events[name]){
            mediator.events[name]=new Array({
                processor:processor,
                once: onceOnly ? true : false
                });
            return;
        }
        mediator.events[name].push({
                processor:processor,
                once: onceOnly ? true : false
                });
    },
    trigger:function(name,data){
        var i=0;//check if mediator[name] exist
        for(i=0;i<mediator.events[name].length;i++){
            try{
                mediator.events[name][i].processor(data);
// problem is when a use once handler is the 3rd in the chain and the
// second handler fails then the 3rd is never removed
// could trigger an error here that has a cleaner listner
            }finally{
                if(mediator.events[name][i].once){
                    mediator.remove(name,mediator.events[name][i]);
                }
            }
        }
    },
// removing listener from event
    remove:function(name,event){
        for(var i=0;i<mediator.events[name].length;i++){
            if(mediator.events[name][i]==event){
                mediator.events[name].splice(i,1);
                return;
            }
        }
    },
// used to provide an event chain through data that will execute a certain
// process
    triggerNext:function(data){
        // some checks on data
        mediator.trigger(data.events[data.index++],data);
    }
}
// possible response parsers
var parser=function(type){
    var parseLogin=function(data){
        console.log(data);
// should call triggerNext here for the worker to be notified.
    }
    if(type=="loginParser"){
        return parseLogin;
    }
}
// connects and triggers next
var connector=function(){
    this.response="";
    this.commObject=null;
    this.connect=function(data){
        $.get(data.url, function(res) {
            data.commObject=this;//maybe you'd like to inpect it
            data.response=res;
            mediator.triggerNext(data);
        });//trigger fail event if failed
    };
}
// example of initiating a process
$("document").ready(function(){
//add all listeners that are used during the entire execution 
// of the application here
    var p=parser("loginParser");
    mediator.addListener("checkLogin",p);
//the following is a temporary listener, this code would be in
// a worker object initLogin function.
    var c=new connector();
    mediator.addListener("connect",c.connect,true);
// the data determines what process will be invoked
// this could be in a worker.initLogin function
    var data={
        processType:"SendLoginAndCheck",
        url:"test.html",
        post:"",//could check in the connector.connect to see if post is set
        events:["connect","checkLogin"],
//there is no worker.afterLogin but the 3rd event could be finishprocess
//and a worker object function can be called to process that
        index:0
    }
//start the process
    mediator.triggerNext(data);
});

      

+1
javascript asynchronous mediator xmlhttprequest


source to share


No one has answered this question yet

See similar questions:

3
How do you know if everyone is loaded on the page?
0
How to hide only and show using any show function in jQuery
-1
Synchronous access to a structure filled with an asynchronous request

or similar:

4086
What's the difference between using "let" and "var"?
2984
What is the difference between a call and an application?
1966
How can I select an element with multiple classes in jQuery?
1700
What is the difference between Bower and npm?
1497
Differences between lodash and underscore
1434
Difference between == and === in JavaScript
953
Access-Control-Allow-Origin Multiple Domains of Origin?
527
XmlHttpRequest error: Origin null is not allowed Access-Control-Allow-Origin
515
Nice use case for Akka
479
Send POST data using XMLHttpRequest



All Articles
Loading...
X
Show
Funny
Dev
Pics