/** 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);
BaseDAO.js
最新推荐文章于 2024-09-20 16:14:48 发布