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

Commit c77c2bc3 by Jan Aagaard Meier

associations

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