Ext4中使用模型代理加载数据,利用NorthWind数据库中的Customers表
Ext.onReady(function ()
{
Ext.regModel("Customers", {
fields: [
//定义模型字段
{ name: "CustomerID", type: "int" },
{ name: "CompanyName", type: "string" },
{ name: "ContactName", type: "string" },
{ name: "ContactTitle", type: "string" },
{ name: "Address", type: "string" },
{ name: "City", type: "string" },
{ name: "Region", type: "string" },
{ name: "PostalCode", type: "string" },
{ name: "Country", type: "string" },
{ name: "Phone", type: "string" },
{ name:"Fax", type:"string"}
],
//配置数据代理
proxy: {
type: "ajax",
url:"/AjaxHandler/getCustomer.ashx"
}
});
var customer = Ext.ModelManager.getModel("Customers");
//通过代理读取数据(其中"AROUT"对应的是表Customers中的CustomerID字段)
customer.load("AROUT", {
success:function(rec)
{
Ext.Msg.alert("提示", rec.get("CompanyName"));
}
});
});
getCustomer.ashx代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ext;
namespace Ext.AjaxHandler
{
/// <summary>
/// getCustomer 的摘要说明
/// </summary>
public class getCustomer : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string customerID=context.Request["id"];
string result = "{";
NorthwindEntities db = new NorthwindEntities();
var query = from c in db.Customers
where c.CustomerID == customerID
select c;
foreach (var c in query)
{
result += "CustomerID:\"" + c.CustomerID + "\",CompanyName:\"" + c.CompanyName + "\"";
}
result += "}";
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
效果如下: