Assigning ColdFusion Client Variables Using Javascript

Can I assign scope variables using JavaScript? In particular, I am trying to assign a value after it returns from an AJAX call.

I understand that ColdFusion is running server-side and client-side JavaScript, but with AJAX distribution, I wonder if something like this is possible. Thanks.

+3
source share
5 answers
Peter Bauton was pretty much dead in his concept, but you asked how to write client variables, and he didn't test his code. Also, what you're trying to do will be called ClientFacade, so I wrote (and tested) ClientFacade CFC and the accompanying JavaScript. Please note that I use jQuery because I never leave anywhere without it.;)

ClientFacade.cfc:

<cfcomponent output="false"
  hint="acts as a remote facade for the client scope">

    <cffunction name="set" output="false" access="remote"
      returntype="boolean" hint="sets a value into the client scope">
        <cfargument name="name" type="string" required="true"/>
        <cfargument name="val" type="any" required="true"/>

        <!--- you should sanitize input here to prevent code injection --->

        <cfscript>
            try {
                client[arguments.name] = arguments.val;
                return(true);
            }catch (any e){
                return(false);
            }
        </cfscript>
    </cffunction>

    <cffunction name="get" output="false" access="remote" returntype="any"
      hint="gets a value from the client scope">
        <cfargument name="name" type="string" required="true"/>
        <cfargument name="defaultVal" type="any" required="false"
            default=""/>

        <!--- you should sanitize input here to prevent code injection --->

        <cfscript>
            if (structKeyExists(client, arguments.name)){
                return(client[arguments.name]);
            }else{
                if (len(trim(arguments.defaultVal)) eq 0){
                    return('');
                }else{
                    return(arguments.defaultVal);
                }
            }
        </cfscript>

    </cffunction>

</cfcomponent>

test.cfm:

<script type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js">
  </script>
foo:<input type="text" name="foo" id="foo"/>
<button id="setValue">Set Value</button>
<button id="alertValue">Alert Value</button>

<script type="text/javascript">
    $(document).ready(function(){

        //attach functionality to our alert button
        $("#alertValue").click(function(e){
            var clientVal = 'initialdata';
            $.getJSON(
                'clientFacade.cfc',
                {method:"get", returnFormat:"json", name:"foo"},
                function(d){
                    clientVal = d;
                    alert(clientVal);
                }
            );
            e.preventDefault();//prevent the button from doing anything else
        });

        //attach functionality to our set button
        $("#setValue").click(function(e){
            var success = false;
            var valu = $("#foo").val();
            $.getJSON(
                'clientFacade.cfc',
                {
                  method:"set",
                  returnFormat:"json",
                  name:"foo",
                  "val":valu
                },
                function(d){
                    success = eval(d);
                    if (!success){
                        alert('Was not able to set the client var :(');
                    }
                }
            );
            e.preventDefault();//prevent the button from doing anything else
        });
    });
</script>

I think everything you wanted. Let me know if I missed something.

+4
source

CF, / , . GET, , , AJAX. - - javascript:

var img = new Image(); img.src= " http://myserver.com/something.cfm?name=value&anothername=anothervalue";

GET 2 . , : ) DOM b) .

+2

CFAJAXPROXY ( CF8) CFC, var .

+1

jQuery, getJSON() - CF (.. access="remote") JS.

CF8 returnformat="json" .

( cfjson ( ) .)


( ) ...

<cffunction name="MyRemoteFunc" returntype="Struct" access="remote" returnformat="json">
    <cfargument name="Name"      type="String"/>
    <cfargument name="Birthday"  type="Date"/>

    <cfset Session.UserDetails.Name = Arguments.Name />
    <cfset Session.UserDetails.Age  = DateDiff('yyyy',Now(),Arguments.Birthday) />
    <cfset Session.UserDetails.Id   = RandRange(1,100) />

    <cfreturn Session.UserDetails />
</cffunction>


<script type="text/javascript">

    function setUserDetails(name,birthday)
    { 
        $j.getJSON
            ( 'http://mydomain/stuff/remote.cfc?method=MyRemoteFunc'
            , { name:arguments.name , birthday:arguments.birthday }
            , setUserDetailsResponse
            );
    }

    function setUserDetailsResponse( result )
    {
        alert( 'Hello '+result.name+', you are '+result.age' years old and your Id is '+result.id+'.' );
    }

</script>
0

Source: https://habr.com/ru/post/1703038/


All Articles