DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 4.3.0 3.14.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-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • 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
    • Views
      • can-attribute-observable
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • 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
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • 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-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-type
      • 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. As how an age observable being created in this next example:

import {value, Reflect as canReflect} from "can";

const age = value.with(15);

console.log( age.value ); //-> 15

Notice that onValue can be used to listen to changes. The following shows creating an age observable, reading it's value, then listening when age changes:

import {value, Reflect as canReflect} from "can";

const age = value.with(15);

const handler = newValue => {
  console.log( newValue ); //-> 18 ... time to Vote!
};

canReflect.onValue(observable, handler);
age.value = 18;

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 first and last observables. The value of the observable is read with fullName.value. Notice how onValue is used to listen when the observable value changes. When one of the values from which the observable derives its value changed:

import {value, Reflect as canReflect} from "can";

const first = value.with("Grace");
const last = value.with("Murray");

const fullName = value.returnedBy( () => {
  return first.value + " " + last.value;
} );

console.log( fullName.value ); //-> "Grace Murray"

const handler = newValue => {
  console.log( newValue ); //-> "Grace Hopper"
};

canReflect.onValue(fullName, handler);
last.value = "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, value} from "can";

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

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

// reading `keyObservable.value`, we get the value at `outer.inner.key`
console.log( keyObservable.value ); //-> "hello"

// writing to `keyObservable.value` will change the value at `outer.inner.key`
keyObservable.value = "aloha";
console.log( 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 5.33.3.

On this page

Get help

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