28,391
社区成员
发帖
与我相关
我的任务
分享
<%@ Language="JavaScript" codepage="65001" %><%
// 引导
function boot() {
var route = (Request.QueryString("r").Item || "").split("/");
var model = getModel(route), c = 0;
while(true) {
var op = (route[c++] || "List").toLowerCase();
if(!model[op]) return tojson({ err: "403 Object Not Found" });
if("function" != typeof model[op]) { model = model[op]; continue; }
var rs = model[op]();
return rs instanceof Object ? tojson(rs) : rs;
}
}
// 模块定义
function getModel(route) {
// 展示 API 接口调试界面
var showApi = function(apipath) {
if(!apipath) apipath = "Api";
sys.apipath = "?r=" + (Request.QueryString("r").item || "").replace(/\w+$/, "") + apipath + "/";
// 可以从此处下载:http://boomstone.net/AspLib/views/apidoc.html
%><!-- #include virtual="/AspLib/views/apidoc.html" --><%
};
var root = {
// 分类模块
cata: {
api: {
Memo: [ "分类", "既可 ajax 调用,也可 通过 this.api 调用。" ],
ListDoc: [ "分类列表", "", "?r=Cata/Api/List" ],
list: function() { return { rows: new Array }; },
ShowDoc: [ "分类查看" ],
show: function() { return { title: "来自查看接口数据" }; },
SaveDoc: [ "分类保存" ],
save: function() { return { msg: "Save OK." }; },
DropDoc: [ "分类删除" ],
drop: function() { return { msg: "Drop OK." }; }
},
list: function() {
var rs = this.api.list();
return "此处 include 分类列表模板页面: " + tojson(rs);
},
// ?r=Cata/Show/5
show: function() {
var rs = this.api.show();
return "Cata Show ID[" + ~~route[2] + "]: " + tojson(rs);
},
// ?r=Cata/ApiDoc
apidoc: function() { return showApi.call(this.api); }
},
// 信息模块
info: {
api: {
list: function() { return { rows: new Array }; },
show: function() { return new Object; },
save: function() { return { msg: "Save OK." }; },
drop: function() { return { msg: "Drop OK." }; }
},
list: function() { return "Info List"; },
show: function() { return "Info Show"; },
apidoc: function() { return showApi.call(this.api); }
}
}
return root;
}
// 全局函数定义
function tojson(str) {
if(sys.JSON) return sys.JSON.stringify(str);
return getJson().stringify(str);
}
function fromjson(str) {
if(sys.JSON) return sys.JSON.parse(str);
return getJson().parse(str);
}
function getJson() {
var doc = new ActiveXObject("htmlfile");
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=edge' />");
return sys.JSON = doc.frames.JSON;
}
var sys = new Object;
Response.Write(boot());
%>