• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Struct of functions [dispatch table]

LEGEND ,
Jan 17, 2019 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,

^ _ ^

Views

519

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 19, 2019 Jan 19, 2019

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 = do

...

Votes

Translate

Translate
LEGEND ,
Jan 18, 2019 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.

Votes

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
community guidelines
Community Expert ,
Jan 19, 2019 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>

Votes

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
community guidelines
LEGEND ,
Jan 22, 2019 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,

^ _ ^

Votes

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
community guidelines
LEGEND ,
Jan 22, 2019 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').

Votes

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
community guidelines
LEGEND ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

LATEST

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.)

Votes

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
community guidelines
Resources
Documentation