Permalink
Browse files

Merge pull request #90 from peruzzo/master

Added instance methods to model and modified model validation
  • Loading branch information...
2 parents 233008d + 993d6c4 commit bc49fa2a7505dd774ceceaa879230dfc027eb02e @joeferner committed Sep 13, 2013
Showing with 26 additions and 6 deletions.
  1. +22 −6 lib/connection.js
  2. +4 −0 lib/model.js
View
@@ -58,24 +58,40 @@ var Connection = Class.extend({
var self = this;
if (obj.validate && typeof(obj.validate) === 'function') {
- return obj.validate(function(success, message) {
+ var args = [];
+ if (obj.validate.length === 2) {
+ args.push(self);
+ }
+
+ args.push(function(success, message) {
if (success) {
return doModelValidation();
}
- return callback(new Error("Validation failed: " + message));
+ return callback(typeof(message) === 'string' ? new Error("Validation failed: " + message) : message);
});
+
+ return obj.validate.apply(null, args);
+
} else {
return doModelValidation();
}
function doModelValidation() {
- if (obj._getModel().validate) {
- return obj._getModel().validate(obj, function(success, message) {
+ if (obj._getModel().validate) {
+ var args = [obj];
+ if (obj._getModel().validate.length === 3) {
+ args.push(self);
+ }
+
+ args.push(function(success, message) {
if (success) {
return doSave();
}
- return callback(new Error("Validation failed: " + message));
- });
+ return callback(typeof(message) === 'string' ? new Error("Validation failed: " + message) : message);
+ });
+
+ return obj._getModel().validate.apply(null, args);
+
} else {
return doSave();
}
View
@@ -207,6 +207,10 @@ exports.define = function (name, columnDefs, opts) {
}
createColumnPropertiesOnObject(this);
+ for (var key in opts.instanceMethods) {
+ this[key] = opts.instanceMethods[key];
+ this[key] = persistUtil.bind(key, this[key], this);
+ }
return this;
};

0 comments on commit bc49fa2

Please sign in to comment.