Link to home
Start Free TrialLog in
Avatar of stracqan
stracqan

asked on

How do I pass binded data from a .cfm back to a Javascript callback handler?

Hello,

Right now I have a CFDIV that is binded to a radio button.  On the click it fires off a .cfm template that then refreshes the contents of the CFDIV.  This all works due to ColdFusion.Navigate function ( thanks _agx_).  What I would like to do is have several variables come back to the callback function after the CFDIV refreshes for use in another javascript function.

In other words, I need the .cfm page to get two values; a latitude and a longitude.  I would then like to use both of those variables in a javascript function that only fires after the CFDIV is refreshed via the ColdFusion.naviagte function.

Any ideas?

Thanks!!!!!
<script>
      function loadDiv(value) {
            // Change the contents of the CFDIV
            ColdFusion.navigate('testGet.cfm?method=getData&testVariable='+ value, 'testDiv');
      }
    function callbackHandler(result){
          // This is where i need the latitude and longitude returned back in order to fire some code for Google Maps
    }
</script>
<cfform>
      Check/uncheck the box .. <cfinput type="checkbox" name="testVariable" />
</cfform>

<cfdiv id="testDiv" />
<cfajaxproxy bind="javascript:loadDiv({testVariable@click})"
        onSuccess="callbackHandler">

TESTGET.CFM
---------------
<cfparam name="url.testVariable" default="0">
<div style="background-color: blue; color: white;">
<cfset latitude = '30'>
<cfset longitude = '-40'>
<cfoutput>
      Latitude #latitude#<br/>
      Longitude #longitude#
</cfoutput>            
</div>

Open in new window

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Check this:


TESTGET.CFM
---------------
<cfparam name="url.testVariable" default="0">
<div style="background-color: blue; color: white;">
<cfset latitude = '30'>
<cfset longitude = '-40'>
<cfoutput>
      Latitude: #latitude#<br/>
      Longitude: #longitude#
<script>
    // define values:
    var myResult = {latitude:#latitude#, longitude:#longitude# };

    // call CallBack:
    callbackHandler(myResult);

    // change your callBackHandler to this for testing:
    function callbackHandler(theResult){
         alert("Latitude: "+theResult.latitude+"\nLongitude: "+theResult.longitude);
    }
</script>
</cfoutput>            
</div>
Avatar of stracqan
stracqan

ASKER

First, thank you for your response.  That works, but I would like to have the callback function called from outside of the TESTGET.cfm page because right now that is how I have it set up and it is not working when I call the .cfm page

In other words, i have my javascript on one page that gets data from the .CFM page and binds it to a CFDIV.  Once the contents of the CFDIV refresh, I would like to have the callback function that is on the orginal page and not the TESTGET.cfm page fire off..

Thanks!
>>   window.onload = loadScript;

The more I think about it,  you could probably just call the js function at very _end_ of the CF page (when everything is loaded) instead of using window load.

<script>
     // finally invoke loadScript()
     loadScript();
</script>

If it works you wouldn't even need the callback function stuff.  I think that's what the previous comment is suggestion ..

SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, so after trying a combination of things, here are my issues:

1. When I try to see what data is being passed back to the callbackfunction by throwing an alert, it says 'undefined'

function callbackHnadler(myResult){
alert(myResult);
}

-- and this is called from

<cfajaxproxy bind="javascript:loadDiv({testVariable@click})" onSuccess="callbackHandler">

-- and in my .CFM page I define two variables: 'lat' and 'long' by using a simple <cfset tag

Therefore, how can I get a result passed back??

2. When I try the loadscript function at the end of the .CFM page it works quite well, but Google Maps is still failing on the following line:

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

I have the map_canvas div defined in the .CFM page and not the main page.

There must be something with the timing for Google Maps.... even when I try to call the function to load the maps on the main page with the Div on in the .CFM page it still throws an error... hmmm
It could be timing and maybe the google script itself too? It's using at least 1 "window." call which could be problematic since the cfdiv isn't really a window.  

So far I can't get it to work reliably.  The ajax stuff is tricky once you start working with external files. Truthfully you might want to try something else like a little jquery or
http://cfgooglemap.riaforge.org/.  See if it works any better.

Sorry I couldn't be of more help.
Thank you both!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

So I figured out a way to accomplish this task with a little trickery... I created a new div that houses the CFDIV and the map container.

I then load the map code inside the external .CFM page to initialize the map on the main page (thanks _agx_).

I then use CSS to float the map over the new div and over the CFDIV to make it look like the map is being populated inside of the CFDIV!

Thanks for your help!
Great! If you get a chance, could you share the final code? I know it'll help someone else trying to do the same thing :)
This may not be the best cross-browser way of donig this, but it does work in all of the most recent browsers.  

Thanks!
<!---style to position map 'over' CFDIV contents--->
<style type="text/css">
<!--
#OuterDiv #map_canvas {
	float: right;
	position: absolute;
	z-index: 1000;
	top: 50px;
	left: 480px;
}
#OuterDiv {
	position: relative;
}
-->
</style>




<!---divs--->
<div id="OuterDiv">
<cfdiv id="testDiv" >
<div id="map_canvas" style="width:200px; height:200px"></div> 
</div>


Call on Main page to get the CFDIV contents
--------------
<cfajaxproxy bind="javascript:loadDiv({SelectBusinessGrp@click},{Category.value})" onSuccess="callbackHandler">
                 
                  
<script>
function loadDiv(value,value2) {
// Change the contents of the CFDIV
ColdFusion.navigate('TestGet.cfm?method=getData&testVariable='+ value+'&testV2='+ value2, 'testDiv');
</script>



Code in the remote .CFM file to initiliaze the map that is located on the main page
--------------------------------------------
    <script type="text/javascript">   
	function initialize() {     
									var latlng = new google.maps.LatLng(40, -70);  
									
									var myOptions = {  						zoom: 15, 						center: latlng,       					mapTypeId: google.maps.MapTypeId.ROADMAP     
									};  
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}  
                                                                   </script> 

<script>
initialize();
</script>

Open in new window