0

I'm struggling with a Coldfusion site I have to redesign:

On my Jquery Mobile / Coldfusion8 site users should be able to log in from several different pages (each with it's own login form - can't change this!)

When a user first enters the index.cfm page, I'm running this:

 <!--- SESSION handler --->
 <cfif Session.loggedIn EQ "No">    
      <CFPARAM NAME="Attributes.Datasource">
      <CFPARAM NAME="Attributes.AdditionalText" DEFAULT="">
      ...
      <CFSET aform = "#CGI.SCRIPT_NAME#">
 </cfif>

Which is part of the login routine and sets up all the parameters and variables. I'm getting no errors, so everything must be ok. The page includes a list with one item being:

 <li>#Attributes.AdditonalText"</li>

and a login form:

 <CFFORM ACTION="#aform#" NAME="LoginForm">

Also, when no session is defined, I'm redirecting to index.cfm, so all users will end up on this page and get their session.

Problem
When the user goes from index.cfm to the next page, Jquery Mobile adds this page to the DOM(!). But the same parameters/variables that were valid on index.cfm now produce errors (both being undefinded).

I don't understand this. Aren't Coldfusion params/variable accessible on a DOM-scale, so any stuff I'm adding dynamically through Ajax and which includes references to these params/variables will also work?

I'm kind of lost on what to do here. Coldfusion newbie, too.

If I have this text in my index.cfm

This is the #attributes.additionalText#

it works, but if I have the same snippet in the 2nd file, which gets appended to the DOM, the exact same snippet returns #attributes.additioanlText# being undefined...

Question:
Why is it that my variables and attributes are not accessible or get undefined on content I dynamically add to the DOM? How can I make them available on a Global Scope?

Thanks for some clues!

frequent
  • 27,643
  • 59
  • 181
  • 333
  • 3
    Coldfusion -> serverside, it and its variables have no knowledge of the DOM. If the variables are needed to build the page, they need to be present when the page is built. Setting them after the fact in a different request won't affect the DOM unless you return new html to add to the DOM. – Kevin B Apr 27 '12 at 14:54
  • Thanks. Clarifies things – frequent Apr 27 '12 at 15:17

2 Answers2

5
  1. ColdFusion knows nothing about the DOM so think of it more as simply returning content which then is loaded by the browser or used by javascript.

  2. Attributes is not a shared scope. To my knowledge it is not even an actually scope in CF but rather a convention (edit: actually is scope in customtags) so if you are moving between pages any attributes value set on page 1 is not going to be available on page 2. You could use a session scope for some of this but it actually looks like you just need to make sure this IF statement is hit on every request. The two primary ways to do this is using OnRequestStart, the other is to have all requests go through index.cfm with an action attribute on the url specifying what code to include, function to hit, etc.

Dan Roberts
  • 4,664
  • 3
  • 34
  • 43
  • Thanks for the info. Is this also true if I'm never really leaving page 1 and page 2 only gets attached to the end of page 1? Say I have a list with 20items on page 1, the last item uses a CFPARAM value. If I now add 20 more items and the last also has the CFPARAM, do I need to redeclare, when I never really leave page 1? – frequent Apr 27 '12 at 15:11
  • @frequent leaving the page has nothing to do with coldfusion, every request you send to coldfusion is treated as a single request, it has no knowledge of previous requests or what is currently "on the page". To coldfusion, every time you request something from the server, it is a new page. You can use scopes that are shared between requests though, such as `session` – Kevin B Apr 27 '12 at 15:21
  • @Dan Roberts - I found out what the problem was. The site I'm having to redo used a custom CF-tag name ... CF_index param1="some", param2="thing"> and inside index.cfm declared these as application.param1="some", application.param2="thing". So from my basic understanding these were neither session nor application variables, but variables only declared for index.cfm. Thus on my next page, they all went undeclared. Does this make sense more or less? I could post the snippets, if necessary. – frequent Apr 27 '12 at 16:27
1

Anything you need to add to the DOM you can do by using ColdFusion to write out the jQuery to populate whatever DOM objects you want when rendering the page. Alternately, you could create an Ajax service call to a ColdFusion CFC that is setup to accept incoming http calls directly and return whatever values you want in JSON.

Here's a simple introduction to how to take the second approach: Easy AJAX using ColdFusion, jQuery and CFCs

Sharondio
  • 2,605
  • 13
  • 16