Skip to content

Fix for pure computeds getting auto-evaluated after mapping #9

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion knockout.mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
}

var realDeferEvaluation = options.deferEvaluation;
var realIsPure = options.pure;

var isRemoved = false;

Expand Down Expand Up @@ -310,7 +311,7 @@
options.deferEvaluation = true; // will either set for just options, or both read/options.
var realDependentObservable = realKoDependentObservable(read, owner, options);

if (!realDeferEvaluation) {
if (!realDeferEvaluation && !realIsPure) {
realDependentObservable = wrap(realDependentObservable);
dependentObservables.push(realDependentObservable);
}
Expand Down
28 changes: 26 additions & 2 deletions spec/proxyDependentObservableBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
if (createOptions.useReadCallback) {
mapped.DO = createComputed({
read: doData,
deferEvaluation: !!createOptions.deferEvaluation
deferEvaluation: !!createOptions.deferEvaluation,
pure: !!createOptions.pure
}, mapped);
}
else if (createOptions.useWriteCallback) {
Expand All @@ -57,7 +58,8 @@
}
else {
mapped.DO = createComputed(doData, mapped, {
deferEvaluation: !!createOptions.deferEvaluation
deferEvaluation: !!createOptions.deferEvaluation,
pure: !!createOptions.pure
});
}

Expand Down Expand Up @@ -271,6 +273,17 @@
}, 0);
});

QUnit.test('pure dependentObservables should NOT be auto-evaluated after mapping', function(assert) {
var done = assert.async();
assert.expect(1);

var mapped = testInfo.create({pure: true});
window.setTimeout(function() {
assert.equal(testInfo.evaluationCount, 0);
done();
}, 0);
});

QUnit.test('un-deferred dependentObservables with read callback that are NOT used immediately SHOULD be auto-evaluated after mapping', function(assert) {
var done = assert.async();
assert.expect(1);
Expand Down Expand Up @@ -305,6 +318,17 @@
}, 0);
});

QUnit.test('pure dependentObservables with read callback should NOT be auto-evaluated after mapping', function(assert) {
var done = assert.async();
assert.expect(1);

var mapped = testInfo.create({pure: true, useReadCallback: true});
window.setTimeout(function() {
assert.equal(testInfo.evaluationCount, 0);
done();
}, 0);
});

QUnit.test('can subscribe to proxy dependentObservable', function(assert) {
assert.expect(0);
var mapped = testInfo.create({deferEvaluation: true, useReadCallback: true});
Expand Down