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

Commit c77c2bc3 by Jan Aagaard Meier

associations

1 parent f5393a25
...@@ -38,7 +38,7 @@ module.exports = (function() { ...@@ -38,7 +38,7 @@ module.exports = (function() {
var self = this var self = this
obj[this.accessors.get] = function() { obj[this.accessors.get] = function() {
var id = obj.id var id = this.id
, where = {} , where = {}
where[self.identifier] = id where[self.identifier] = id
......
...@@ -10,6 +10,11 @@ Mixin.hasOne = function(associatedDAO, options) { ...@@ -10,6 +10,11 @@ Mixin.hasOne = function(associatedDAO, options) {
// the id is in the foreign table // the id is in the foreign table
var association = new HasOne(this, associatedDAO, Utils._.extend((options||{}), this.options)) var association = new HasOne(this, associatedDAO, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
if (this.DAO) {
association.injectGetter(this.DAO.prototype);
association.injectSetter(this.DAO.prototype);
}
return this return this
} }
...@@ -17,6 +22,10 @@ Mixin.belongsTo = function(associatedDAO, options) { ...@@ -17,6 +22,10 @@ Mixin.belongsTo = function(associatedDAO, options) {
// the id is in this table // the id is in this table
var association = new BelongsTo(this, associatedDAO, Utils._.extend((options||{}), this.options)) var association = new BelongsTo(this, associatedDAO, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
if (this.DAO) {
association.injectGetter(this.DAO.prototype);
association.injectSetter(this.DAO.prototype);
}
return this return this
} }
...@@ -24,6 +33,11 @@ Mixin.hasMany = function(associatedDAO, options) { ...@@ -24,6 +33,11 @@ Mixin.hasMany = function(associatedDAO, options) {
// the id is in the foreign table or in a connecting table // the id is in the foreign table or in a connecting table
var association = new HasMany(this, associatedDAO, Utils._.extend((options||{}), this.options)) var association = new HasMany(this, associatedDAO, Utils._.extend((options||{}), this.options))
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
if (this.DAO) {
association.injectGetter(this.DAO.prototype);
association.injectSetter(this.DAO.prototype);
}
return this return this
} }
......
var Utils = require("./utils") var Utils = require("./utils")
, DAO = require("./dao") , DAO = require("./dao")
, DataTypes = require("./data-types") , DataTypes = require("./data-types")
, Util = require('util')
module.exports = (function() { module.exports = (function() {
var DAOFactory = function(name, attributes, options) { var DAOFactory = function(name, attributes, options) {
...@@ -25,6 +26,15 @@ module.exports = (function() { ...@@ -25,6 +26,15 @@ module.exports = (function() {
// extract validation // extract validation
this.validate = this.options.validate || {} this.validate = this.options.validate || {}
// DAO prototype
this.DAO = function(values, options) {
DAO.apply(this, arguments);
};
Util.inherits(this.DAO, DAO);
} }
Object.defineProperty(DAOFactory.prototype, 'attributes', { Object.defineProperty(DAOFactory.prototype, 'attributes', {
...@@ -188,7 +198,7 @@ module.exports = (function() { ...@@ -188,7 +198,7 @@ module.exports = (function() {
} }
DAOFactory.prototype.build = function(values, options) { DAOFactory.prototype.build = function(values, options) {
var instance = new DAO(values, Utils._.extend(this.options, { hasPrimaryKeys: this.hasPrimaryKeys, factory: this })) var instance = new this.DAO(values, Utils._.extend(this.options, { hasPrimaryKeys: this.hasPrimaryKeys, factory: this }))
, self = this , self = this
options = options || {} options = options || {}
...@@ -219,10 +229,10 @@ module.exports = (function() { ...@@ -219,10 +229,10 @@ module.exports = (function() {
}) })
Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct }) Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct })
Utils._.each(this.associations, function(association) { // Utils._.each(this.associations, function(association) {
association.injectGetter(instance) // association.injectGetter(instance)
association.injectSetter(instance) // association.injectSetter(instance)
}) // })
instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true
instance.selectedValues = values instance.selectedValues = values
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!