CFAjaxproxy Bind Magic

Posted By : todd sharp Posted At : January 8, 2008 9:16 AM Posted In: Ajax, ColdFusion

6

I'm working on a small project with some of the ColdFusion 8 Ajax magic and on a whim decided to try using cfajaxproxy in a 'creative' manner.  Turns out it worked quite nicely. Here's my situation: In the past we would put our JavaScript calls inline on an HTML element (in an onClick event or some such) or we'd use the good old <a href="javascript:foo();"></a> method. The recent unobtrusive, cross-browser compatible trends have lead us to use our JavaScript framework to attach these events. For example in Spry we might have:

<script>
foo = function(){
alert('you clicked me!');
}
//attach the event to the fooDiv

Spry.Utils.addLoadListener(function(){
    Spry.Utils.addEventListener("fooDiv", "click", foo, false);
});
</script>

<div id="fooDiv" style="cursor: pointer;">click me</div>

So part of my project is to highlight the beauty, power and simplicity of the ColdFusion 8 Ajax functionality. With that in mind I thought of the cfajaxproxy tag which can be used to bind form elements. I decided to try using it with a div instead, and it turns out it worked. There is one minor catch - cfajaxproxy requires a simple value to be passed. Simply passing the id of the div won't work because CF evaluates that id to the HTML element - which is not a simple value. I got around that by passing the innerHTML value of the div like so:

<cfajaxproxy bind="javascript:foo({fooDiv.innerHTML@click})" />
<script>
foo = function(){
alert('you clicked me!');
}
</script>
<div id="fooDiv" style="cursor: pointer;">click me</div>

Comments (6)

Matt Williams's Gravatar This doesn't seem to attach the click event to the actual div. I played around with it a bit and simply putting a line break between "me" and the closing div breaks the binding.

I like the idea, but maybe there is a better way to pass a simple value than the innerHTML.

Matt Williams's Gravatar A quick try on passing the id seems to work:
bind="javascript:foo({fooDiv.id@click})"

todd sharp's Gravatar Sweet! Thanks for posting the update!

Chris Smith's Gravatar Im going to try this with ThickBox from a cfdiv...

BMSPACK's Gravatar Nice, I gonna try

pakbehl's Gravatar very nice, can't wait to try