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

Commit 7673b2fa by Sascha Gehlich

Added HasMany#injectCreator and Model.createAssociatedModel()

1 parent e1031475
......@@ -4,7 +4,7 @@ var Utils = require("./../utils")
, _ = require('lodash')
var HasManySingleLinked = require("./has-many-single-linked")
, HasManyMultiLinked = require("./has-many-double-linked")
, HasManyDoubleLinked = require("./has-many-double-linked")
module.exports = (function() {
var HasMany = function(source, target, options) {
......@@ -115,6 +115,7 @@ module.exports = (function() {
get: Utils._.camelize('get_' + as),
set: Utils._.camelize('set_' + as),
add: Utils._.camelize(Utils.singularize('add_' + as, this.target.options.language)),
create: Utils._.camelize(Utils.singularize('create_' + as, this.target.options.language)),
remove: Utils._.camelize(Utils.singularize('remove_' + as, this.target.options.language)),
hasSingle: Utils._.camelize(Utils.singularize('has_' + as, this.target.options.language)),
hasAll: Utils._.camelize('has_' + as)
......@@ -188,7 +189,7 @@ module.exports = (function() {
var self = this
obj[this.accessors.get] = function(options) {
var Class = Object(self.through) === self.through ? HasManyMultiLinked : HasManySingleLinked
var Class = Object(self.through) === self.through ? HasManyDoubleLinked : HasManySingleLinked
return new Class(self, this).injectGetter(options)
}
......@@ -246,7 +247,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]()
.success(function(oldAssociatedObjects) {
var Class = Object(self.through) === self.through ? HasManyMultiLinked : HasManySingleLinked
var Class = Object(self.through) === self.through ? HasManyDoubleLinked : HasManySingleLinked
new Class(self, instance).injectSetter(emitter, oldAssociatedObjects, newAssociatedObjects, defaultAttributes)
})
.proxy(emitter, {events: ['error', 'sql']})
......@@ -266,7 +267,7 @@ module.exports = (function() {
.proxy(emitter, {events: ['error', 'sql']})
.success(function(currentAssociatedObjects) {
if (currentAssociatedObjects.length === 0 || Object(self.through) === self.through) {
var Class = Object(self.through) === self.through ? HasManyMultiLinked : HasManySingleLinked
var Class = Object(self.through) === self.through ? HasManyDoubleLinked : HasManySingleLinked
new Class(self, instance).injectAdder(emitter, newAssociatedObject, additionalAttributes, !!currentAssociatedObjects.length)
} else {
emitter.emit('success', newAssociatedObject);
......@@ -323,6 +324,26 @@ module.exports = (function() {
return this
}
HasMany.prototype.injectCreator = function(obj) {
var self = this
obj[this.accessors.create] = function(values, fieldsOrOptions) {
var instance = this
return new Utils.CustomEventEmitter(function(emitter) {
// Create the related model instance
self.target
.create(values, fieldsOrOptions)
.proxy(emitter, { events: ['error', 'sql'] })
.success(function(newAssociatedObject) {
instance[self.accessors.add](newAssociatedObject)
.proxy(emitter)
})
}).run()
}
return this
};
/**
* The method checks if it is ok to delete the previously defined foreign key.
* This is done because we need to keep the foreign key if another association
......
......@@ -50,6 +50,7 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
association.injectGetter(this.DAO.prototype)
association.injectSetter(this.DAO.prototype)
association.injectCreator(this.DAO.prototype)
return this
}
......
......@@ -295,6 +295,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
describe('createAssociations', function() {
it('creates a new associated object', function(done) {
var Article = this.sequelize.define('Article', { 'title': DataTypes.STRING })
, Label = this.sequelize.define('Label', { 'text': DataTypes.STRING })
Article.hasMany(Label)
Article.sync({ force: true }).success(function() {
Label.sync({ force: true }).success(function() {
Article.create({ title: 'foo' }).success(function(article) {
article.createLabel({ text: 'bar' }).success(function(label) {
Label
.findAll({ where: { ArticleId: article.id }})
.success(function(labels) {
expect(labels.length).to.equal(1)
done()
})
})
})
})
})
})
})
describe("getting assocations with options", function() {
beforeEach(function(done) {
var self = this
......@@ -620,6 +645,30 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
describe('createAssociations', function() {
it('creates a new associated object', function(done) {
var User = this.sequelize.define('User', { username: DataTypes.STRING })
, Task = this.sequelize.define('Task', { title: DataTypes.STRING })
User.hasMany(Task)
Task.hasMany(User)
User.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() {
Task.create({ title: 'task' }).success(function(task) {
task.createUser({ username: 'foo' }).success(function() {
task.getUsers().success(function(_users) {
expect(_users).to.have.length(1)
done()
})
})
})
})
})
})
})
describe('optimizations using bulk create, destroy and update', function () {
beforeEach(function (done) {
var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!