Skip to main content
WolfShade
Legend
January 17, 2019
Answered

Struct of functions [dispatch table]

  • January 17, 2019
  • 1 reply
  • 648 views

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,

^ _ ^

    This topic has been closed for replies.
    Correct answer BKBK

    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>

    1 reply

    WolfShade
    WolfShadeAuthor
    Legend
    January 18, 2019

    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.

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    January 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 = doWhat; 

                        } 

                    var func=obj[thisAction]; 

                    func();

                    }; 

                alertIt('title1');  

               alertIt('title2');   

               alertIt('abcd'); 

       </cfscript>

    WolfShade
    WolfShadeAuthor
    Legend
    January 22, 2019

    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,

    ^ _ ^