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

Commit d71a2e7b by Mick Hansen

feat(promises): refactor reload to use promise style

1 parent 0121225f
Showing with 16 additions and 22 deletions
......@@ -558,28 +558,22 @@ module.exports = (function() {
*
* @see {DAOFactory#find}
* @param {Object} [options] Options that are passed on to `DAOFactory.find`
* @return {EventEmitter}
* @return {Promise}
*/
DAO.prototype.reload = function(options) {
var where = [
this.QueryInterface.quoteTable(this.Model.name) + '.' + this.QueryInterface.quoteIdentifier(this.Model.primaryKeyAttribute)+'=?',
this.get(this.Model.primaryKeyAttribute, {raw: true})
]
return new Utils.CustomEventEmitter(function(emitter) {
this.Model.find({
where: where,
limit: 1,
include: this.options.include || null
}, options)
.on('sql', function(sql) { emitter.emit('sql', sql) })
.on('error', function(error) { emitter.emit('error', error) })
.on('success', function(obj) {
this.set(obj.dataValues, {raw: true, reset: true})
this.isDirty = false
emitter.emit('success', this)
}.bind(this))
}.bind(this)).run()
var self = this
, where = [
this.QueryInterface.quoteTable(this.Model.name) + '.' + this.QueryInterface.quoteIdentifier(this.Model.primaryKeyAttribute)+'=?',
this.get(this.Model.primaryKeyAttribute, {raw: true})
]
return this.Model.find({
where: where,
limit: 1,
include: this.options.include || null
}, options).then(function (reload) {
self.set(reload.dataValues, {raw: true, reset: true});
}).return(self);
}
/*
......@@ -611,7 +605,7 @@ module.exports = (function() {
* @param {Object} updates See `setAttributes`
* @param {Object} options See `save`
*
* @return {EventEmitter}
* @return {Promise}
*/
DAO.prototype.updateAttributes = function(updates, options) {
if (options instanceof Array) {
......@@ -632,7 +626,7 @@ module.exports = (function() {
* @param {Object} [options={}]
* @param {Boolean} [options.force=false] If set to true, paranoid models will actually be deleted
*
* @return {EventEmitter}
* @return {Promise}
*/
DAO.prototype.destroy = function(options) {
options = options || {}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!