林秀栋的技术博客

设计模式(二十三) 数据访问对象模式

//封装一个方法,统一对本地数据进行增删改查

//把数据存储到本地,因为每个模块存在同一个地方,可通过时间戳等字符串标识“分隔”。
var b = function(p, t){
	this.p = p;
	this.t = t || ‘|-|’;
}
b.prototype = {
	status: {
		success: 0,	//成功
		failure: 1,		//失败
		overflow: 2,	//溢出
		timeout: 3	//过期
	},
	storage = localStorage || window.localStorage;
	getKey: function(key){	//获取本地储存数据库数据真实字段
		return this.p + key;
	},
	set: function(key, value, callback, time){	//添加(修改)数据
		var status = this.status.success,
			key = this.getKey(key);
		try{
			//time = … 	//获取时间戳
		}catch(e){
			//time = …
		}
		try{
			this.storage.setItem( … );
		}catch(e){
			status = this.status.overflow;
		}
		callback && callback.call(this, status, key, value);
	},
	//……
}