BaseDAO.js

/**
object.OpenTextFile(filename[, iomode[, create[, format]]])
参数
object
必选项。object 应为 FileSystemObject 的名称。
filename
必选项。指明要打开文件的字符串表达式。
iomode
可选项。可以是三个常数之一:ForReading 、 ForWriting 或 ForAppending 。
create
可选项。Boolean 值,指明当指定的 filename 不存在时是否创建新文件。如果创建新文件则值为 True ,如果不创建则为 False 。如果忽略,则不创建新文件。
format
可选项。使用三态值中的一个来指明打开文件的格式。如果忽略,那么文件将以 ASCII 格式打开。
设置
iomode 参数可以是下列设置中的任一种:
常数 值 描述
ForReading 1 以只读方式打开文件。不能写这个文件。
ForWriting 2 以写方式打开文件
ForAppending 8 打开文件并从文件末尾开始写。

format 参数可以是下列设置中的任一种:
值 描述
TristateTrue 以 Unicode 格式打开文件。
TristateFalse 以 ASCII 格式打开文件。
TristateUseDefault 使用系统默认值打开文件。
*/ 
(function ($) {
	$.fn.BaseDAO = function(option) {
		option = $.extend({
			dataPath : $.fn.config.dataPath
		}, option);
		
		var self = this;
		var fso = {};
		/**
		 * 获得 config.js 中的dataPath值
		 */
		var dataPath = option.dataPath;
		
		$.extend(this, {
			/**
			 * 初始化
			 * @returns
			 */
			_init : function() {
				/**
				 * 创建 Active 读写文件
				 */
				fso = new ActiveXObject("Scripting.FileSystemObject");
			},
			/**
			 * 读取文件内容
			 * @param dataPath 文件地址
			 * @returns
			 */
			read : function(dataPath) {
				var file = undefined;
				var content = "";
				try {
					file = fso.OpenTextFile(dataPath, 1, true);
					while (!file.AtEndOfStream) {
						content += file.ReadLine();
					}
				} finally {
					if (file) {
						file.Close();
					}
				}
				/**
				 * 将JSON字符串转换为 JSON对象
				 */
				return eval('(' + content + ')'); 
			},
			/**
			 * 向文件写入内容
			 * @param dataPath 文件地址
			 * @param content 内容
			 * @returns
			 */
			write : function(dataPath, content) {
				var file = undefined;
				try {
					file = fso.OpenTextFile(dataPath, 2, true);
					/**
					 * 将JSON对象转换为 JSON字符串
					 */
					file.WriteLine(JSON.stringify(content));
					file.close();
				} finally {
					if (file) {
						file.Close();
					}
				}
			}
		});
		
		this._init();
		return this;
	};
})(jQuery);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值