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

Commit 17128deb by Sascha Depold

better "scoping" for options attribute

1 parent dc1c1820
Showing with 14 additions and 14 deletions
......@@ -7,21 +7,21 @@ var Model = module.exports = function(values, options) {
this.definition = null // will be set in Model.build
this.attributes = []
this.options = options || {}
this.__options = options || {}
// add all passed values to the model and store the attribute names in this.attributes
Utils._.map(values, function(value, key) { self.addAttribute(key, value) })
// set id to null if not passed as value
// a newly created model has no id
var defaults = this.options.hasPrimaryKeys ? {} : { id: null }
var defaults = this.__options.hasPrimaryKeys ? {} : { id: null }
if(this.options.timestamps) {
defaults[this.options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.__options.timestamps) {
defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.options.paranoid)
defaults[this.options.underscored ? 'deleted_at' : 'deletedAt'] = null
if(this.__options.paranoid)
defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null
}
Utils._.map(defaults, function(value, attr) {
......@@ -52,7 +52,7 @@ Model.prototype.query = function() {
}
Model.prototype.save = function() {
var attr = this.options.underscored ? 'updated_at' : 'updatedAt'
var attr = this.__options.underscored ? 'updated_at' : 'updatedAt'
if(this.hasOwnProperty(attr))
this[attr] = new Date()
......@@ -69,7 +69,7 @@ Model.prototype.save = function() {
})
return eventEmitter.run()
} else {
var identifier = this.options.hasPrimaryKeys ? this.primaryKeyValues : this.id
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, identifier))
}
}
......@@ -95,11 +95,11 @@ Model.prototype.updateAttributes = function(updates) {
}
Model.prototype.destroy = function() {
if(this.options.timestamps && this.options.paranoid) {
this[this.options.underscored ? 'deleted_at' : 'deletedAt'] = new Date()
if(this.__options.timestamps && this.__options.paranoid) {
this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = new Date()
return this.save()
} else {
var identifier = this.options.hasPrimaryKeys ? this.primaryKeyValues : this.id
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(QueryGenerator.deleteQuery(this.definition.tableName, identifier))
}
}
......@@ -120,8 +120,8 @@ Model.prototype.__defineGetter__("identifiers", function() {
})
Model.prototype.__defineGetter__('isDeleted', function() {
var result = this.options.timestamps && this.options.paranoid
result = result && this[this.options.underscored ? 'deleted_at' : 'deletedAt'] != null
var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null
return result
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!