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

Commit 696cc28e by Overlook Motel

Tidying code (no functional change)

1 parent 1fa99b9d
Showing with 17 additions and 20 deletions
...@@ -49,11 +49,14 @@ module.exports = (function() { ...@@ -49,11 +49,14 @@ module.exports = (function() {
*/ */
this.associationAccessor = this.as this.associationAccessor = this.as
if (!this.associationAccessor && (typeof this.through === "string" || Object(this.through) === this.through)) { if (!this.associationAccessor) {
this.associationAccessor = this.through.tableName || this.through if (typeof this.through === 'string') {
} this.associationAccessor = this.through
else if (!this.associationAccessor) { } else if (Object(this.through) === this.through) {
this.associationAccessor = this.combinedTableName this.associationAccessor = this.through.tableName
} else {
this.associationAccessor = this.combinedTableName
}
} }
/* /*
...@@ -69,15 +72,15 @@ module.exports = (function() { ...@@ -69,15 +72,15 @@ module.exports = (function() {
if (this.through) { if (this.through) {
_.each(this.target.associations, function (association, accessor) { _.each(this.target.associations, function (association, accessor) {
if (self.source === association.target) { if (self.source === association.target) {
var paired = false var paired
// If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't pair, but regular ones will) // If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't pair, but regular ones will)
if (self.through === true && accessor === self.associationAccessor) { if (self.through === true) {
paired = true paired = accessor === self.associationAccessor
} }
// If through is not default, determine pairing by through value (model/string) // If through is not default, determine pairing by through value (model/string)
if (self.through !== true && self.options.through === association.options.through) { else {
paired = true paired = self.options.through === association.options.through
} }
// If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh // If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh
if (paired) { if (paired) {
...@@ -90,13 +93,12 @@ module.exports = (function() { ...@@ -90,13 +93,12 @@ module.exports = (function() {
} }
}) })
} }
/* /*
* If we are double linked, and through is either default or a string, we create the through model and set it on both associations * If we are double linked, and through is either default or a string, we create the through model and set it on both associations
*/ */
if (this.doubleLinked) { if (this.doubleLinked && this.through === true) {
if (this.through === true) { this.through = this.combinedTableName
this.through = this.combinedTableName
}
} }
if (typeof this.through === "string") { if (typeof this.through === "string") {
...@@ -185,8 +187,7 @@ module.exports = (function() { ...@@ -185,8 +187,7 @@ module.exports = (function() {
}) })
// define a new model, which connects the models // define a new model, which connects the models
var combinedTableAttributes = {} var sourceKeyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type
, sourceKeyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type
, targetKeyType = this.target.rawAttributes[this.target.primaryKeyAttribute].type , targetKeyType = this.target.rawAttributes[this.target.primaryKeyAttribute].type
, sourceAttribute = { type: sourceKeyType } , sourceAttribute = { type: sourceKeyType }
, targetAttribute = { type: targetKeyType } , targetAttribute = { type: targetKeyType }
...@@ -206,15 +207,11 @@ module.exports = (function() { ...@@ -206,15 +207,11 @@ module.exports = (function() {
if (primaryKeyDeleted) { if (primaryKeyDeleted) {
targetAttribute.primaryKey = sourceAttribute.primaryKey = true targetAttribute.primaryKey = sourceAttribute.primaryKey = true
} else { } else {
var uniqueKey = [this.through.tableName, this.identifier, this.foreignIdentifier, 'unique'].join('_') var uniqueKey = [this.through.tableName, this.identifier, this.foreignIdentifier, 'unique'].join('_')
targetAttribute.unique = sourceAttribute.unique = uniqueKey targetAttribute.unique = sourceAttribute.unique = uniqueKey
} }
combinedTableAttributes[this.identifier] = sourceAttribute
combinedTableAttributes[this.foreignIdentifier] = targetAttribute
if (!this.through.rawAttributes[this.identifier]) { if (!this.through.rawAttributes[this.identifier]) {
this.through.rawAttributes[this.identifier] = { this.through.rawAttributes[this.identifier] = {
_autoGenerated: true _autoGenerated: true
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!