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

Debounce should not use an anonymous function #103

Merged
merged 1 commit into from
Jan 17, 2016
Merged
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: 18 additions & 7 deletions addon/validations/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function createCPValidationFor(attribute, validations) {
let cache = getDebouncedValidationsCacheFor(attribute, model);
// Return a promise and pass the resolve method to the debounce handler
value = new Promise(resolve => {
cache[getKey(validator)] = run.debounce(validator, () => resolve(validator.validate(attrValue, options, model, attribute)), debounce, false);
cache[getKey(validator)] = run.debounce(validator, debouncedValidate, validator, attrValue, options, model, attribute, resolve, debounce, false);
});
} else {
value = validator.validate(attrValue, options, model, attribute);
Expand Down Expand Up @@ -305,6 +305,21 @@ function getCPDependentKeysFor(attribute, validations) {
return dependentKeys.uniq();
}

/**
* Debounce handler for running a validation for the specified options
* @method debouncedValidate
* @private
* @param {Validator} validator
* @param {Unknown} value
* @param {Object} options
* @param {Object} model
* @param {String} attribute
* @param {Function} resolve
*/
function debouncedValidate(validator, value, options, model, attribute, resolve) {
resolve(validator.validate(value, options, model, attribute));
}

/**
* A handler used to create ValidationResult object from values returned from a validator
* @method validationReturnValueHandler
Expand Down Expand Up @@ -470,10 +485,8 @@ function lookupValidator(owner, type) {
*
* @method validate
* @param {Object} options
* - on: {Array} Will only run validations on the attributes in this list
* - excludes: {Array} Will skip validations on the attributes in this list
* @param {Boolean} async If false, will get all validations and will error if an async validations is found.
* If true, will get all validations and wrap them in a promise hash
* @param {Boolean} async If `false`, will get all validations and will error if an async validations is found.
* If `true`, will get all validations and wrap them in a promise hash
* @return {Promise or Object} Promise if async is true, object if async is false
*/
function validate(options = {}, async = true) {
Expand Down Expand Up @@ -531,8 +544,6 @@ function validate(options = {}, async = true) {
* ```
* @method validateSync
* @param {Object} options
* - on: {Array} Will only run validations on the attributes in this list
* - excludes: {Array} Will skip validations on the attributes in this list
* @return {Object}
*/
function validateSync(options) {
Expand Down