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

Commit 2f4d0f13 by Mick Hansen

feat(promises): implement promise in dao.destroy()

1 parent e41a2a67
......@@ -654,39 +654,29 @@ module.exports = (function() {
options.force = options.force === undefined ? false : Boolean(options.force)
var self = this
, query = null
, promise
return new Utils.CustomEventEmitter(function(emitter) {
self.Model.runHooks(self.Model.options.hooks.beforeDestroy, self, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
// This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
promise = self.Model.runHooks(self.Model.options.hooks.beforeDestroy, self).then(function () {
var query
, identifier
if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date()
query = self.save(options)
} else {
var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
query = self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options)
}
query.on('sql', function(sql) {
emitter.emit('sql', sql)
})
.error(function(err) {
emitter.emit('error', err)
})
.success(function(results) {
self.Model.runHooks(self.Model.options.hooks.afterDestroy, self, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
return query.proxySql(promise);
}).then(function (results) {
return self.Model.runHooks(self.Model.options.hooks.afterDestroy, self).then(function () {
return results;
});
});
emitter.emit('success', results)
})
})
})
}).run()
return promise
}
/**
......
......@@ -214,6 +214,12 @@ module.exports = (function() {
return this
}
CustomEventEmitter.prototype.proxySql = function(promise) {
return this.proxy(promise, {
events: ['sql']
})
}
/**
* Attach listeners to the emitter, promise style.
*
......
......@@ -59,8 +59,9 @@ Hooks.runHooks = function() {
var self = this
, tick = 0
, hooks = arguments[0]
, args = Array.prototype.slice.call(arguments, 1, arguments.length-1)
, fn = arguments[arguments.length-1]
, lastIndex = arguments.length-1
, fn = typeof arguments[lastIndex] === "function" ? arguments[lastIndex] : null
, args = Array.prototype.slice.call(arguments, 1, fn ? lastIndex : arguments.length)
if (typeof hooks === "string") {
hooks = this.options.hooks[hooks] || []
......@@ -98,7 +99,9 @@ Hooks.runHooks = function() {
run(hooks[tick])
}).spread(function () {
if (fn) {
fn.apply(self, [null].concat(Array.prototype.slice.apply(arguments)));
}
}, fn);
}
......
......@@ -4,6 +4,7 @@ var util = require("util")
, proxyEventKeys = ['success', 'error', 'sql']
, Utils = require('./utils')
, INTERNAL = function() {}
, async = require("bluebird/js/main/async.js")
var SequelizePromise = function(resolver) {
var self = this;
......@@ -31,9 +32,33 @@ var SequelizePromise = function(resolver) {
util.inherits(SequelizePromise, Promise)
// Need to hack then to make sure our promise is chainable
SequelizePromise.prototype.then = function (didFulfill, didReject, didProgress) {
return this._then(didFulfill, didReject, didProgress, void 0, new SequelizePromise(function () {}), this.then);
// Need to hack _then to make sure our promise is chainable
Promise.prototype._then = function (
didFulfill,
didReject,
didProgress,
receiver,
internalData
) {
var haveInternalData = internalData !== void 0;
var ret = haveInternalData ? internalData : new SequelizePromise(INTERNAL); // The relevant line, rest is fine
if (!haveInternalData && this._isBound()) {
ret._setBoundTo(this._boundTo);
}
var callbackIndex = this._addCallbacks(didFulfill, didReject, didProgress, ret, receiver);
if (!haveInternalData && this._cancellable()) {
ret._setCancellable();
ret._cancellationParent = this;
}
if (this.isResolved()) {
async.invoke(this._queueSettleAt, this, callbackIndex);
}
return ret;
};
SequelizePromise.prototype.on = function(evt, fct) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!