不要怂,就是干,撸起袖子干!

Commit ee908129 by sdepold

refactored __defineGetter__ to Object.defineProperty

1 parent 8a0b2d5b
Showing with 53 additions and 45 deletions
...@@ -7,9 +7,9 @@ module.exports = (function() { ...@@ -7,9 +7,9 @@ module.exports = (function() {
var Model = function(values, options) { var Model = function(values, options) {
var self = this var self = this
this.__factory = null // will be set in Model.build
this.attributes = [] this.attributes = []
this.validators = {} // holds validation settings for each attribute this.validators = {} // holds validation settings for each attribute
this.__factory = null // will be set in Model.build
this.__options = Utils._.extend({ this.__options = Utils._.extend({
underscored: false, underscored: false,
hasPrimaryKeys: false, hasPrimaryKeys: false,
...@@ -33,6 +33,58 @@ module.exports = (function() { ...@@ -33,6 +33,58 @@ module.exports = (function() {
get: function(){ return this.QueryInterface.QueryGenerator } get: function(){ return this.QueryInterface.QueryGenerator }
}) })
Object.defineProperty(Model.prototype, 'isDeleted', {
get: function() {
var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null
return result
}
})
Object.defineProperty(Model.prototype, 'values', {
get: function() {
var result = {}
, self = this
this.attributes.forEach(function(attr) {
result[attr] = self[attr]
})
return result
}
})
Object.defineProperty(Model.prototype, 'primaryKeyValues', {
get: function() {
var result = {}
, self = this
Utils._.each(this.__factory.primaryKeys, function(_, attr) {
result[attr] = self[attr]
})
return result
}
})
Object.defineProperty(Model.prototype, "identifiers", {
get: function() {
var primaryKeys = Utils._.keys(this.__factory.primaryKeys)
, result = {}
, self = this
if(!this.__factory.hasPrimaryKeys)
primaryKeys = ['id']
primaryKeys.forEach(function(identifier) {
result[identifier] = self[identifier]
})
return result
}
})
Model.prototype.save = function() { Model.prototype.save = function() {
var updatedAtAttr = this.__options.underscored ? 'updated_at' : 'updatedAt' var updatedAtAttr = this.__options.underscored ? 'updated_at' : 'updatedAt'
...@@ -147,50 +199,6 @@ module.exports = (function() { ...@@ -147,50 +199,6 @@ module.exports = (function() {
} }
} }
Model.prototype.__defineGetter__("identifiers", function() {
var primaryKeys = Utils._.keys(this.__factory.primaryKeys)
, result = {}
, self = this
if(!this.__factory.hasPrimaryKeys)
primaryKeys = ['id']
primaryKeys.forEach(function(identifier) {
result[identifier] = self[identifier]
})
return result
})
Model.prototype.__defineGetter__('isDeleted', function() {
var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null
return result
})
Model.prototype.__defineGetter__('values', function() {
var result = {}
, self = this
this.attributes.forEach(function(attr) {
result[attr] = self[attr]
})
return result
})
Model.prototype.__defineGetter__('primaryKeyValues', function() {
var result = {}
, self = this
Utils._.each(this.__factory.primaryKeys, function(_, attr) {
result[attr] = self[attr]
})
return result
})
Model.prototype.equals = function(other) { Model.prototype.equals = function(other) {
var result = true var result = true
, self = this , self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!