> -----Original Message-----
> From: James Smith [mailto:[EMAIL PROTECTED]
> Sent: Monday, December 17, 2007 6:25 AM
> To: CF-Community
> Subject: RE: JS HTTP Request?
> 
> Ok, could I have some help...

You've got it.
 
> function updateMinSale(rowNum) {
>       var tempURL =
> 'http://127.0.0.1/cfc/calculators.cfc?method=minPriceCalc&productgroupi
> d=2&c
> ostprice=2';
>       myRequestPool = new DP_RequestPool();
>       myRequest = new DP_Request("GET",tempURL);
>       document.getElementById("min" + rowNum).value = WHATGOESHERE;
> }

Set up the Pool first - this really should be a "global" object since it's
managing requests.  As you have it you'll create a new pool every time when
all you need is a new request.  You also have to start the pool with the
"startInterval" for it to start looking for requests.

You can do this in one line (create the pool and start it looking) like
this:

myRequestPool = new DP_RequestPool().startInterval(100);


> For testing I am using the URL set in the tempURL variable, the
> parameters
> will change later but will do for now.  The CFC returns a simple
> numeric
> value like...
>
> <wddxPacket
> version='1.0'><header/><data><number>2.26</number></data></wddxPacket>

First, there's a slight wrinkle here:  Since your CFC is returning a WDDX
packet you'll have to deal with that packet.  We'll get to that later - for
now let's simplify things a bit and assume that your CFC returns simply
"2.26" (a plain number) rather than a WDDX packet.

Secondly remember that requests, in JavaScript, are going to be asynchronous
(that's the "A" in "AJAX"): the request get's "sent" and the next line of
code is run right away, BEFORE the response comes back.  Because of this you
have to separate your "calling" code from your "receiving" code.

There are other ways but the simplest is to have one function to make the
call and another to use the response (a "handler").

Your call function might be "getMinSale()" and your handler might be
"updateMinSale".

Also remember that you must add the request to the Pool for it to be
processed.  Doing "new DP_Request" just "sets it up", but doesn't actually
make the call:

function getMinSale(rowNum) {
                // Your URL
        var tempURL =
'http://127.0.0.1/cfc/calculators.cfc?method=minPriceCalc&productgroupid=2&c
ostprice=2';
                // Create your Request
        var myRequest = new DP_Request(
                                        "GET",
                                        tempURL,
                                        null,
                                        updateMinSale,
                                        [rowNum]);
                // Add the request to the queue (without this bit nothing is
run)
        myRequestPool.addRequest(myRequest);
};

The Parameters used to create the request are:
        "GET": Make it a "GET" request.
        tempURL: To that URL
        null: There are no additional arguments to pass to the URL
        updateMinSale: This is the function (the handler) that will be
called when the call is done and process the (no parens here)
        [rowNum]: An array of additional parameters to pass to the handler.

This will make the call to your URL, get the response and then call the
handler.  The first argument passed to the handler is ALWAYS the result of
the call (the response from your CFC).  We've added more parameters (the
"[rowNum]") so the call to the handler, based on the request above will be:

updateMinSale(result, rowNum)

Your function needs to "know" that it's getting the result back as the first
parameter.  Again (for a minute),assuming that the response is "plain" and
not WDDX the handler would be pretty simple:


function updateMinSale(result, rowNum) {
        document.getElementById("min" + rowNum).value = result;
};

But - your code IS returning WDDX - so you have one more thing to do.
Luckily I've also got a JavaScript WDDX component that will solve this
problem here:

http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_W
DDX

You have to load this library to use it (duh) but once it's loaded you can
"extract" (deserialize) a WDDX packet to usable JavaScript data like this:

var MyObject = dpWDDX("deserialize", MyWDDX);

This would change our handler function to:

function updateMinSale(result, rowNum) {
                // Extract the WDDX data
        var myResult = dpWDDX("deserialize", result);
                // Update the Field
        document.getElementById("min" + rowNum).value = myResult;
};

Still pretty simple actually.

So, anyway.

The whole mess together would be (remember that this assumes that both the
DP_PanelManager and DP_WDDX libraries are loaded):

        // Create (and start!) the pool
myRequestPool = new DP_RequestPool().startInterval(100);

        // Caller Function - this is called on the "change" (or whatever) of
the field you want to trigger the call.
function getMinSale(rowNum) {
                // Your URL
        var tempURL =
'http://127.0.0.1/cfc/calculators.cfc?method=minPriceCalc&productgroupid=2&c
ostprice=2';
                // Create your Request
        var myRequest = new DP_Request(
                                        "GET",
                                        tempURL,
                                        null,
                                        updateMinSale,
                                        [rowNum]);
                // Add the request to the queue (without this bit nothing is
actually run!)
        myRequestPool.addRequest(myRequest);
};

        // Handler Function - this is what gets called when the request is
done working.
function updateMinSale(result, rowNum) {
                // Extract the WDDX data
        var myResult = dpWDDX("deserialize", result);
                // Update the Field
        document.getElementById("min" + rowNum).value = myResult;
};

I know that this stuff can be a mental leap the first few times through
(that's of the reasons I created the Pooler in the first place - as complex
as it is WITH it it's 10 times more so without it!)  But after a while -
especially after the idea of asynchronous calls sinks in - it does get
smoother.  I promise!  ;^)

Hope this helps.

Jim Davis


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;160198600;22374440;w

Archive: 
http://www.houseoffusion.com/groups/CF-Community/message.cfm/messageid:248490
Subscription: http://www.houseoffusion.com/groups/CF-Community/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.5

Reply via email to