Struct of functions [dispatch table]
Hello, all,
I am waiting for a customer to respond to a request, and thought I'd play around, for a bit, while waiting. (Sometimes, these waits can go for days.)
I was thinking about something I had learned in Javascript a long time ago, and wondered if I could do the same thing in CF: a dispatch table.
Here is an example of a JS dispatch table:
<a href="javascript:void(0);" alt="This is title one." id="title1" class="test">One</a>
<a href="javascript:void(0);" alt="This is title two." id="title2" class="test">One</a>
<a href="javascript:void(0);" alt="This is title three." id="title3" class="test">One</a>
<div id="testText"> </div>
<script> // Pretend I'm loading jQuery
$(".test").click(function(){
var divAlign = {
title1:function(){$("#testText").css('textAlign','left');},
title2:function(){$("#testText").css('textAlign','center');},
dflt:function(){$("#testText").css('textAlign','right');}
}
var alignIt = function(doWhat){
var doWhat = divAlign.hasOwnProperty(doWhat) ? doWhat : 'dflt' ;
divAlign[doWhat]();
};
alignIt($(this).attr('id'));
$("#testText").html($(this).attr('alt'));
});</script>
Click a link, change the text in the div to the value of the alt attribute, and align the text based upon which one is clicked. Slightly faster than a switch/case.
I'm trying to do the same in CFSCRIPT (but show alerts instead of changing the DOM), and I'm banging my head into a wall. I have essentially the same code in CFSCRIPT, but I get an error:
"Element doWhat is undefined in divAlign"
I've even tried setting a variable called "doWhat" prior to creating the divAlign struct, and same error message.
What am I missing, here?
V/r,
^ _ ^
