Ok now that you have a Application.cfm - great. But you can still run into problems.
 
cfinvoke creates a new object and then destroys it after the request is completed. With the way you are doing it, your component just lives for the request event. I would do it like this.
 
 But further than? In Application.cfm page:
 
<cfapplication name="ShoppingApplication" sessionmanagement="yes" />
 
// and, you setup your session something like:
<cfif not isDefined("cookie.userID")>
 <cfcookie name="userID" value="#createuuid()#"/>
</cfif>
 
cfif (not isDefined("session.user")) or (session.user.userid neq cookie.userID)><cfscript>
 cftrace(text="Init Session variables like user and MainViewController" ,category = "Execution Trace");
 session.user = createObject("component", "user");
 session.user.init(userid=cookie.userID);
 session.shoppingCart = createObject("component","cartModule");
 session.shoppingCart.init(); // If you do not place the init in the Component 'autorun' section (ie between the <component> tag and before any <cffunction tags
</cfscript></cfif>
// End Application.cfm
 
Assuming that is done:
 
When you invoke a cfc from the web with CfObject.cfc?method etc - it creates a new instance - and maps the Form vars to method arguments where applicable. Note that if this cartModule instance does not have ANY relation to the one in the session - it is a new one created in response to the post from the cfm page. This means that in your cartModule method that accepts the post - you have to dispatch further calls to the session.shoppingCart  instance of cartModule. I would do this like this:
 
<cfcomponent name="ShoppingCart"><cfscript>
    // Public Instance Vars   
   
    // Private Instance Vars
    shopingItems = ArrayNew(1);
       
    function addCart()
    {
        var newItem = StructNew();
        newItem.quantity = url.qty;
        newItem.id = url.id;
        arrayAppend(shopingItems,newItem)
    }
    function getCartItems()
    {
        return duplicate(shopingItems);
    }
</cfscript>
<cffunction name="shopingCartDispatch" access="remote" output="Yes">
 <cfargument name="mode" default="viewCart" />
 <cfscript>
    <cftrace text="Executing: " & #shoppingCart# & " from shopingCartDispatchmethod" category="Execution Flow" />
     <cfset evaluate("session.shoppingCart." & mode & '()') />  <!--- This is the key Bind to instance in Session.... --->
 </cfscript> 
 <cfinclude template="MainView.cfm" />
</cffunction>
 
<cffunction name="fiewCart" output='Yes" .......
 
Anyway hope that helps
 
Cheers
Hardy
-----Original Message-----
From: Paul Giesenhagen [mailto:[EMAIL PROTECTED]
Sent: 03 October 2003 17:10
To: [EMAIL PROTECTED]
Subject: [CFCDev] Just starting CFC's -- Session Question

I am just now starting my learning on CFC's and having issues with sessions .. it is quite frustrating.
 
I created a VERY simple little input for building an array populated with a struct and then sticking it into a session.
 
I am not getting any session information from the calling CFM page .. if I set the session inside of the CFC.
 
Can anyone see what I am doing wrong?
 
If I run cart.cfm?mode=addCart  (I see the session set in the CFC -- cfdump)
 
AND then Run cart.cfm?mode=viewCart it says cartData is undefined in SESSION.
 
<!--- Calling CFM page --->
<cfparam name="url.mode" default="">
 
<cfswitch _expression_="#url.mode#">
 <cfcase value="delCart">
  <cfset variables.foo = StructDelete(session, "cartData")>
 </cfcase>
 <cfcase value="addCart">
  <cfparam name="url.qty" default="1">
  <cfparam name="url.id" default="1">
  <!--- <cfobject type="component" name="addCart" component="test.components.cartModule"> --->
  <cfinvoke component="test.components.cartModule" method="addCart"/>
 </cfcase>
 <cfcase value="viewCart">
  <cfdump var="#session.cartData#">
 </cfcase>
</cfswitch>
 
 
<!--- cartModule.cfc --->
<cfcomponent displayname="cartModule" hint="manages  Items to the Cart">
 <cffunction name="addCart" output="true" displayname="Adds Cart Stuff" hint="Adds Cart Stuff">
  <cfif IsDefined("session.cartData")>
   <cfset cartLen = ArrayLen(cart) + 1>
   <cfset cart[cartLen] = structNew()>
   <cfset cart[cartLen].qty = url.qty>
   <cfset cart[cartLen].id = url.id>
  <cfelse>
   <cfset cart = arrayNew(1)>
   <cfset cartLen = ArrayLen(cart) + 1>
   <cfset cart[cartLen] = structNew()>
   <cfset cart[cartLen].qty = url.qty>
   <cfset cart[cartLen].id = url.id>
  </cfif>
  <cfset session.cartData = cart>
  <cfdump var="#session.cartData#">
 </cffunction> 
</cfcomponent>
 
 
Also: If I call this component via CFOBJECT the <CFDUMP in the function doesn't display anything -- If I use <CFINVOKE it displays the dump.
 
Thanks
Paul Giesenhagen
QuillDesign
 

Reply via email to