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

Commit ea5c4337 by Sascha Depold

refactored associations mixin

1 parent 9854b2f7
var Utils = require("./../utils") var Utils = require("./../utils")
/* /* Defines Mixin for all models. */
Defines Mixin for all models. var Mixin = module.exports = function(){}
*/
var Associations = module.exports = { Mixin.hasOne = function(associatedModel, options) {
classMethods: { // the id is in the foreign table
hasOne: function(associatedModel, options) { var HasOne = require('./has-one')
// the id is in the foreign table var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options))
var HasOne = require('./has-one') this.associations[association.associationAccessor] = association.injectAttributes()
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
belongsTo: function(associatedModel, options) { var BelongsTo = require("./belongs-to")
// the id is in this table var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options))
var BelongsTo = require("./belongs-to") this.associations[association.associationAccessor] = association.injectAttributes()
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
hasMany: function(associatedModel, options) { var HasMany = require("./has-many")
// the id is in the foreign table or in a connecting table var association = new HasMany(this, associatedModel, Utils._.extend((options||{}), this.options))
var HasMany = require("./has-many") this.associations[association.associationAccessor] = association.injectAttributes()
var association = new HasMany(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes()
}
},
instanceMethods: {
}
} }
/* example for instance methods:
Mixin.prototype.test = function() {
console.log('asd')
}
*/
...@@ -167,6 +167,15 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() { ...@@ -167,6 +167,15 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
// private // 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() { ModelDefinition.prototype.__addOptionalClassMethods = function() {
var self = this var self = this
Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct }) Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct })
...@@ -204,14 +213,4 @@ ModelDefinition.prototype.__findAutoIncrementField = function() { ...@@ -204,14 +213,4 @@ ModelDefinition.prototype.__findAutoIncrementField = function() {
}) })
} }
ModelDefinition.prototype.__query = function() { Utils._.extend(ModelDefinition.prototype, require("./associations/mixin"))
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 })
...@@ -172,4 +172,4 @@ Model.prototype.equalsOneOf = function(others) { ...@@ -172,4 +172,4 @@ Model.prototype.equalsOneOf = function(others) {
} }
/* Add the instance methods to Model */ /* 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!