12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- /**
- * desc: webapp模型基类
- * author: wangyang
- * date: 2015-04-11
- */
- define(['storage'], function(storage) {
- var BaseObject = function() {
- this.storagePrefix = '';
- };
- BaseObject.prototype = {
- set: function(key, value, persisted) {
- this[key] = value;
- if (typeof persisted !== 'boolean') {
- persisted = false;
- }
- if (persisted) {
- storage.setItem(this.storagePrefix + key, value);
- }
- },
- get: function(key, default_value) {
- if (typeof default_value == 'undefined') {
- default_value = null;
- }
- return this.hasOwnProperty(key) && this[key] !== null ? this[key] : default_value;
- },
- setCache: function(key, value) {
- var val = typeof(value) != 'undefined' ? value : (this.hasOwnProperty(key) ? this[key] : null);
- storage.setItem(this.storagePrefix + key, val);
- },
- getCache: function(key, cache_time, default_value) {
- if (typeof default_value == 'undefined') {
- default_value = null;
- }
- var cache = storage.getItem(this.storagePrefix + key, cache_time);
- return cache ? cache : default_value;
- },
- remove: function(key) {
- this[key] = null;
- storage.removeItem(this.storagePrefix + key);
- }
- };
- return BaseObject;
- });
|