Solutions to several issues in html + ashx development _ jquery

Source: Internet
Author: User
Tags variable scope
Those who are dealing with html + ashx will surely find that although this mode is elegant, it will encounter some difficult issues during development. I am no exception. The following are some of my experiences in actual development. I hope to share with you some suggestions and better solutions! Question 1: Use the delegate dictionary instead of switch... case.

This problem was discovered when processing requests. You certainly do not want to build many handler in your project to process so many requests, so I thought of processing multiple requests in a handler, adding an action parameter to the ajax request, and processing or returning the corresponding data according to the action in the handler, no one is using if... else To determine the action, most people will think of using switch... case, I used a switch at the beginning, but gradually found that each case is not a code block and cannot provide an independent scope for the variables in it! In the words of Sun Wukong ".

I searched the internet and many people encountered this problem. One solution is to separate each process into a method in handler, which is clear, but the corresponding method should be called through reflection in the ProcessRequest method! I was not satisfied with this solution, so I thought of delegation, thought of dictionary, and changed the reflection call Method to the index delegate in the dictionary.

First, declare a Private Static delegate dictionary in handler:

Static Dictionary > Hs;
Then, use the static constructor in handler (the class of the general processing program) to initialize hs. More importantly, add the processing method to the static constructor:

The Code is as follows:


Static Handler ()
{
Hs = new Dictionary > ();
Hs. Add ("add", delegate ()
{
Int id = int. Parse (req ("id "));
String title = req ("title ");
Return "add ";
});
Hs. Add ("update", delegate ()
{
Int id = int. Parse (req ("id "));
String title = req ("title ");
Return "update ";
});
}


Finally, it is called in the ProcessRequest method:

The Code is as follows:


Context. Response. ContentType = "text/plain ";
HttpRequest req = context. Request;
String action = req ["action"]. ToLower ();
String result = hs [action] ();
Context. Response. Write (result );


This avoids the variable scope and Reflection Efficiency of switch... case. My idea for the req () method used above is to provide public things in a static way, such:

The Code is as follows:


Static string req (string key)
{
Return HttpContext. Current. Request [key];
}
Static string jss (object obj)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
Return JSS. Serialize (obj );
}


Question 2: permission issues.

You certainly do not want your data to be accessed after the user has not logged in or the login has expired. Assume that the login user uses Session ["user"] for storage. Of course, it is very easy to judge Session ["user"] in handler, but the problem is how to make the Session ["user" refer to null's user to access the specified page (this is the login page login.html ). Haha, will you think of using context. Response. Redirect ("login.html") to solve this problem! The callback source code is returned as the ajax request result!

As a matter of fact, we understand that there is a very simple method, that is, to return a specific value when Session ["user"] is null. Here we assume "unlogin ", then, after each ajax request is complete, determine whether the returned value is "unlogin ".

This method is simple and reliable, but stupid, troublesome, and not feasible. So I thought of jquery again. ajaxSuccess (), I was a little worried when I thought of it for unified processing, will jquery call the callback function of a specific request before calling the global callback function? I conducted a test with this question, and the result was as expected: Execute the callback of the specific request first and then execute the global callback! I couldn't do it, so I had to check the jquery source code ~. The success () method was found in the jquery-1.4.2.js without compression, so it is true that the order after the change is as follows:

The Code is as follows:


Function success (){
If (s. global ){
Trigger ("ajaxSuccess", [xhr, s]);
}
// If a local callback was specified, fire it and pass it the data
If (s. success & xhr. responseText! = "Unlogin "){
S. success. call (callbackContext, data, status, xhr );
}
}


The execution order is changed. Where can I write the code that can be redirected? Write each page once? No, no. This is not the style of writing a program. It is a feasible method to write it into a jquery file (at the bottom:

The Code is as follows:


$ (Document). ajaxSuccess (function (event, xhr, settings ){
If (xhr. responseText = "unlogin "){
Window. top. location. href = "/login.html ";
}
})


It is shown that not every page's ajaxrequest requires the user to log on, such as the login.html page. Therefore, you must exclude the pages that do not need to log on when determining:

The Code is as follows:


If (HttpContext. Current. Request. UrlReferrer. ToString (). ToLower (). IndexOf ("login.html") <0)
{
If (HttpContext. Current. Session ["user"] = null)
{
HttpContext. Current. Response. Write ("unlogin ");
HttpContext. Current. Response. End ();
}
}


Question 3: Data Template.

What is really needed and everything comes into being! Just before writing this essay, I saw a jquery. tmpl article in the garden! The production of tmpl solves this problem! I know that this method is not as powerful as tmpl, but tmpl has a problem that cannot be solved. In fact, there are two main problems in the template. 1. If the template is stored in js, it is hard to edit it, 2. Where should we store the template to facilitate the view during design! Tmpl stores the template in
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.