在Application.cfc中混淆函数语法

时间:2016-07-03 09:55:00

标签: cfml

我无法理解Application.cfc中的函数。 我知道function doSomething() {...}是用于函数声明的语法,但在Application.cfc中也是一个事件监听器。因此,应用程序服务器选择基于上下文。例如,Jquery使用.on('event')。 对于两个不同的目的使用相同的语法对我来说仍然有点混乱。有关如何澄清该主题的任何提示? 感谢

1 个答案:

答案 0 :(得分:1)

各种框架烘焙自己的活动;但是,与现代cfml服务器语言相关的标准事件如下:

public function     Executes
OnSessionStart:     At start of session
OnSessionEnd:       When the session expires
OnApplicationStart: When the application is initialized
OnApplicationEnd:   When the application expires
OnRequestStart:     Before the requested page executes
OnRequestEnd:       After the requested page executes
OnRequest(page):    Overrides the default request behavior 
OnServerStart:      On server startup
OnServerEnd:        On server shutdown  
OnError:            Handles Errors if not already caught  

在application.cfc中使用服务器事件的典型方式:

public void function OnApplicationStart() 
{   application.loginRequired="cart,checkout,account";        }

public void function OnSessionStart()
{    session.LoggedIn=false; /* default Logged in to false */ }

public void function OnRequestStart()
{   // if login requested, test it and set logged in true if good
    if( isDefined("Form.Usr") and StructKeyExists(Form,"psw")
       and TestLogin(Form) )
        Session.LoggedIn=true;
    // never leave password lingering longer than needed
    StructDelete(Form,"Psw",false);
    // restore saved form entries if applicable
    if(isDefined("Session.SavedForm))
    {   StructAppend(Form,Session.SavedForm,true);
        StructDelete(Form,"SavedForm");
    }
    // restore saved url arguments if applicable
    if(isDefined("Session.SavedURL))
    {   StructAppend(URL,Session.SavedURL,true);
        StructDelete(Form,"SavedURL");
    }
}
public string function OnRequest(page)
{   include("/header.cfm");

    // if login is required and not logged in, show login form
    if(listFindNoCase(application.LoginRequired,listLast(page,'/'))
       and not Session.LoggedIn)
    {   session.SavedForm=Form;
        session.SavedURL=URL;
        include("/login.cfm");
    }
    // otherwise if desired page exists, show it
    else if(fileexists(expandpath(page)))
    {   try{include(page);} catch(any er) 
        {   writeoutput("Error: " & er.message);
            writelog(text=serializeJSON(er),type="Error");  
        }
    }
    // otherwise, if page missing, show a nice default page
    else
        include("/404.cfm");

    include("/footer.cfm");
    return "";
 }

在Web浏览器中处理客户端事件:

<body onload="setTimeout(bodyOnLoad,100)">...
<script> 
 function bodyOnLoad() {
   document.getElementsByName('searchBox').focus()
 } 
<script>

JQuery版本:

<script>
    $(document).ready(function(){
       $('input[name=searchBox]').focus();
    });
</script>