views:

220

answers:

5

Hi,

I understand that there is no multithreading support in javascript. And i wanted some expert advice on the below scenario..

My requirement is to perform a AJAX call and upon successful completetion, i want to trigger set of events (to update the different parts of UI parallely)

  • I am planned to use Subscribe/Publish pattern, is it possible to subscribe multiple listners to the AJAX completion event.
  • If possible, i wanted to know how these listners notified on publish.. (parallely in muthithreaded way or one by one).

    And suggest me the best way to achive this one.. I really appreciate your thoughts.

EDIT::

I know there are popular frameworks like JQuery supports this pattern. But am in a situation to develop this functionality from a scratch (we have our own framework).

Cheers

Ramesh Vel

A: 

This is possible and you should really use a library for that to avoid all the browser incompatibilities. For example jQuery provides Ajax functionality that lets you execute code on completion of the Ajax call. See here for documentation.

ujh
not really using any frameworks.. need to implement this in core js framework.. see my edits above.. And this is going to be IE only app..
Ramesh Vel
I'd still use a framework. It's just so much easier that way.
ujh
+2  A: 

This article describes what you're trying to accomplish pretty closely. Essentially you just have a JavaScript file that holds an array of handlers/subscribers. Each subscriber registers itself with the publisher (i.e. gets added to the handlers array). Then in the onClose handler of your Ajax call, you'd call a function that iterates over the subscribers and notifies them each by name:

this.handlers = [];
...
 for(var i = 0; i < this.handlers.length; i++)
  {
    this.handlers[i].eventHandler.call(this, eventArgs);
 }
...
yalestar
thanks for the reply Yalestar.. it seems the given script calls the listeners in a seqution manner(one by one).. is there a way to simulate this to parallel one...
Ramesh Vel
A: 

if you want to fire functions in parralell use following:

this.handlers = [];
...
 for(var i = 0; i < this.handlers.length; i++)
  {
    setTimeout(function(){this.handlers[i].eventHandler.call(this, eventArgs);},0);
 }
...
Eldar Djafarov
+1  A: 

I've a Request Pooler that might give you a good head-start here.

I'm not sure that'll do everything you want (although it sounds like it may be close). It's old, but it works:

Depressed Press DP_RequestPool

It supports multiple simultaneous requests with timeout/retry, per-request handlers, has a very small footprint and can be combined with other code easily.

You create a pool (telling it how many simultaneous requests are allowed) and then toss requests at it. When they're done they call whatever handler you specified.

A small, complete example of it's use:

        // The handler function
function AddUp(Num1, Num2, Num3) {
    alert(Num1 + Num2 + Num3);
};

    // Instantiate the Pool
myRequestPool = new DP_RequestPool(4);

    // Start the Interval
myRequestPool.startInterval(100);

    // Create the Request
myRequest = new DP_Request(
    "GET",
    "http://www.mysite.com/Add.htm",
    {"FirstNum" : 5, "SecondNum" : 10},
    AddUp,
    [7,13]);

    // Add the request to the queue
myRequestPool.addRequest(myRequest);

It's open source - feel free to chop/fold/spindle or mutilate it to your hearts content.

Jim Davis

Jim Davis
thanks for sharing Jim, Its realling interesting one,, it seems the DP_requestpool will handle multiple AJAX reuests simultaneously... But in my context i wanted to fire multiple events parallely on the completion of single AJAX response...
Ramesh Vel
Actually the same framework that allows you to launch several AJAX requests simultaneously should be easily adoptable to launching several of pretty anything simultaneously.You modify the object from a specific request pool to a more general "statement pool" pretty easily I think.
Jim Davis
+1  A: 

Could this serve as a ligthweight message passing framework?

function MyConstructor() {
    this.MessageQueues = {};

    this.PostMessage = function (Subject) {
        var Queue = this.MessageQueues[Subject];
        if (Queue) return function() {
                                        var i = Queue.length - 1;
                                        do Queue[i]();
                                        while (i--);
                                    }
        }

    this.Listen = function (Subject, Listener) {
        var Queue = this.MessageQueues[Subject] || [];
        (this.MessageQueues[Subject] = Queue).push(Listener);
    }
}

then you could do:

var myInstance = new MyConstructor();
myInstance.Listen("some message", callback());
myInstance.Listen("some other message", anotherCallback());
myInstance.Listen("some message", yesAnotherCallback());

and later:

myInstance.PostMessage("some message");

would dispatch the queues

Juaco
Thanks Juaco, this is a nice example....
Ramesh Vel