> -----Original Message-----
> From: James Smith [mailto:[EMAIL PROTECTED]
> Sent: Friday, December 14, 2007 8:13 AM
> To: CF-Community
> Subject: JS HTTP Request?
> 
> I need to use some complex calculations from a CFC to update a form
> field
> when a different field is changed.  This seems like a fairly simple
> matter
> of using...
> 
> onChange="getHttp();"

It's not going to be quite that simple, but I've a component which will make
it much simpler here:

http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_R
equestPool/Index.cfm

It's a completely abstracted JavaScript HTTP Request pool - runs in all
major browsers and doesn't require any server-side code.

On your page you instantiate the pool (at the top, onload(), wherever) and
start it's polling mechanism:

        // Instantiate the Pool
myRequestPool = new DP_RequestPool(4);
        // Start the Interval
myRequestPool.startInterval(100);

This checks for new requests every 100 ms.  (There's also a "stopInterval()"
as well which will stop all queued requests from being processed).

To make a request you create a "DP_Request" object and add it to the queue
using the addRequest() method.  At its simplest form (call a page and do
nothing) it looks like this:

var myRequest = new DP_Request("GET","http://www.mysite.com/Add.htm";)
myRequestPool.addRequest(myRequest);

Requests can be reused over and over, if you like, just by adding them to
the queue again.

That's not that useful however, so you can also pass parameters to the page
being called (as an object containing name=value pairs) and a handler (a
function to call when complete which will do something with the result).

In your case its simplest to think about this as two functions: one to make
the request, one to handle the response.

function getHTTP() {
        var myRequest = new DP_Request(
                        "GET",
                        "http://www.mysite.com/whatever.cfm";,
                        null,
                        handleHTTP);
        myRequestPool.addRequest(myRequest);
};

This function (called as in your example) creates a new request and puts it
on the queue.  The "null" in the request is where input parameters would go.
You can include an object here or create one inline using object literal
notation (ask if you need more information).

The "handleHTTP" (no parens) is the function that will be called with the
output from the call.  The first argument passed to ANY handler function is
the results of the HTTP call.  It might look like this:

function handleHTTP(results) {
        document.getElementById("myFormField").value = results;
};

This is really simple of course but all it's doing is dumping the results
into the field (change this to however you want to use the value).

That's it.

There are actually a few ways to streamline this but all of them need more
explanation that won't really add to the solution.  In short tho:

+) You could easily reuse the Request object.  This is a good idea if the
object really doesn't change (as in the example) but gets more dangerous if
the properties change.  Remember objects are accessed by reference so any
changes to the object will affect all of those in the queue waiting to be
serviced - this can be useful sometimes but only if you really "get" what's
going on.

+) The handler function, especially if it's really that simple, could be
built inline as an anonymous function (a "closure").  This could simply your
code a bit but that's about it.  For more complex scenarios this can be life
saver however if the handler code needs to change with the circumstances of
the call.

+) You don't really need function to do the call, you could do all that work
right in the "onchange" handler itself (create and add the request, or just
add it if you're using a pre-built request) but it could get messy.

I'd be happy to help with this further if you want to post a longer snippet
of the code you're working on.

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:248446
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