Posts tagged "cf_tip"

Why Distinguish Between GETs and POSTs?

It’s nice that ColdFusion gives you the option of distinguishing between GET and POST requests by putting variables in two different scopes (the “url” and “form” scopes, or structs), but with the framework I’ve put together for ColdFusion applications, I don’t really need to know. In fact, I don’t want my code distinguishing between the two because it makes it less generic (meaning I’m forced to make requests in way or the other). The answer has been to include this little piece of code from my Application.cfc file:

[code]

[/code]

Now I reference everything in the “request” scope or struct, regardless of whether it was submitted through a form or a URL parameter which makes my code more generic and reusable.

Don’t Forget to Scope CFHTTP

I ran into a nasty MXNA 2.0 bug last week. As many of you noticed, we had a case
where one person’s posts were attributed to someone else. I was stumped for about
an hour as I went through lots of lines of code, and long spells of staring into
space and contemplating. Then it hit me that since this has only happened one time
in all the thousands of posts MXNA 2.0 has aggregated, it must be a concurrency
issue.

And it was. MXNA 2.0 uses cached instances of parser components, and in one of
those components was a CFHTTP tag that wasn’t scoped, or "VARed". Just
the right sequence of events caused the variable cfhttp.fileContent to be overwritten
with a string from someone else’s feed. It’s a one in a million shot, but it happened
once, and it would have happened again given enough time.

If you’re using CFHTTP in a component, and you’re using CF 7.0, your code should
look like this:

<cfset var foo = 0/>
<cfhttp result="foo".../>

If you’re using CF 6.x, it should look like this:

<cfset var cfhttp.fileContent = 0/>
<cfhttp .../>

Note: I owe Sean Corfield a big thanks for helping me track this down.

Partial Page Caching with ColdFusion

ColdFusion Jedi Master Ray Camden will be giving a Macrochat on partial page caching
with ColdFusion tomorrow (Wednesday) from 1:00 to 2:00 Eastern. Here’s
the official stuff:

Learn how to write a custom tag that allows for simple caching in ColdFusion MX.
Ray Camden will lead this discussion on creating a CF tag for partial page caching
of information. Ray is the Director of Development for Mindseye, Inc., a Team Macromedia
member, and Macromedia User Group manager.

What You Will Learn

  • Existing ColdFusion Caching (query caching and cfcache)
  • Persistant Scopes
  • The ScopeCache Custom Tag

If you’re interested, you
must register
!

Eliminate ColdFusion Whitespace Once and For All

Since I’m at Macworld this week, and consequently don’t have a lot of time to
put into my weblog, I’m going to be lazy, and reprint a comment that was sent to
me by Jon Alsbury. It was submitted in response to a post entitled Controlling
Whitespace in ColdFusion
. John writes:

The most effective (and easy to implement) technique for reducing whitespace in
CFMX generated pages I have discovered so far is to set up a simple servlet filter
to intercept the response in order to strip out whitespace before it is returned
to the client. The filer I’ve been using for this is called Trim Filter and can
be downloaded here:

http://www.servletsuite.com/servlets/trimflt.htm

Setup is easy: simply download trimflt.jar from the above URL, drop it into your
‘cfusionmx/lib’ directory. Add the following to ‘cfusionmx/wwwroot/WEB-INF/web.xml’:

<filter>
<filter-name>trimFilter</filter-name>
<filter-class>com.cj.trim.trimFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>trimFilter</filter-name>
<url-pattern>*.cfm</url-pattern>
</filter-mapping>

Submitting Flash Forms Without Refreshing

I’ve been working on a way to submit Blackstone Flash forms without refreshing the page, and I have it working quite well. The code lets you either submit data to the server without re-rendering the Flash, or submit the data to a different window (the CFFORM tag supports targets, but the target attribute doesn’t give you a way to submit to a different window).

I’ve only tested it with Firefox, but I’m pretty sure it will work on all modern browsers. It’s a bit too early to release the code just yet, but once Blackstone is live, I’ll release the code along with a tutorial.

What do you guys think of what you’ve seen of Flash forms so far?

Reinitializing an Application Using Blackstone Events

One of the most useful tags I’ve ever written is the init tag. Typically, it goes
in your Application.cfm file, and the body of the tag gets executed only the first
time it’s encountered, which means it’s the perfect place to put initialization
code. You can also force the body to execute again by passing a certain query string
parameter in with any request. This is very useful during development when you need
to flush cached components or other data.

As I started thinking more in Blackstone terms, however, I wondered what the Blackstone
equivalent on the init tag would be. Of course, the init tag still works in Blackstone
as does all the CFMX 6.1 code I’ve run so far, but I wanted to take advantage of
the new event handling
mechanisms available in Blackstone
. So here’s what I’ve come
up with so far.

Now, I handle application initialization using the onApplicationStart event. That
takes care of running the code only once (usually when the application receives
its first request since the server was last started). I then added the following
inside my onRequestStart event handler:

<!--- Re-initialize the application, if necessary --->
<cfif structKeyExists(url, "restart") and url.restart>
<!--- Invoke onApplicationStart event --->
</cfif>

Now, when I want to invoke the onApplicationStart event, I just append a restart=true
parameter to any request, and my application is reinitialized without me having
to restart the server.

Chopping Off the End of a List

I’m sure there are tons of these functions around, but I decided to write my own.
The listChop function chops a list down to the specified size. Use it like this:

<cfset myList = "a,b,c,d,e"/>
<!--- Chop this list down to 3 elements. --->
<cfset myList = listChop(myList, 3[, delimiter])/>

Here’s the function:

<cffunction name="listChop" returnType="string" output="no" >
<cfargument name="targetList" type="string" required="true"/>
<cfargument name="amountToKeep" type="numeric" required="true"/>
<cfargument name="delimiter" type="string" required="false" default=","/>
<cfset var listSize = listLen(arguments.targetList, arguments.delimiter)/>
<cfset var i = 0/>
<cfif arguments.amountToKeep lt 0 or listSize - arguments.amountToKeep le 0>
<cfreturn arguments.targetList/>
</cfif>
<cfloop from="#arguments.amountToKeep+1#" to="#listSize#" index="i">
<cfset arguments.targetList = listDeleteAt(arguments.targetList,
arguments.amountToKeep+1,
arguments.delimiter)/>
</cfloop>
<cfreturn arguments.targetList/>
</cffunction>

I’m using it in a pretty big application I’m writing, so let me know if you see
any bugs.

What If You Want To Round Down?

ColdFusion’s round() function rounds up. What if you want to round down? Use this:

<cffunction name="roundDown" output="no">
<cfargument name="target" type="numeric" required="true"/>
<cfreturn abs(round((arguments.target * -1)))/>
</cffunction>

Example:

round(1.1) = 1
roundDown(1.1) = 1

round(1.5) = 2
roundDown(1.5) = 1

round(1.6) = 2
roundDown(1.6) = 2

Addition:

Bill pointed out that the function above doesn’t work with negative numbers. This one does. Thanks, Bill!

<cffunction name="roundDown" output="no">
<cfargument name="target" type="numeric" required="true"/>
<cfreturn (round((arguments.target * -1))) * -1/>
</cffunction>

Example:

roundDown(-1.5) = -2