Created
March 30, 2016 07:42
-
-
Save aliaksandr-master/68c6a1163e423c065e6e2a96bf236f65 to your computer and use it in GitHub Desktop.
BaseClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseModel { | |
constructor (data, options = {}) { | |
if (options.$parse == null || options.$parse) { | |
data = this.$parse(data, options); | |
} | |
this.$$data = _.cloneDeep(data); | |
_.extend(this, data); | |
} | |
get $uid () { | |
if (!this.$$uid) { | |
this.$$uid = Utils.uniqId(); | |
} | |
return this.$$uid; | |
} | |
set $uid (value) { | |
throw new Error('you can not set $uid from code'); | |
} | |
get $idPath () { | |
return 'id'; | |
} | |
set $idPath (value) { | |
throw new Error('you can not set $idPath from code'); | |
} | |
get $id () { | |
if (this.$$id != null) { | |
return this.$$id; | |
} | |
if (this.$idPath != null) { | |
const _id = _.get(this, this.$idPath, null); | |
if (_id != null) { | |
this.$$id = _id; | |
} | |
return _id; | |
} | |
return null; | |
} | |
set $id (value) { | |
if (this.$$id !== null) { | |
$log.error('re-set $id!'); | |
return; | |
} | |
if (value == null) { | |
return; | |
} | |
this.$$id = String(value); | |
} | |
get $isNew () { | |
return this.$id == null; | |
} | |
set $isNew (value) { | |
throw new Error('you can not set $isNew from code'); | |
} | |
//$eject (options) { | |
// return this.$id; | |
//} | |
//$evict ($population, options) { | |
// var that = this.$clone(options); | |
// | |
// _.each($population, (_1, path) => { | |
// var v = _.get(that, path, null); | |
// | |
// if (v instanceof BaseModel) { | |
// _.set(that, path, v.$eject(options)); | |
// | |
// if (v == null) { | |
// throw new Error('all models need to be saved'); | |
// } | |
// } | |
// }); | |
// | |
// return that; | |
//} | |
//$populate ($population, options) { | |
// var that = this.$clone(options); | |
// | |
// if (!options) { | |
// options = {}; | |
// } | |
// | |
// var resolvePromises = _.transform($population, (resolvePromises, resolver, path) => { | |
// var model = _.get(that, path, null); | |
// | |
// if (model == null) { | |
// return; | |
// } | |
// | |
// var resolvePromise = resolver.call(that, model, options).then((resolvedModel) => { | |
// _.set(that, path, resolvedModel); | |
// }); | |
// | |
// resolvePromises.push(resolvePromise); | |
// }, []); | |
// | |
// return $q.all(resolvePromises).then(() => { | |
// return that; | |
// }); | |
//} | |
$clone (options = {}) { | |
const Model = this.constructor; | |
const model = new Model(null, { parse: false }); | |
_.each(this, (v, k) => { | |
model[k] = _.cloneDeep(v); | |
}); | |
} | |
$diff (options = {}) { | |
const model = this.$serialize(options); | |
if (this.$$data == null) { | |
return _.cloneDeep(model); | |
} | |
return Utils.deepDiff(model, this.$$data); | |
} | |
$check (options = {}) { | |
return true; | |
} | |
$fromJSON (data, options = {}) { | |
return _.cloneDeep(data); | |
} | |
$toJSON (data, options = {}) { | |
return _.transform(data, (obj, value, key) => { | |
if (!options.$toJSONAllow$ && /^\$[^$]]/.test(key)) { | |
return; | |
} | |
if (!options.$toJSONAllow$$ && /^\$+/.test(key)) { | |
return; | |
} | |
if (!options.$toJSONAllowPrivate && /^_/.test(key)) { | |
return; | |
} | |
if (!options.$toJSONAllowUndefined && _.isUndefined(value)) { | |
return; | |
} | |
if (!options.$toJSONAllowNull && _.isNull(value)) { | |
return; | |
} | |
obj[key] = _.cloneDeep(value); | |
}, options.$toJSONArray ? [] : {}); | |
} | |
$toJSONString (data, options = {}) { | |
return JSON.stringify(data); | |
} | |
$fromJSONString (data, options = {}) { | |
if (!data) { | |
return {}; | |
} | |
if (_.isPlainObject(data)) { | |
return data; | |
} | |
try { | |
return JSON.parse(data); | |
} catch (err) { | |
$log.error(err.message); | |
return {}; | |
} | |
} | |
$parse (data, options = {}) { | |
data = this.$fromJSONString(data); | |
data = this.$fromJSON(data, options); | |
return data; | |
} | |
$serialize (options = {}) { | |
const data = this.$toJSON(this, options); | |
if (options.$toJSONString == null || options.$toJSONString) { | |
return this.$toJSONString(data); | |
} | |
return data; | |
} | |
//static $resolver (resolver, mainOptions) { | |
// var Model = this; | |
// | |
// return (data, options) => { | |
// options = _.extend({}, mainOptions, options); | |
// | |
// return resolver(data, options).then((data) => new Model(data, options)); | |
// }; | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment