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

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