Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow passing list of fields for validation to save #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ define([
// handling to the property definitions objects.
var hasOwnPropertyInstance,
property = object.hasOwnProperty('_properties') && object._properties[key];

hasOwnPropertyInstance = property;

if (!property) {
Expand Down Expand Up @@ -104,15 +104,15 @@ define([
// A base class for modelled data objects.

// schema: Object | dstore/Property
// A hash map where the key corresponds to a property definition.
// A hash map where the key corresponds to a property definition.
// This can be a string corresponding to a JavaScript
// primitive values (string, number, boolean), a constructor, a
// null (to allow any type), or a Property object with more advanced
// definitions.
schema: {},

// additionalProperties: boolean
// This indicates whether properties are allowed that are not
// This indicates whether properties are allowed that are not
// defined in the schema.
additionalProperties: true,

Expand Down Expand Up @@ -142,7 +142,7 @@ define([
values[key] = typeof defaultValue === 'function' ? defaultValue.call(this) : defaultValue;
}
}

},

_setValues: function (values) {
Expand All @@ -156,14 +156,17 @@ define([
save: function (/*Object*/ options) {
// summary:
// Saves this object, calling put or add on the attached store.
// options.skipValidation:
// options.skipValidation: boolean?
// Normally, validation is performed to ensure that the object
// is not invalid before being stored. Set `skipValidation` to
// true to skip it.
// options.validateFields: string[]?
// List of fields to pass to `validate` when only partially
// validating during save.
// returns: any

var object = this;
return when((options && options.skipValidation) ? true : this.validate(), function (isValid) {
return when((options && options.skipValidation) ? true : this.validate(options.validateFileds), function (isValid) {
if (!isValid) {
throw object.createValidationError(object.errors);
}
Expand Down Expand Up @@ -438,7 +441,7 @@ define([
// If this is true, it won't call the listener for the current value,
// just future updates. If this is true, it also won't return
// a new reactive object

var reactive;
if (typeof listener === 'string') {
// a property key was provided, use the Model's method
Expand All @@ -457,7 +460,7 @@ define([
var handle = this._addListener(function (value) {
var result = listener(value);
if (reactive) {
// TODO: once we have a real notification API again, call that, instead
// TODO: once we have a real notification API again, call that, instead
// of requesting a change
reactive.put(result);
}
Expand All @@ -474,7 +477,7 @@ define([
// Indicates whether or not to perform validation when properties
// are modified.
// This can provided immediate feedback and on the success
// or failure of a property modification. And Invalid property
// or failure of a property modification. And Invalid property
// values will be rejected. However, if you are
// using asynchronous validation, invalid property values will still
// be set.
Expand Down Expand Up @@ -624,7 +627,7 @@ define([
(this.type === typeof value))) {
errors.push(value + ' is not a ' + this.type);
}

if (this.required && !(value != null && value !== '')) {
errors.push('required, and it was not present');
}
Expand Down Expand Up @@ -758,4 +761,4 @@ define([
var simplePropertyValueOf = Property.prototype.valueOf;
var simplePropertyPut = Property.prototype.put;
return Model;
});
});