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

Commit 17128deb by Sascha Depold

better "scoping" for options attribute

1 parent dc1c1820
Showing with 32 additions and 33 deletions
var Utils = require("./utils") var Utils = require("./utils")
, Mixin = require("./associations/mixin") , Mixin = require("./associations/mixin")
, QueryGenerator = require("./query-generator") , QueryGenerator = require("./query-generator")
var Model = module.exports = function(values, options) { var Model = module.exports = function(values, options) {
var self = this var self = this
this.definition = null // will be set in Model.build this.definition = null // will be set in Model.build
this.attributes = [] this.attributes = []
this.options = options || {} this.__options = options || {}
// add all passed values to the model and store the attribute names in this.attributes // 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) }) Utils._.map(values, function(value, key) { self.addAttribute(key, value) })
// set id to null if not passed as value // set id to null if not passed as value
// a newly created model has no id // 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) { if(this.__options.timestamps) {
defaults[this.options.underscored ? 'created_at' : 'createdAt'] = new Date() defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.options.underscored ? 'updated_at' : 'updatedAt'] = new Date() defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.options.paranoid) if(this.__options.paranoid)
defaults[this.options.underscored ? 'deleted_at' : 'deletedAt'] = null defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null
} }
Utils._.map(defaults, function(value, attr) { Utils._.map(defaults, function(value, attr) {
...@@ -46,17 +46,17 @@ Model.prototype.addAttribute = function(attribute, value) { ...@@ -46,17 +46,17 @@ Model.prototype.addAttribute = function(attribute, value) {
Model.prototype.query = function() { Model.prototype.query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg }) var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.definition.modelManager.sequelize , s = this.definition.modelManager.sequelize
args.push(this) args.push(this)
return s.query.apply(s, args) return s.query.apply(s, args)
} }
Model.prototype.save = 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)) if(this.hasOwnProperty(attr))
this[attr] = new Date() this[attr] = new Date()
if(this.isNewRecord) { if(this.isNewRecord) {
var self = this var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() { var eventEmitter = new Utils.CustomEventEmitter(function() {
...@@ -69,7 +69,7 @@ Model.prototype.save = function() { ...@@ -69,7 +69,7 @@ Model.prototype.save = function() {
}) })
return eventEmitter.run() return eventEmitter.run()
} else { } 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)) return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, identifier))
} }
} }
...@@ -95,11 +95,11 @@ Model.prototype.updateAttributes = function(updates) { ...@@ -95,11 +95,11 @@ Model.prototype.updateAttributes = function(updates) {
} }
Model.prototype.destroy = function() { Model.prototype.destroy = function() {
if(this.options.timestamps && this.options.paranoid) { if(this.__options.timestamps && this.__options.paranoid) {
this[this.options.underscored ? 'deleted_at' : 'deletedAt'] = new Date() this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = new Date()
return this.save() return this.save()
} else { } 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)) return this.query(QueryGenerator.deleteQuery(this.definition.tableName, identifier))
} }
} }
...@@ -108,43 +108,43 @@ Model.prototype.__defineGetter__("identifiers", function() { ...@@ -108,43 +108,43 @@ Model.prototype.__defineGetter__("identifiers", function() {
var primaryKeys = Utils._.keys(this.definition.primaryKeys) var primaryKeys = Utils._.keys(this.definition.primaryKeys)
, result = {} , result = {}
, self = this , self = this
if(!this.definition.hasPrimaryKeys) if(!this.definition.hasPrimaryKeys)
primaryKeys = ['id'] primaryKeys = ['id']
primaryKeys.forEach(function(identifier) { primaryKeys.forEach(function(identifier) {
result[identifier] = self[identifier] result[identifier] = self[identifier]
}) })
return result return result
}) })
Model.prototype.__defineGetter__('isDeleted', function() { Model.prototype.__defineGetter__('isDeleted', function() {
var result = this.options.timestamps && this.options.paranoid var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.options.underscored ? 'deleted_at' : 'deletedAt'] != null result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null
return result return result
}) })
Model.prototype.__defineGetter__('values', function() { Model.prototype.__defineGetter__('values', function() {
var result = {} var result = {}
, self = this , self = this
this.attributes.forEach(function(attr) { this.attributes.forEach(function(attr) {
result[attr] = self[attr] result[attr] = self[attr]
}) })
return result return result
}) })
Model.prototype.__defineGetter__('primaryKeyValues', function() { Model.prototype.__defineGetter__('primaryKeyValues', function() {
var result = {} var result = {}
, self = this , self = this
Utils._.each(this.definition.primaryKeys, function(_, attr) { Utils._.each(this.definition.primaryKeys, function(_, attr) {
result[attr] = self[attr] result[attr] = self[attr]
}) })
return result return result
}) })
...@@ -155,18 +155,18 @@ Model.prototype.equals = function(other) { ...@@ -155,18 +155,18 @@ Model.prototype.equals = function(other) {
Utils._.each(this.values, function(value, key) { Utils._.each(this.values, function(value, key) {
result = result && (value == other[key]) result = result && (value == other[key])
}) })
return result return result
} }
Model.prototype.equalsOneOf = function(others) { Model.prototype.equalsOneOf = function(others) {
var result = false var result = false
, self = this , self = this
others.forEach(function(other) { result = result || self.equals(other) }) others.forEach(function(other) { result = result || self.equals(other) })
return result return result
} }
/* Add the instance methods to Model */ /* Add the instance methods to Model */
Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct}) Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct})
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!