⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajaxedpage.asp

📁 javasrcipt编写的Ajax的类库
💻 ASP
📖 第 1 页 / 共 2 页
字号:
<%
'**************************************************************************************************************
'* Michal Gabrukiewicz Copyright (C) 2007 									
'* For license refer to the license.txt    									
'**************************************************************************************************************

'**************************************************************************************************************

'' @CLASSTITLE:		AjaxedPage
'' @CREATOR:		Michal Gabrukiewicz - gabru at grafix.at
'' @CREATEDON:		2007-06-28 20:32
'' @CDESCRIPTION:	Represents a page which Provides the functionality to call server-side ASP procedures
''					directly from client-side. When using the class be sure the draw() method is the first
''					which is called before any response has been done.
''					init(), main() and callback() need to be implemented within the page. init() is always
''					called first and allows preperation before any content is written to the response e.g.
''					security checks and stuff which is necessary for main() and callback() should be placed
''					into the init(). After the init whether main() or callback() is called. They are never
''					called both within one page execution.
''					main() = common state for the page which shows the user's presentation
''					callback() = handles all client requests
''					callback() needs to be defined with an parameter which holds the actual action to perform.
''					so the signature should be sub callback(action)
''					- REFER TO demo.asp FOR A SAMPLE USAGE.
''					- Requires Prototype JavaScript library (available at prototypejs.org)
'' @VERSION:		0.1

'**************************************************************************************************************
class AjaxedPage

	'private members
	private status, jason, callbackFlagName, loadedSources, componentLocation, loadingText, connectionString
	
	'public members
	public loadPrototypeJS		''[bool] should protypeJS library be loaded. turn this off if you've done it manually. default = true
	public buffering			''[bool] turns the response.buffering on or off (no affect on callback). default = true
	public contentType			''[string] contenttype of the response. default = empty
	public debug				''[bool] turns debugging on of. default = false
	public DBConnection			''[bool] indicates if a database connection should be opened automatically for the page.
								''If yes then a connection with the configured connectionstring is established and can be used
								''within the init(), callback() and main() procedures. default = false
	
	'**********************************************************************************************************
	'* constructor 
	'**********************************************************************************************************
	public sub class_initialize()
		set lib.page = me
		set jason = new JSON
		jason.toResponse = true
		loadPrototypeJS = lib.init(AJAXED_LOADPROTOTYPEJS, true)
		status = -1
		buffering = lib.init(AJAXED_BUFFERING, true)
		callbackFlagName = "PageAjaxed"
		set loadedSources = server.createObject("scripting.dictionary")
		componentLocation = lib.init(AJAXED_LOCATION, "/ajaxed/")
		loadingText = lib.init(AJAXED_LOADINGTEXT, "loading...")
		connectionString = lib.init(AJAXED_CONNSTRING, empty)
		DBConnection = false
		debug = false
	end sub
	
	'**********************************************************************************************************
	'' @SDESCRIPTION: 	Draws the page. Must be the first call on a page
	'' @DESCRIPTION:	The lifecycle is the following after executing draw().
	''					1. setting the HTTP Headers (response)
	''					2. init()
	''					3. main() or callback(action) - action is trimmed to 255 chars (for security reasons)
	''					- dont forget to provide your HTML, HEAD and BODY tags. The page does not do this for you ;)
	'**********************************************************************************************************
	public sub draw()
		setHTTPHeader()
		if DBConnection then
			if isEmpty(connectionString) then lib.throwError("No connectionstring configured.")
			db.open(connectionString)
		end if
		init()
		if isCallback() then
			writeln("{ ""root"": {")
			status = 0
			callback(left(RF(callbackFlagName), 255))
			if status = 0 then write(" null ")
			if status = 1 then
				writeln(vbcrlf & "} }")
			else
				writeln(vbcrlf & "}")
			end if
		else
			if loadPrototypeJS then loadJSFile(loc("prototypejs/prototype.js"))
			loadJSFile(loc("class_ajaxedPage/ajaxed.js"))
			execJS(array(_
				"ajaxed.prototype.debug = " & lib.iif(debug, "true", "false") & ";",_
				"ajaxed.prototype.indicator.innerHTML = '" & loadingText & "';"_
			))
			main()
		end if
		if DBConnection then db.close()
	end sub
	
	'******************************************************************************************************************
	'' @SDESCRIPTION:	returns a value on callback()
	'' @DESCRIPTION:	those values are accessible on the javascript callback function defined in ajaxed.callback()
	'' @PARAM:			val [variant]: check JSON.toJSON() for more details
	'******************************************************************************************************************
	public sub return(val)
		if status < 0 then throwError("return() can only be called on a callback.")
		if status < 2 then
			status = 1
			response.clear()
			writeln("{ ""root"": ")
		end if
		jason.toJSON empty, val, true
		status = 3
	end sub
	
	'******************************************************************************************************************
	'' @SDESCRIPTION:	returns a named value on callback(). call this within the callback() sub
	'' @DESCRIPTION:	this method can be called more than once because the value will be named and therefore more 
	''					values can be returned. 
	'' @PARAM:			name [string]: name of the value (accessible within the javascript callback)
	'' @PARAM:			val [variant]: refer to JSON.toJSON() method for details
	'******************************************************************************************************************
	public sub returnValue(name, val)
		if status < 0 then throwError("returnValue() can only be called on a callback.")
		if status > 2 then throwError("returnValue() cannot be called after return() has been called.")
		if name = "" then throwError("returnValue() requires a name.")
		if status > 0 then writeln(",")
		jason.toJSON name, val, true
		status = 1
	end sub
	
	'******************************************************************************************************************
	'' @SDESCRIPTION:	Returns wheather the page was already sent to server or not.
	'' @DESCRIPTION:	It should be the same as the isPostback() from asp.net.
	'' @RETURN:			[bool] posted back (true) or not (false)
	'******************************************************************************************************************
	public function isPostback()
		isPostback = (request.form.count > 0)
	end function
	
	'******************************************************************************************************************
	'' @SDESCRIPTION:	writes a string to the output in the same line
	'' @DESCRIPTION:	just a wrapper to str.write
	'' @PARAM:			value [string]: output string
	'******************************************************************************************************************
	public function write(value)
		str.write(value)
	end function
	
	'******************************************************************************************************************
	'' @SDESCRIPTION:	writes a line to the output
	'' @DESCRIPTION:	just a wrapper to str.writeln
	'' @PARAM:			value [string]: output string
	'******************************************************************************************************************
	public function writeln(value)
		str.write(value)
	end function
	
	'***********************************************************************************************************
	'' @SDESCRIPTION:	executes a given javascript. input may be a string or an array. each field = a line
	'' @PARAM:			JSCode [string]. [array]: your javascript-code. e.g. window.location.reload()
	'***********************************************************************************************************
	public sub execJS(JSCode)
		writeln("<script>")
		if isArray(JSCode) then
			for i = 0 to uBound(JSCode)
				writeln(JSCode(i))
			next
		else
			writeln(JSCode)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -