How to create global function on Coldfusion Flash Form page
How do I create a global function that I can call from my actionscript code in a Coldfusion Flash Forms page. Currently, all actionscript functionality on the page is linked to an event, for example, how do I create a GetCustomDate method that can be accessed by both cfsavecontents below. Currently, I have to do whatever functionality is required for GetCustomDate in each cfsavecontent definition.
For example:
<cfsavecontent variable="OnCustomDateClick">
var customdate = GetCustomDate();
//do other stuff
</cfsavecontent>
<cfsavecontent variable="OnNewDateClick">
var newdate = GetCustomDate();
//do other stuff
</cfsavecontent>
<cfinput onClick="#OnCustomDateClick#" type="text" name="customDate">
<cfinput onClick="#OnNewDateClick#" type="text" name="newDate">
The code may not be 100% accurate but you can get the jist of what I'm trying to accomplish. I really hope there is a way to do this.
--- UPDATE -------------------------------------------------------------------------------
Ok, so based on the comments so far, we are almost there except for one thing: Please excuse that the code is a little more comple开发者_JAVA百科x that the simple GetCustomDate example I gave previously. It was made simple to illustrate the problem.
Given the code below. When I try and call GetCurrency like so:
_global.GetCurrency('40346feb-feb9-11d8-995f-00c0df070000');
alert('Symbol After: ' + _global.GetMyResult());
The output is:
Symbol Before: $
Symbol After: Undefined
As you can see by the alert, the currencySymbol value ($) is set and accessible from within the GetCurrency method, but once it leaves that method, it's value is 'undefined'.
I'm not sure why this is happening. I have also tried returning the currencySymbol (this is commented out) variable from the GetCurrency method, but flash/actionscript doesn't seem to like the returning of values from the OnResult event.
How do I call this GetCurrency function so that once the function has completed, I have access to the result somehow??
<cfsavecontent variable="onLoad">
<!--- Init --->
_global.myResult;
//create connection
var connection:mx.remoting.Connection = mx.remoting.NetServices.createGatewayConnection("http://<cfoutput>#cgi.HTTP_HOST#</cfoutput>/flashservices/gateway/");
//declare service
var myService:mx.remoting.NetServiceProxy;
var serviceName:String = '';
var responseHandler = {};
responseHandler.onStatus = function( stat: Object ):Void
{
//if there is any error, show an alert
alert("Error while calling cfc:" + stat.description);
}
<!--- MyResult Get/Set --->
_global.SetMyResult = function(result)
{
_global.myResult = result;
alert('_global.myResult' + _global.myResult)
}
_global.GetMyResult = function()
{
return _global.myResult;
}
<!--- Get Service Name --->
_global.GetServiceName = function(serviceName)
{
var tServiceName:String = '';
var tWebRoot:String = '<cfoutput>#Replace(Mid(request.site.webroot,2,Len(request.site.webroot)-1),'/','.','ALL')#</cfoutput>';
<cfif request.site.webroot NEQ "">
tServiceName = tWebRoot + '.' + + serviceName;
</cfif>
<!--- SALT --->
tServiceName = 'pricelist.webroot.' + serviceName;
return tServiceName;
}
<!--- Get Currency --->
_global.GetCurrency = function(currencyId)
{
responseHandler.onResult = function( results: Object ):Void
{
var currencySymbol:String = '';
if (results.length == 0)
{
alert('No details found!');
}
else
{
currencySymbol = results.getItemAt(0).currencySymbol;
_global.SetMyResult(currencySymbol);
alert('Symbol Before: ' + _global.GetMyResult());
}
//return currencySymbol;
}
serviceName = _global.GetServiceName('components.currency');
myService = connection.getService(serviceName, responseHandler );
myService.getDetail(currencyId);
}
_global.GetCurrency('40346feb-feb9-11d8-995f-00c0df070000');
alert('Symbol After: ' + _global.GetMyResult());
</cfsavecontent>
Thanks
I'm not familiar with ActionScript at all, but after defining your function you can assign it to a different scope:
<cffunction name="GetCustomDate">
<!--- do stuff --->
</cffunction>
<cfset request.GetCustomDate = GetCustomDate>
So after doing that, your code would change as follows:
<cfsavecontent variable="OnCustomDateClick">
var customdate = request.GetCustomDate();
//do other stuff
</cfsavecontent>
<cfsavecontent variable="OnNewDateClick">
var newdate = request.GetCustomDate();
//do other stuff
</cfsavecontent>
Sorry if I didn't address your question appropriately - like I said, I am not familiar with ActionScript.
Ok, per your comment that will not work. Try defining your function like this:
_global.GetCustomDate = function() { /* do stuff here */ }
And calling it like this:
<cfsavecontent variable="OnCustomDateClick">
var customdate = _global.GetCustomDate();
//do other stuff
</cfsavecontent>
<cfsavecontent variable="OnNewDateClick">
var newdate = _global.GetCustomDate();
//do other stuff
</cfsavecontent>
Why not put your GetCustomDate()
actionscript function in a <cfformitem type="script">
before your cfsavecontents?
<cfformitem type="script">
function GetCustomDate():Date{
// do stuff
}
</cfformitem>
精彩评论