Skip to content

Commit

Permalink
[BUGFIX] Debounce context not properly set (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan committed Jan 12, 2017
1 parent a233a42 commit 441d154
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion addon/validations/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ function generateValidationResultsFor(attribute, model, validators, validate, op

// Return a promise and pass the resolve method to the debounce handler
value = new Promise((resolve) => {
let t = run.debounce(validator, resolve, debounce, false);
let t = run.debounce(validator, resolveDebounce, resolve, debounce);

if (!opts.disableDebounceCache) {
cache[guidFor(validator)] = t;
Expand Down Expand Up @@ -747,6 +747,18 @@ function lookupValidator(owner, type) {
return validatorClass;
}

/**
* Call the passed resolve method. This is needed as run.debounce expects a
* static method to work properly.
*
* @method resolveDebounce
* @private
* @param {Function} resolve
*/
function resolveDebounce(resolve) {
resolve();
}

/**
* ```javascript
* model.validate({ on: ['username', 'email'] }).then(({ m, validations }) => {
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/validations/factory-general-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,33 @@ test('debounced validations', function(assert) {
}, 500);
});

test('debounced validator should only be called once', function(assert) {
let count = 0;

let done = assert.async();
let Validations = buildValidations({
firstName: validator(() => count++, {
debounce: 500
})
});

let object = setupObject(this, Ember.Object.extend(Validations));

object.set('firstName', 'O');
object.get('validations.attrs.firstName.isValid');

object.set('firstName', 'Off');
object.get('validations.attrs.firstName.isValid');

object.set('firstName', 'Offir');
object.get('validations.attrs.firstName.isValid');

run.later(() => {
assert.equal(count, 1);
done();
}, 500);
});

test('debounced validations should cleanup on object destroy', function(assert) {
let done = assert.async();
let initSetup = true;
Expand Down

0 comments on commit 441d154

Please sign in to comment.