Permalink
Browse files

[fix] check for callbacks before invoking

  • Loading branch information...
1 parent 9a8aff5 commit 7c3afd1d034a6b6575d7349deddf1bd00713374f @bmeck bmeck committed Nov 7, 2011
Showing with 26 additions and 10 deletions.
  1. +26 −10 lib/resourceful/resource.js
@@ -38,8 +38,14 @@ Resource.runBeforeHooks = function (method, obj, callback, finish) {
if (hook) {
hook(obj, function (e, obj) {
- if (e || obj) { callback(e, obj); }
- else { loop(hooks); }
+ if (e || obj) {
+ if(callback) {
+ callback(e, obj);
+ }
+ }
+ else {
+ loop(hooks);
+ }
});
} else {
finish();
@@ -106,7 +112,9 @@ Resource._request = function (/* method, [key, obj], callback */) {
} else {
that.runAfterHooks(method, e, obj, function () {
that.emit("error", e, obj);
- callback(e);
+ if (callback) {
+ callback(e);
+ }
});
}
} else {
@@ -142,7 +150,9 @@ Resource._request = function (/* method, [key, obj], callback */) {
that.runAfterHooks(method, null, obj, function (e, res) {
if (e) { that.emit('error', e); }
else { that.emit(method, res || result); }
- callback(e || null, result);
+ if (callback) {
+ callback(e || null, result);
+ }
});
}
});
@@ -156,8 +166,7 @@ Resource.get = function (id, callback) {
}
return id ?
this._request('get', id, callback)
- :
- callback(new Error('key is undefined'));
+ : callback && callback(new Error('key is undefined'));
};
Resource.create = function (attrs, callback) {
@@ -169,7 +178,10 @@ Resource.create = function (attrs, callback) {
validate = this.prototype.validate(instance, this.schema);
if (!validate.valid) {
- return callback(validate.errors);
+ if (callback) {
+ callback(validate.errors);
+ }
+ return;
}
instance.save(function (e, res) {
@@ -178,15 +190,17 @@ Resource.create = function (attrs, callback) {
instance._rev = instance._rev || res.rev;
}
- callback(e, instance);
+ if (callback) {
+ callback(e, instance);
+ }
});
};
Resource.save = function (obj, callback) {
var validate = this.prototype.validate(obj, this.schema);
if (!validate.valid) {
- return callback(validate.errors);
+ return callback && callback(validate.errors);
}
if (this._timestamps) {
@@ -500,7 +514,9 @@ Resource.prototype.save = function (callback) {
self.isNewRecord = false;
}
- callback(err, res);
+ if (callback) {
+ callback(err, res);
+ }
});
}
};

0 comments on commit 7c3afd1

Please sign in to comment.