Query Based Routing Example

For small and simple procedures that are not expected to grow in complexity over time, simple routing based on URL parameter values from a [Pull] directly from the Request query is sufficient and requires less code. For complex procedures that are expected to grow and evolve over time, a larger more robust foundation for the routing should be used.


'Query Based Routing Example.mox

'Only use what you need; not more than you need. Use as an example; not a template in *most* cases
'ProcNameCSS and ProcNameJS should have ProcName replaced with a short name that is unique to this Procedure
'Start, Start.*, and End.NotFound are recommended names for the beginning of your routing
'Route.Thing.* and Action.DoIt.* are place holders for your own names that should be named after your module and/or objects
'Route., Action., and Util. prefixes should be used for your routes, actions and utilities
'Utility Methods/Functions should be prefixed after Util. with the ProcName so they don't conflict with utilities from includes
'Other object specific prefixes should be used for supporting Methods and Functions and they should be grouped together
'Complex pages that manage multiple objects/child-objects should be divided up into separate Public Methods
'Target single Procedure length is up to 1000 for initial deployment, expecting to grow up to 2000 over time
'Always first leverage the CMS for routing as much as possible; don't forget about Nav.Root.Type=Nav Records


Rem 'Init
    'TRON                   "Temp/QueryPlay.log"
    
    'SetKillCount           10000 'Only IF needed: some reasonable number 10x expected max-ever lines per request
    
    HttpsEnsure             'Any procedure that accesses non-public data needs this
    HttpNoCache             'Any procedure that accesses non-public data needs this
    
    'Include                "Example.Include1"
    'Include                "Example.Include2"
    
    HtmlBlock               "Head"
        <? ProcNameCSS ?>
        <? ProcNameJS ?>
    End HtmlBlock
    
    LangWith                "QueryPlay"
EndRem


Rem 'General Constants
    'If any
EndRem


Rem 'Routing Constants
    
    'Ajax
    Const cAjaxQuick1       = "Quick1"
    Const cAjaxQuick2       = "Quick2"
    
    'Routes
    Const cRouteThing1      = "Thing1"
    Const cRouteThing2      = "Thing2"
    
    'Actions
    Const cActionDoIt1      = "DoIt1"
    Const cActionDoIt2      = "DoIt2"
    
EndRem


Rem 'Entry Point
    Start                   "Request"
EndRem


Method Start(pQ)
    [Pull]                  (pQ), "Ajax Route Action"
    
    If  Ajax : AjaxReply    : Start.Ajax    pQ
    ElseIf  Route           : Start.Route   pQ
    ElseIf  Action          : Start.Action  pQ
    Else                    : Default.Get   pQ
    End If
End Method


Method Start.CSS(pQ)
    HtmlBlock               "ProcNameCSS", "Replace"
        <style>
            .customCSS      {color: red;}
        style>
    End HtmlBlock
End Method


Method Start.JS(pQ)
    HtmlBlock               "ProcNameJS", "Replace"
        <script>
            var customJS =  123;
        script>
    End HtmlBlock
End Method


Method Start.Ajax(pQ)
    [Pull]  (pQ), "Ajax"
    
    If      Ajax = cAjaxQuick1      : Ajax.Quick1       pQ
    ElseIf  Ajax = cAjaxQuick2      : Ajax.Quick2       pQ
    Else                            : HttpStatus        "404 Not Found"
    End If
End Method


Method Start.Route(pQ)
    Start.CSS               pQ
    Start.JS                pQ
    
    [Pull]  (pQ), "Route"
    
    If      Route = cRouteThing1    : Route.Thing.One   pQ
    ElseIf  Route = cRouteThing2    : Route.Thing.Two   pQ
    Else                            : End.NotFound      pQ
    End If
End Method


Method Start.Action(pQ)
    [Pull]  (pQ), "Action"
    
    If      Action = cActionDoIt1   : Action.DoIt.One   pQ
    ElseIf  Action = cActionDoIt2   : Action.DoIt.Two   pQ
    Else                            : End.NotFound      pQ
    End If
End Method


Method End.NotFound(pQ)
    HttpStatus              "404 Not Found"
    Html                    ((RecoverHtmlObj$ (Lookup$ Content.Category|Error-404|Intro)) & _
                            (RecoverHtmlObj$ (Lookup$ Content.Category|Error-404|Body)))
    Exit Proc
End Method


Method Default.Get(pQ)
    [Pull]  (pQ), "Option1 Option2"
    [New] Me [Me]

    Rem 'Get our data
        Util.ProcName.FilterQuery    (Me  & ".Output")
    EndRem
    
    Rem 'Apply options
        
    EndRem
    
    Rem 'Put it on the page
        
    EndRem
End Method


Method Route.Thing.One(pQ)
    [Pull]  (pQ), "Req.Path Option1 Option2"
    
    Rem 'Get our data
        
    EndRem
    
    Rem 'Apply options
        
    EndRem
    
    Rem 'Put it on the page
        HtmlBlock
            <div class="CustomPanelStuff">
                <? ThingOneForm ?>
            div>
        End HtmlBlock
        
        HtmlForm            "ThingOneForm", " Save", ("/" & Req.Path & "?Action=" & cActionDoIt1)
        HtmlHistory         ("/" & Req.Path & "?Route=" & cRouteThing1)
    EndRem
End Method


Method Route.Thing.Two(pQ)

End Method


Method Action.DoIt.One(pQ)
    [Pull]  (pQ), "Req.Path Option1 Option2"
    
    Rem 'Get our data
        
    EndRem
    
    Rem 'Apply options
        
    EndRem
    
    Rem 'Make changes to DB
        
    EndRem
    
    Rem 'Display a specific Route
        NewQuery            "DoIt.One.Reroute", "", "y"
            SetNew          "Route", ("`" & cRouteThing1)
            SetNew          "Option1", "`Test It"
            SetNew          "Option2", "`Another Test"
            Start           "DoIt.One.Reroute"
        WorkQuery           "Me.Output"
    EndRem
End Method


Method Action.DoIt.Two(pQ)

End Method


Method Util.ProcName.FilterQuery(pQ)

End Method


Function Util.ProcName.CalcIt(pOne, pTwo)
    
    Return rOne, rTwo
End Function


Macro Util.ProcName.mSaveMeTyping(pOne, pTwo, pThree)
    
End Macro