Skip to content

Commit

Permalink
Merge pull request #135 from canjs/130-value-api
Browse files Browse the repository at this point in the history
Add a value property getter
  • Loading branch information
chasenlehara authored Jul 4, 2018
2 parents 2587f35 + 0bda030 commit 2d79a51
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
19 changes: 14 additions & 5 deletions can-observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ function Observation(func, context, options){
// A flag if we are bound or not
this.bound = false;

// Set _value to undefined so can-view-scope & can-compute can check for it
this._value = undefined;

// These properties will manage what our new and old dependencies are.
this.newDependencies = ObservationRecorder.makeDependenciesRecord();
this.oldDependencies = null;
Expand Down Expand Up @@ -74,7 +77,7 @@ canReflect.assign(Observation.prototype, {
// Start recording dependencies.
ObservationRecorder.start();
// Call the observation's function and update the new value.
this.value = this.func.call(this.context);
this._value = this.func.call(this.context);
// Get the new dependencies.
this.newDependencies = ObservationRecorder.stop();

Expand Down Expand Up @@ -122,13 +125,13 @@ canReflect.assign(Observation.prototype, {
update: function() {
if (this.bound === true) {
// Keep the old value.
var oldValue = this.value;
var oldValue = this._value;
this.oldValue = null;
// Re-run `this.func` and update dependency bindings.
this.onBound();
// If our value changed, call the `dispatch` method provided by `can-event-queue/value/value`.
if (oldValue !== this.value) {
this[dispatchSymbol](this.value, oldValue);
if (oldValue !== this._value) {
this[dispatchSymbol](this._value, oldValue);
}
}
},
Expand Down Expand Up @@ -166,7 +169,7 @@ canReflect.assign(Observation.prototype, {
Observation.updateChildrenAndSelf(this);
}

return this.value;
return this._value;
} else {
// If we are not bound, just call the function.
return this.func.call(this.context);
Expand Down Expand Up @@ -205,6 +208,12 @@ canReflect.assign(Observation.prototype, {
}
});

Object.defineProperty(Observation.prototype, "value", {
get: function() {
return this.get();
}
});

var observationProto = {
"can.getValue": Observation.prototype.get,
"can.isValueLike": true,
Expand Down
14 changes: 12 additions & 2 deletions can-observation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ QUnit.test("getValueDependencies work with can-reflect", function() {

});

QUnit.test("Observation can listen to something decorated with onValue and offValue", function(){
QUnit.test("Observation can listen to value decorated with onValue and offValue", function(){
var v1 = reflectiveValue(1);
var v2 = reflectiveValue(2);

Expand All @@ -430,7 +430,7 @@ QUnit.test("Observation can listen to something decorated with onValue and offVa
});


QUnit.test("Observation can listen to something decorated with onValue and offValue", function(){
QUnit.test("Observation can listen to observable decorated with onValue and offValue", function(){
var v1 = reflectiveObservable(1);
var v2 = reflectiveObservable(2);

Expand Down Expand Up @@ -584,3 +584,13 @@ skipProductionTest("Observation decorates onDependencyChange handler", function(
"onDependencyChange changes the observation"
);
});

QUnit.test("value property getter", function() {
var observation = new Observation(function() {
return "Hello";
});

// Check getting the value
QUnit.equal(observation.value, "Hello", "value returns");

});
7 changes: 3 additions & 4 deletions doc/can-observation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
@parent can-observables
@collection can-infrastructure
@group can-observation.prototype prototype
@group can-observation.types types
@package ../package.json

Create observable values that derive their value from other observable
Expand All @@ -26,7 +25,7 @@ const fullName = new Observation( function() {
return person.first + " " + person.last;
} );

fullName.get(); //-> "Ramiya Meyer";
fullName.value; //-> "Ramiya Meyer";

fullName.on( function( newName ) {
newName; //-> "Bodhi Meyer"
Expand Down Expand Up @@ -72,7 +71,7 @@ const fullName = new Observation( function() {
return person.first + " " + person.last;
} );

fullName.get(); //-> "Ramiya Meyer";
fullName.value; //-> "Ramiya Meyer";

fullName.on( function( newName ) {
newName; //-> "Bodhi Meyer"
Expand All @@ -90,4 +89,4 @@ Use [can-observation.prototype.off] to unbind.
observables call OR.add.
- Binds to those using recorder-dependency-helpers
- when a change happens, adds itself to the notify queue
- repeats process
- repeats process
21 changes: 21 additions & 0 deletions doc/value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@property can-observation.prototype.value value
@parent can-observation.prototype
@description Reads the value of the observation.
@signature `observation.value`

The following creates a `fullName` observation that derives its values from
the `person` observable. The value of the observation is read with
`fullName.value`:

```js
import Observation from "can-observation";
import observe from "can-observe";

const person = observe( { first: "Grace", last: "Hopper" } );

const fullName = new Observation( function() {
return person.first + " " + person.last;
} );

fullName.value; //-> "Grace Hopper";
```

0 comments on commit 2d79a51

Please sign in to comment.