Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[api] Added ability to modify instance values during resource.before(…

…'create').
  • Loading branch information...
commit 17425b52414ee52e857fbe5062d6f17a1138d7f5 1 parent a67f4f4
@Marak Marak authored
Showing with 23 additions and 17 deletions.
  1. +23 −17 lib/resourceful/resource.js
View
40 lib/resourceful/resource.js
@@ -192,33 +192,39 @@ Resource.create = function (attrs, callback) {
}
var that = this;
- this.runBeforeHooks("create", attrs, callback, function () {
- var instance = new(that)(attrs);
- // could happen after validate, but would unnecessarily remove the validation safety net
- that.runAfterHooks("create", null, instance, function (e, res) {
- if (e) {
- return that.emit('error', e);
- }
+ var instance = new(that)(attrs);
+ var validate = that.prototype.validate(instance, that.schema);
- var validate = that.prototype.validate(instance, that.schema);
+ if (!validate.valid) {
+ var e = { validate: validate, value: attrs, schema: that.schema };
+ that.emit('error', e);
+ if (callback) {
+ callback(e);
+ }
+ return;
+ }
- // Why is this here? We are validating in Resource.save
- if (!validate.valid) {
- var e = { validate: validate, value: instance, schema: that.schema };
- that.emit('error', e);
- if (callback) {
- callback(e);
- }
- return;
+ this.runBeforeHooks("create", instance, callback, function (err, result) {
+
+ if (!validate.valid) {
+ var e = { validate: validate, value: attrs, schema: that.schema };
+ that.emit('error', e);
+ if (callback) {
+ callback(e);
}
+ return;
+ }
+ that.runAfterHooks("create", null, instance, function (e, res) {
+ if (e) {
+ return that.emit('error', e);
+ }
instance.save(function (e, res) {
if (res) {
instance._id = instance._id || res.id;
instance._rev = instance._rev || res.rev;
}
-
if (callback) {
callback(e, instance);
}
Please sign in to comment.
Something went wrong with that request. Please try again.