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

Commit b89572f0 by Sascha Depold

fixed null result for belongsTo

1 parent 49723bf5
......@@ -25,7 +25,8 @@ BelongsTo.prototype.injectGetter = function(obj) {
var self = this
obj[Utils._.camelize('get_' + this.associationName)] = function() {
return self.target.find(obj[self.identifier])
var id = obj[self.identifier]
return self.target.find(id)
}
return this
......@@ -35,7 +36,7 @@ BelongsTo.prototype.injectSetter = function(obj) {
var self = this
obj[Utils._.camelize('set_' + this.associationName)] = function(associatedObject) {
obj[self.identifier] = associatedObject.id
obj[self.identifier] = associatedObject ? associatedObject.id : null
return obj.save()
}
......
......@@ -40,7 +40,8 @@ ModelDefinition.prototype.addDefaultAttributes = 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)
}
......@@ -77,8 +78,7 @@ ModelDefinition.prototype.find = function(options) {
// options is not a hash but an id
if(typeof options == 'number')
options = {where: options}
else {
if (Utils.argsArePrimaryKeys(arguments, this.primaryKeys)) {
else if (Utils.argsArePrimaryKeys(arguments, this.primaryKeys)) {
var where = {}
, self = this
......@@ -88,7 +88,9 @@ ModelDefinition.prototype.find = function(options) {
})
options = {where: where}
}
} else if((options == null) || (options == undefined)) {
var NullEmitter = require("./null-emitter")
return new NullEmitter()
}
options.limit = 1
......
var Utils = require("./utils")
var NullEmitter = module.exports = function(delay) {
var self = this
delay = delay || 10
setTimeout(function() { self.emitNull() }, delay)
}
Utils.addEventEmitter(NullEmitter)
NullEmitter.prototype.emitNull = function() {
this.emit('success', null)
this.emit('failure', null)
}
\ No newline at end of file
var client = new (require("mysql").Client)()
var Utils = module.exports = {
_: (function() {
var _ = require("underscore");
......
......@@ -48,5 +48,31 @@ module.exports = {
})
})
})
},
'it should correctly delete associations': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) {
u.setTask(t).on('success', function() {
u.getTask().on('success', function(task) {
assert.eql(task.title, 'a task')
u.setTask(null).on('success', function() {
u.getTask().on('success', function(task) {
assert.isNull(task)
exit(function(){})
})
})
})
})
})
})
})
})
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!