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

Commit 8a0b2d5b by sdepold

renamed __definition to __factory

1 parent 0ce1a625
...@@ -2,7 +2,7 @@ var Utils = require('./../utils') ...@@ -2,7 +2,7 @@ var Utils = require('./../utils')
module.exports = (function() { module.exports = (function() {
var HasManyDoubleLinked = function(definition, instance) { var HasManyDoubleLinked = function(definition, instance) {
this.__definition = definition this.__factory = definition
this.instance = instance this.instance = instance
} }
...@@ -11,18 +11,18 @@ module.exports = (function() { ...@@ -11,18 +11,18 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {} var where = {}
where[self.__definition.identifier] = self.instance.id where[self.__factory.identifier] = self.instance.id
var primaryKeys = Utils._.keys(self.__definition.connectorModel.rawAttributes) var primaryKeys = Utils._.keys(self.__factory.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__definition.identifier })[0] , foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0]
self.__definition.connectorModel.findAll({where: where}).success(function(associatedObjects) { self.__factory.connectorModel.findAll({where: where}).success(function(associatedObjects) {
var ids = associatedObjects.map(function(obj) { return obj[foreignKey] }) var ids = associatedObjects.map(function(obj) { return obj[foreignKey] })
if (ids.length == 0) { if (ids.length == 0) {
customEventEmitter.emit('success', []) customEventEmitter.emit('success', [])
} else { } else {
self.__definition.target.findAll({where: 'id in (' + ids.join(", ") + ')'}) self.__factory.target.findAll({where: 'id in (' + ids.join(", ") + ')'})
.success(function(objects) { customEventEmitter.emit('success', objects) }) .success(function(objects) { customEventEmitter.emit('success', objects) })
.error(function(err){ customEventEmitter.emit('failure', err) }) .error(function(err){ customEventEmitter.emit('failure', err) })
} }
...@@ -39,16 +39,16 @@ module.exports = (function() { ...@@ -39,16 +39,16 @@ module.exports = (function() {
.error(function(err) { emitter.emit('failure', err) }) .error(function(err) { emitter.emit('failure', err) })
.success(function() { .success(function() {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
, association = self.__definition.target.associations[self.__definition.associationAccessor] , association = self.__factory.target.associations[self.__factory.associationAccessor]
, foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier , foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier
, unassociatedObjects = newAssociations.filter(function(obj) { return !obj.equalsOneOf(oldAssociations) }) , unassociatedObjects = newAssociations.filter(function(obj) { return !obj.equalsOneOf(oldAssociations) })
unassociatedObjects.forEach(function(unassociatedObject) { unassociatedObjects.forEach(function(unassociatedObject) {
var attributes = {} var attributes = {}
attributes[self.__definition.identifier] = self.instance.id attributes[self.__factory.identifier] = self.instance.id
attributes[foreignIdentifier] = unassociatedObject.id attributes[foreignIdentifier] = unassociatedObject.id
chainer.add(self.__definition.connectorModel.create(attributes)) chainer.add(self.__factory.connectorModel.create(attributes))
}) })
chainer chainer
...@@ -65,7 +65,7 @@ module.exports = (function() { ...@@ -65,7 +65,7 @@ module.exports = (function() {
var emitter = new Utils.CustomEventEmitter(function() { var emitter = new Utils.CustomEventEmitter(function() {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
var foreignIdentifier = self.__definition.target.associations[self.__definition.associationAccessor].identifier var foreignIdentifier = self.__factory.target.associations[self.__factory.associationAccessor].identifier
var obsoleteAssociations = oldAssociations.filter(function(obj) { return !obj.equalsOneOf(newAssociations) }) var obsoleteAssociations = oldAssociations.filter(function(obj) { return !obj.equalsOneOf(newAssociations) })
if(obsoleteAssociations.length == 0) if(obsoleteAssociations.length == 0)
...@@ -73,13 +73,13 @@ module.exports = (function() { ...@@ -73,13 +73,13 @@ module.exports = (function() {
obsoleteAssociations.forEach(function(associatedObject) { obsoleteAssociations.forEach(function(associatedObject) {
var where = {} var where = {}
, primaryKeys = Utils._.keys(self.__definition.connectorModel.rawAttributes) , primaryKeys = Utils._.keys(self.__factory.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__definition.identifier })[0] , foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0]
where[self.__definition.identifier] = self.instance.id where[self.__factory.identifier] = self.instance.id
where[foreignKey] = associatedObject.id where[foreignKey] = associatedObject.id
self.__definition.connectorModel.find({where: where}).success(function(connector) { self.__factory.connectorModel.find({where: where}).success(function(connector) {
chainer.add(connector.destroy()) chainer.add(connector.destroy())
if(chainer.emitters.length == obsoleteAssociations.length) { if(chainer.emitters.length == obsoleteAssociations.length) {
......
...@@ -2,15 +2,15 @@ var Utils = require('./../utils') ...@@ -2,15 +2,15 @@ var Utils = require('./../utils')
module.exports = (function() { module.exports = (function() {
var HasManySingleLinked = function(definition, instance) { var HasManySingleLinked = function(definition, instance) {
this.__definition = definition this.__factory = definition
this.instance = instance this.instance = instance
} }
HasManySingleLinked.prototype.injectGetter = function() { HasManySingleLinked.prototype.injectGetter = function() {
var where = {} var where = {}
where[this.__definition.identifier] = this.instance.id where[this.__factory.identifier] = this.instance.id
return this.__definition.target.findAll({where: where}) return this.__factory.target.findAll({where: where})
} }
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) { HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
...@@ -18,14 +18,14 @@ module.exports = (function() { ...@@ -18,14 +18,14 @@ module.exports = (function() {
// clear the old associations // clear the old associations
oldAssociations.forEach(function(associatedObject) { oldAssociations.forEach(function(associatedObject) {
associatedObject[self.__definition.identifier] = null associatedObject[self.__factory.identifier] = null
associatedObject.save() associatedObject.save()
}) })
// set the new one // set the new one
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
newAssociations.forEach(function(associatedObject) { newAssociations.forEach(function(associatedObject) {
associatedObject[self.__definition.identifier] = self.instance.id associatedObject[self.__factory.identifier] = self.instance.id
chainer.add(associatedObject.save()) chainer.add(associatedObject.save())
}) })
chainer chainer
......
...@@ -59,7 +59,7 @@ module.exports = (function() { ...@@ -59,7 +59,7 @@ module.exports = (function() {
// add the inserted row id to the instance // add the inserted row id to the instance
if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId'))) if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee[this.callee.__definition.autoIncrementField] = results.insertId this.callee[this.callee.__factory.autoIncrementField] = results.insertId
if (this.sql.indexOf('SELECT') == 0) { if (this.sql.indexOf('SELECT') == 0) {
// transform results into real model instances // transform results into real model instances
......
...@@ -53,7 +53,7 @@ module.exports = (function() { ...@@ -53,7 +53,7 @@ module.exports = (function() {
// add the inserted row id to the instance // add the inserted row id to the instance
if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && metaData.hasOwnProperty('lastID')) { if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && metaData.hasOwnProperty('lastID')) {
var autoIncrementField = this.callee.__definition.autoIncrementField var autoIncrementField = this.callee.__factory.autoIncrementField
this.callee[autoIncrementField] = metaData.lastID this.callee[autoIncrementField] = metaData.lastID
} }
......
...@@ -141,7 +141,7 @@ module.exports = (function() { ...@@ -141,7 +141,7 @@ module.exports = (function() {
, self = this , self = this
options = options || {} options = options || {}
instance.__definition = this instance.__factory = this
Utils._.map(this.attributes, function(definition, name) { Utils._.map(this.attributes, function(definition, name) {
if(typeof instance[name] == 'undefined') { if(typeof instance[name] == 'undefined') {
......
...@@ -7,7 +7,7 @@ module.exports = (function() { ...@@ -7,7 +7,7 @@ module.exports = (function() {
var Model = function(values, options) { var Model = function(values, options) {
var self = this var self = this
this.__definition = null // will be set in Model.build 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.__options = Utils._.extend({ this.__options = Utils._.extend({
...@@ -22,7 +22,7 @@ module.exports = (function() { ...@@ -22,7 +22,7 @@ module.exports = (function() {
Utils._.extend(Model.prototype, Mixin.prototype) Utils._.extend(Model.prototype, Mixin.prototype)
Object.defineProperty(Model.prototype, 'sequelize', { Object.defineProperty(Model.prototype, 'sequelize', {
get: function(){ return this.__definition.modelManager.sequelize } get: function(){ return this.__factory.modelManager.sequelize }
}) })
Object.defineProperty(Model.prototype, 'QueryInterface', { Object.defineProperty(Model.prototype, 'QueryInterface', {
...@@ -40,10 +40,10 @@ module.exports = (function() { ...@@ -40,10 +40,10 @@ module.exports = (function() {
this[updatedAtAttr] = new Date() this[updatedAtAttr] = new Date()
if(this.isNewRecord) { if(this.isNewRecord) {
return this.QueryInterface.insert(this, this.__definition.tableName, this.values) return this.QueryInterface.insert(this, this.__factory.tableName, this.values)
} else { } else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
, tableName = this.__definition.tableName , tableName = this.__factory.tableName
, query = this.QueryInterface.update(this, tableName, this.values, identifier) , query = this.QueryInterface.update(this, tableName, this.values, identifier)
return query return query
...@@ -119,7 +119,7 @@ module.exports = (function() { ...@@ -119,7 +119,7 @@ module.exports = (function() {
Model.prototype.updateAttributes = function(updates) { Model.prototype.updateAttributes = function(updates) {
var self = this var self = this
var readOnlyAttributes = Utils._.keys(this.__definition.primaryKeys) var readOnlyAttributes = Utils._.keys(this.__factory.primaryKeys)
readOnlyAttributes.push('id') readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt') readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt') readOnlyAttributes.push('updatedAt')
...@@ -143,16 +143,16 @@ module.exports = (function() { ...@@ -143,16 +143,16 @@ module.exports = (function() {
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.QueryInterface.delete(this, this.__definition.tableName, identifier) return this.QueryInterface.delete(this, this.__factory.tableName, identifier)
} }
} }
Model.prototype.__defineGetter__("identifiers", function() { Model.prototype.__defineGetter__("identifiers", function() {
var primaryKeys = Utils._.keys(this.__definition.primaryKeys) var primaryKeys = Utils._.keys(this.__factory.primaryKeys)
, result = {} , result = {}
, self = this , self = this
if(!this.__definition.hasPrimaryKeys) if(!this.__factory.hasPrimaryKeys)
primaryKeys = ['id'] primaryKeys = ['id']
primaryKeys.forEach(function(identifier) { primaryKeys.forEach(function(identifier) {
...@@ -184,7 +184,7 @@ module.exports = (function() { ...@@ -184,7 +184,7 @@ module.exports = (function() {
var result = {} var result = {}
, self = this , self = this
Utils._.each(this.__definition.primaryKeys, function(_, attr) { Utils._.each(this.__factory.primaryKeys, function(_, attr) {
result[attr] = self[attr] result[attr] = self[attr]
}) })
...@@ -248,7 +248,7 @@ module.exports = (function() { ...@@ -248,7 +248,7 @@ module.exports = (function() {
var query = function() { var 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.__factory.modelManager.sequelize
args.push(this) args.push(this)
return s.query.apply(s, args) return s.query.apply(s, args)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!