Skip to content

Commit 82de121

Browse files
committed
fixes #194 able to read the internal observed value
1 parent 5a3587d commit 82de121

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

Diff for: scope-key-data.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var dispatchSymbol = canSymbol.for("can.dispatch");
2929
// this work.
3030

3131
var peekValue = ObservationRecorder.ignore(canReflect.getValue.bind(canReflect));
32+
var peekGet = ObservationRecorder.ignore(Observation.prototype.get);
3233

3334
var getFastPathRoot = ObservationRecorder.ignore(function(computeData){
3435
if( computeData.reads &&
@@ -112,6 +113,10 @@ var ScopeKeyData = function(scope, key, options){
112113
var valueDependencies = new Set();
113114
valueDependencies.add(observation);
114115
this.dependencies = {valueDependencies: valueDependencies};
116+
117+
// This is basically what .get() should give, but it
118+
// isn't used to figure out the last value.
119+
this._latestValue = undefined;
115120
};
116121

117122
valueEventBindings(ScopeKeyData.prototype);
@@ -128,7 +133,7 @@ assign(ScopeKeyData.prototype, {
128133
constructor: ScopeKeyData,
129134
dispatch: function dispatch(newVal){
130135
var old = this.value;
131-
this.value = newVal;
136+
this._latestValue = this.value = newVal;
132137
// call the base implementation in can-event-queue
133138
this[dispatchSymbol].call(this, this.value, old);
134139
},
@@ -141,7 +146,7 @@ assign(ScopeKeyData.prototype, {
141146
// rewrite the observation to call its event handlers
142147
this.toFastPath(fastPathRoot);
143148
}
144-
this.value = peekValue(this.observation);
149+
this._latestValue = this.value = peekGet.call(this.observation);
145150
},
146151
onUnbound: function onUnbound() {
147152
this.bound = false;
@@ -168,10 +173,10 @@ assign(ScopeKeyData.prototype, {
168173
}
169174
}
170175

171-
if (this.bound === true ) {
172-
return this.value;
176+
if (this.bound === true && this.fastPath === true) {
177+
return this._latestValue;
173178
} else {
174-
return this.observation.get();
179+
return peekGet.call(this.observation);
175180
}
176181
},
177182
toFastPath: function(fastPathRoot){
@@ -188,6 +193,7 @@ assign(ScopeKeyData.prototype, {
188193
// but I think we will be able to get at it b/c there should only be one
189194
// dependency we are binding to ...
190195
if(target === fastPathRoot && typeof newVal !== "function") {
196+
self._latestValue = newVal;
191197
this.newVal = newVal;
192198
} else {
193199
// restore

Diff for: test/scope-key-data-test.js

+39
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,42 @@ QUnit.test("ScopeKeyData's thisArg is observable", function(){
4646
value: "B"
4747
});
4848
});
49+
50+
QUnit.test("reading ScopeKeyData will update underlying observable", function(){
51+
var context = new SimpleMap({
52+
"prop" :"value"
53+
});
54+
55+
var prop = new Scope(context).computeData("this.prop",{proxyMethods: false});
56+
57+
canReflect.onValue(prop, function(){});
58+
59+
context.on("prop", function(){
60+
61+
QUnit.equal(canReflect.getValue(prop), "VALUE", "able to read fastPath value");
62+
},"notify");
63+
64+
context.set("prop", "VALUE");
65+
66+
67+
var root = new SimpleObservable("value");
68+
var observation = new Observation(function(){
69+
return root.value;
70+
});
71+
72+
context = {
73+
"prop" : observation
74+
};
75+
76+
prop = new Scope(context).computeData("this.prop",{proxyMethods: false});
77+
78+
canReflect.onValue(prop, function(){});
79+
80+
81+
canReflect.onValue(root, function(){
82+
83+
QUnit.equal(canReflect.getValue(prop), "VALUE", "able to read deep, non-fast-path value");
84+
},"notify");
85+
86+
root.value = "VALUE";
87+
});

Diff for: test/scope-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ QUnit.test("./ scope lookup should read current scope", function () {
631631
var parent = new SimpleMap();
632632
var map = new SimpleMap();
633633
var scope = new Scope(parent).add(map);
634-
QUnit.equal(scope.attr("./"), map);
634+
QUnit.equal(scope.get("./"), map);
635635
});
636636

637637
QUnit.test("getTemplateContext() gives a scope with the templateContext", function() {

0 commit comments

Comments
 (0)