Created
July 23, 2012 17:11
-
-
Save eveiga/3164782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DAOFactory.prototype.get_or_create = function(params, defaults){ | |
var self = this; | |
return new Utils.CustomEventEmitter(function(emitter) { | |
self.find({ where: params }) | |
.success( | |
function(instance){ | |
if (instance === null) { | |
for (var attrname in defaults) { | |
params[attrname] = defaults[attrname]; | |
} | |
self.create(params).success( | |
function(instance) { | |
emitter.emit('success', instance) | |
} | |
) | |
} else { | |
emitter.emit('success', instance) | |
} | |
} | |
); | |
}).run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yup, this gist is incomplete :P
https://github.com/sdepold/sequelize/pull/225
This is the correct version!