DoneJS StealJS jQuery++ FuncUnit DocumentJS
4.3.0
5.0.0 3.13.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-define
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
        • methods
          • bind
          • from
          • returnedBy
          • to
          • with
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-value

  • npm package badge
  • Star
  • Edit on GitHub

Get an observable that’s bound to a specific property on another object.

Object

can-value exports an object with the following methods:

{
    bind(object, keyPath)
    // Returns an observable for getting and setting a property on an object.

    from(object, keyPath)
    // Returns an observable for only getting a property on an object.

    returnedBy(getter)
    // Creates an observable that derives its value from other observable values.

    to(object, keyPath)
    // Returns an observable for only setting a property on an object.

    with(initialValue)
    // Creates an observable with an initial value that can be read, written, and observed.
}

Use

Observable from an initial value

At its simplest, with can be used to create an observable from another initial value:

import value from "can-value";

const observable = value.with(15);

observable.value; // is 15

You can use onValue to listen for when the observable value changes:

const handler = function(newValue) {
  newValue; // is 22
};
canReflect.onValue(observable, handler);
observable.value = 22;

Observable derived from other values

returnedBy can be used to create an observable value that derives its value from other observable values. When the derived values change, the observable’s value will be updated automatically.

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

import canReflect from "can-reflect";
import observe from "can-observe";
import value from "can-value";

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

const fullName = value.returnedBy( function() {
    return person.first + " " + person.last;
} );
fullName.value; // is "Grace Murray"

You can use onValue to listen for when the observable value changes (because one of the values from which the observable derives its value changed):

const handler = function(newValue) {
  newValue; // is "Grace Hopper"
};
canReflect.onValue(fullName, handler);
person.last = "Hopper";

Bind to other objects

Use can-value when you need an observable that can get or set a property on an object.

In the example below, we use bind to get an observable that can get and set outer.inner.key:

import DefineMap from "can-define/map/map";
import value from "can-value";

const outer = new DefineMap({
  inner: {
    key: "hello"
  }
});

const keyObservable = value.bind(outer, "inner.key");

Now if we read keyObservable.value, we get the value at outer.inner.key:

keyObservable.value; // is "hello"

We can also set keyObservable.value to change the value at outer.inner.key:

keyObservable.value = "aloha";
// Now outer.inner.key === "aloha"

from and to exist to create observables that just get or just set properties on an object, respectively.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 4.3.0.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news