Highlighted

Struct of functions [dispatch table]

LEGEND ,
Jan 17, 2019

Copy link to clipboard

Copied

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,

^ _ ^

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

An idea:

<cfscript> 

            obj = { 

                title1:function(){writeOutput("<script>alert('Title One');</script>");}, 

                title2:function(){writeOutput("<script>alert('Title Two');</script>");}, 

                deflt:function(){writeOutput("<script>alert('Title Three');</script>");} 

                }; 

            alertIt = function(doWhat){ 

                var thisAction = "deflt"; 

                if(StructKeyExists(obj,doWhat)){ 

                    thisAction = doWhat; 

                    } 

                var func=obj[thisAction]; 

                func();

                }; 

            alertIt('title1');  

           alertIt('title2');   

           alertIt('abcd'); 

   </cfscript>

Views

322

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Struct of functions [dispatch table]

LEGEND ,
Jan 17, 2019

Copy link to clipboard

Copied

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,

^ _ ^

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

An idea:

<cfscript> 

            obj = { 

                title1:function(){writeOutput("<script>alert('Title One');</script>");}, 

                title2:function(){writeOutput("<script>alert('Title Two');</script>");}, 

                deflt:function(){writeOutput("<script>alert('Title Three');</script>");} 

                }; 

            alertIt = function(doWhat){ 

                var thisAction = "deflt"; 

                if(StructKeyExists(obj,doWhat)){ 

                    thisAction = doWhat; 

                    } 

                var func=obj[thisAction]; 

                func();

                }; 

            alertIt('title1');  

           alertIt('title2');   

           alertIt('abcd'); 

   </cfscript>

Views

323

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 17, 2019 0
LEGEND ,
Jan 18, 2019

Copy link to clipboard

Copied

Here is the code I have, so far, if it helps.  Error message is "Invalid CFML construct found on line 20 at column 37.  ColdFusion was looking at the following text: thisAction".

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>Untitled Document</title>
    </head>

    <body>
        <cfscript>

            obj = {

                title1:function(){writeOutput("<script>alert('Title One');</script>");},

                title2:function(){writeOutput("<script>alert('Title Two');</script>");},

                deflt:function(){writeOutput("<script>alert('Title Three');</script>");}

                };

            alertIt = function(doWhat){

                var thisAction = "deflt";

                if(StructKeyExists(obj,doWhat)){

                    thisAction = doWhat;

                    }

                obj[thisAction]();

                };

            alertIt('title1');

           alertIt('title2'); 

          alertIt('abcd');

        </cfscript>
    </body>
</html>

V/r,

^ _ ^

UPDATE:  I placed the code inside a try/catch block, and it's not triggering the catch.  It displays the same error message on the page, as if the catch were being ignored.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 18, 2019 0
Adobe Community Professional ,
Jan 19, 2019

Copy link to clipboard

Copied

An idea:

<cfscript> 

            obj = { 

                title1:function(){writeOutput("<script>alert('Title One');</script>");}, 

                title2:function(){writeOutput("<script>alert('Title Two');</script>");}, 

                deflt:function(){writeOutput("<script>alert('Title Three');</script>");} 

                }; 

            alertIt = function(doWhat){ 

                var thisAction = "deflt"; 

                if(StructKeyExists(obj,doWhat)){ 

                    thisAction = doWhat; 

                    } 

                var func=obj[thisAction]; 

                func();

                }; 

            alertIt('title1');  

           alertIt('title2');   

           alertIt('abcd'); 

   </cfscript>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 19, 2019 0
LEGEND ,
Jan 22, 2019

Copy link to clipboard

Copied

I'll give it a shot.  But I suspect that since the error message indicates that "thisAction" doesn't exist, it will still throw an error.

I'll report back once I've tried it.  Thanks.

V/r,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 22, 2019 0
LEGEND ,
Jan 22, 2019

Copy link to clipboard

Copied

Nope.  My assumption was totally incorrect.  Counter-intuitively (for me) your suggestion works.  I have no idea why.  If "thisAction" didn't exist for the way I originally presented my code, it should not exist for the declaration followed by execution, but it does.

Thank you, BKBK, for that suggestion.  My CF-flavoured dispatch table is now functional. 

V/r,

^ _ ^

UPDATE:  I forgot to mention that I also had to place the struct properties in string delineating apostrophe (ie, title1 became 'title1').

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 22, 2019 0
LEGEND ,
Mar 18, 2019

Copy link to clipboard

Copied

AND (dramatic pause), I just learned something else about my CF dispatch table.

You can add arguments.  So far I've only passed strings to it, but they go and can be accessed from the arguments scope (ie, arguments[1], arguments[2], etc.)  I wonder if I named the argument values, would I be able to access them via arguments['arg1'] and such?

I'm gonna go try it! 

V/r,

^ _ ^

UPDATE: YUP!  I named the arguments and was easily able to use bracket/name notation, and IT WORKED!  Woot, woot!  (I'm such a geek.)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 18, 2019 0