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

Commit ea5c4337 by Sascha Depold

refactored associations mixin

1 parent 9854b2f7
var Utils = require("./../utils")
/*
Defines Mixin for all models.
*/
var Associations = module.exports = {
classMethods: {
hasOne: function(associatedModel, options) {
// the id is in the foreign table
var HasOne = require('./has-one')
var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
},
belongsTo: function(associatedModel, options) {
// the id is in this table
var BelongsTo = require("./belongs-to")
var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
},
hasMany: function(associatedModel, options) {
// the id is in the foreign table or in a connecting table
var HasMany = require("./has-many")
var association = new HasMany(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
}
},
instanceMethods: {
}
/* Defines Mixin for all models. */
var Mixin = module.exports = function(){}
Mixin.hasOne = function(associatedModel, options) {
// the id is in the foreign table
var HasOne = require('./has-one')
var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
}
Mixin.belongsTo = function(associatedModel, options) {
// the id is in this table
var BelongsTo = require("./belongs-to")
var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
}
Mixin.hasMany = function(associatedModel, options) {
// the id is in the foreign table or in a connecting table
var HasMany = require("./has-many")
var association = new HasMany(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
}
/* example for instance methods:
Mixin.prototype.test = function() {
console.log('asd')
}
*/
......@@ -167,6 +167,15 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
// private
ModelDefinition.prototype.__query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize
// add this as the second argument
if(arguments.length == 1) args.push(this)
return s.query.apply(s, args)
}
ModelDefinition.prototype.__addOptionalClassMethods = function() {
var self = this
Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct })
......@@ -204,14 +213,4 @@ ModelDefinition.prototype.__findAutoIncrementField = function() {
})
}
ModelDefinition.prototype.__query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize
// add this as the second argument
if(arguments.length == 1) args.push(this)
return s.query.apply(s, args)
}
Utils._.map(require("./associations/mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct })
Utils._.extend(ModelDefinition.prototype, require("./associations/mixin"))
......@@ -172,4 +172,4 @@ Model.prototype.equalsOneOf = function(others) {
}
/* Add the instance methods to Model */
Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct})
Utils._.extend(Model.prototype, Mixin.prototype)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!