Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added instance methods to model and modified model validation #90

Merged
merged 2 commits into from

2 participants

@peruzzo

I ended up making two changes in this pull request.

1) Added a setting in the configuration model that accepts methods instance, I needed to customize the toJSON for all my models, using the onLoad method I could not do in the instance where it was used the new.

2) modified the validation method to pass as argument the connection, enabling more complex validations do. Another change was that if the return message is not a string, its value as the callback object will be returned.

peruzzo and others added some commits
@peruzzo peruzzo added the connection as an argument in the method validation. If the
return of the validation is not a string object itself is returned.
8b09c6b
Tiago Peruzzo added instanceMethods 993d6c4
@joeferner joeferner merged commit bc49fa2 into joeferner:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 13, 2013
  1. @peruzzo

    added the connection as an argument in the method validation. If the

    peruzzo authored
    return of the validation is not a string object itself is returned.
  2. added instanceMethods

    Tiago Peruzzo authored
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 6 deletions.
  1. +22 −6 lib/connection.js
  2. +4 −0 lib/model.js
View
28 lib/connection.js
@@ -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
4 lib/model.js
@@ -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;
};
Something went wrong with that request. Please try again.