Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Don't wrap dependent observables if already marked as deferEvaluation #73

Merged
merged 2 commits into from

2 participants

@jamesfoster

As mentioned in Issue #69, the deferEvaluation flag is not always respected.

This is the fix.

@RoyJacobs RoyJacobs merged commit 5b2c61d into SteveSanderson:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 68 additions and 11 deletions.
  1. +5 −5 knockout.mapping.js
  2. +63 −6 spec/proxyDependentObservableBehaviors.js
View
10 knockout.mapping.js
@@ -207,12 +207,12 @@
ko.dependentObservable = function (read, owner, options) {
options = options || {};
- var realDeferEvaluation = options.deferEvaluation;
-
if (read && typeof read == "object") { // mirrors condition in knockout implementation of DO's
options = read;
}
+ var realDeferEvaluation = options.deferEvaluation;
+
var isRemoved = false;
// We wrap the original dependent observable so that we can remove it from the 'dependentObservables' list we need to evaluate after mapping has
@@ -230,14 +230,13 @@
return DO(val);
},
deferEvaluation: true
- });
- wrapped.__ko_proto__ = realKoDependentObservable;
+ });
+ if(DEBUG) wrapped._wrapper = true;
return wrapped;
};
options.deferEvaluation = true; // will either set for just options, or both read/options.
var realDependentObservable = new realKoDependentObservable(read, owner, options);
- realDependentObservable.__ko_proto__ = realKoDependentObservable;
if (!realDeferEvaluation) {
realDependentObservable = wrap(realDependentObservable);
@@ -246,6 +245,7 @@
return realDependentObservable;
}
+ ko.dependentObservable.fn = realKoDependentObservable.fn;
ko.computed = ko.dependentObservable;
var result = callback();
ko.dependentObservable = localDO;
View
69 spec/proxyDependentObservableBehaviors.js
@@ -29,14 +29,17 @@ var generateProxyTests = function(useComputed) {
return "test";
};
if (createOptions.useReadCallback) {
- DOdata = {
- read: DOdata
- };
+ mapped.DO = func({
+ read: DOdata,
+ deferEvaluation: !!createOptions.deferEvaluation
+ }, mapped);
+ }
+ else {
+ mapped.DO = func(DOdata, mapped, {
+ deferEvaluation: !!createOptions.deferEvaluation
+ });
}
- mapped.DO = func(DOdata, mapped, {
- deferEvaluation: !!createOptions.deferEvaluation
- });
return mapped;
}
}
@@ -339,6 +342,60 @@ var generateProxyTests = function(useComputed) {
equal(i.evaluationCount, 2);
});
});
+
+ test('dependentObservable.fn extensions are not missing during mapping', function() {
+ var obj = {
+ x: 1
+ };
+
+ var model = function(data) {
+ var _this = this;
+
+ ko.mapping.fromJS(data, {}, _this);
+
+ _this.DO = func(_this.x);
+ };
+
+ var mapping = {
+ create: function(options) {
+ return new model(options.data);
+ }
+ };
+
+ ko.dependentObservable.fn.myExtension = true;
+
+ var mapped = ko.mapping.fromJS(obj, mapping);
+
+ equal(mapped.DO.myExtension, true)
+ });
+
+ test('Dont wrap dependent observables if already marked as deferEvaluation', function() {
+ var obj = {
+ x: 1
+ };
+
+ var model = function(data) {
+ var _this = this;
+
+ ko.mapping.fromJS(data, {}, _this);
+
+ _this.DO1 = func(_this.x, null, {deferEvaluation: true});
+ _this.DO2 = func({read: _this.x, deferEvaluation: true});
+ _this.DO3 = func(_this.x);
+ };
+
+ var mapping = {
+ create: function(options) {
+ return new model(options.data);
+ }
+ };
+
+ var mapped = ko.mapping.fromJS(obj, mapping);
+
+ equal(mapped.DO1._wrapper, undefined);
+ equal(mapped.DO2._wrapper, undefined);
+ equal(mapped.DO3._wrapper, true);
+ });
};
generateProxyTests(false);
Something went wrong with that request. Please try again.