Not yet rated

Problem

I want to create a JavaScript object from a CFC then using a onclick event, pass my arguments to the CFC. For example useful for a 'add to cart' button without submitting the page.

Solution

You can create your CFC and a JavaScript object using cfajaxproxy, jsclassname. JavaScript proxy class that will represent the CFC. You could then create a submit button with an onclick event, in this event using the proxy you then pass any arguments to the CFC remotely.

Detailed explanation

 In CF8 jsclassname does this for you by creating a JavaScript proxy class that will represent the CFC you want to call. How cool is that!

<cfajaxproxy cfc= cart" jsclassname= "cfccall" />

Next create a submit button with an onclick event, in this event  using the proxy  just created pass my arguments to
the CFC. To capture the return from the CFC this example uses a simple JavaScript alert box. For the purpose the example everything is in the onclick event, however this is nothing stopping you from
writing a JavaScript function with validation your data before its passed to the CFC.

<cfform name="myform">

<cfinput type="button" value="Add To Cart" name="addme"
onClick="alert((new
cfccall()).addFunction(prodId=#prodId#,qty=1))">

</cfform>

To make this example work you will need to make sure the component you create is set  to access="remote".

<cffunction name="addFunction" access="remote"
returnType="string" verifyclient="yes"> 

Hope this helps.

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes