Use callback to implement script entity classes and simulate Ajax

Source: Internet
Author: User

 

 

Here are some points. We use Asp.net to develop database applications. You often need to create entities such

Public class userentity
...{
Public String Username
...{
Get ...{
Return "";}
}
Public String useremail
...{
Get ...{
Return "";}
}
}

The following describes how to use scripts to simulate such entities.

First of all, we need to explain the concept of returning such entity scripts.

Userentity = function ()
...{
Return... {"username": "shenyi", "useremail": "shenyi@csdn.net "}
}

In this way, we only need to write

VaR scriptentity = new userentity ();
Scriptentity. Username;
Scriptentity. useremail;

It can be referenced, and the script entity has been built. Next we will retrieve the data from the server as follows:

Create An ASPX page. If getdata. aspx is called, write it in the code.

Public void page_load ()
...{
Int userid = int. parse (request. querystring ["uid"]. tostring ());
String SQL = "select * from users where userid =" + userid. tostring ();
Using (datatable dt = sqlhelper. executedataset (SQL). Tables [0]) // sqlhelper do not tell me what it is
...{
// Construct a character here to make a script variable equal to {"username": DT. Rows [0] ["username] ......}
// Such as response. Write ("userreturnvalue = {... // splice yourself here }");
Response. End ();
}
}

 

The following is how to establish a connection between the script and the ASPX page:

VaR userreturnvalue = "" // This is very important because it must be assigned a value on the server.

Comjs = function ()
...{

This. getentitying = function (UID, callback)
...{
VaR scriptobj = Document. createelement ("script ");
Scriptobj. type = "text/JavaScript ";
Scriptobj. src = "getdata. aspx? Uid = "+ uid
Document. getelementsbytagname ("head") [0]. appendchild (scriptobj );
 
 
Scriptobj. onreadystatechange = function ()
...{
If (scriptobj. readystate = "loaded ")
...{

Callback. endgetentity (userreturnvalue );
}
}

}
}

 

Using callback is the most important step. After the script is loaded, it will assign values to userreturnvalue. Therefore, we cannot directly obtain this object.

After the above script is written, we will start to obtain this object on the page.

VaR CJS = new comjs (); // initialize our script object
Function begingetentity ()
...{
This. endgetentity = function (entitydatalist)
...{
// Entitydatalist: this is the final object. You can directly use entitydatalist. username or entitydatalist. Field name in the table.
// Obtain the object of this object
// Note that business operations must be performed in this function. This is a callback. That is, execution starts only when the server obtains the data here.
Strictly speaking, data is obtained asynchronously. Similar to Ajax, but does not use XMLHTTP objects
}
}

VaR Bge = new begingetentity ();
CJS. getentity ("3", bgelist); // here 3 indicates that I want to retrieve data whose user ID is 3

 

Good/the whole method of building entities using scripts is well written. I hope it will be useful to everyone, especially beginners. In fact, to implement Ajax in scripts, not only XMLHTTP can implement very common scripts, but also

Please follow my personal website: www.shenyisyn.org

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.