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

returnedBy

  • Edit on GitHub

Creates an observable that derives its value from other observable values.

value.returnedBy( getter(), context )

Creates an observable value that can be read and observed using can-reflect.

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 {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";

Parameters

  1. getter {function}:

    A function that returns the value being observed.

  2. context {Object}:

    An optional context that will be used as the this of getter.

Returns

{Object}:

An observable compatible with getValue. It also has a value property that can be used to get and set the value.

value.returnedBy( getter(lastSet), context, initialValue )

Creates an observable value that can be set. When the getter function takes an argument, the getter function will be passed whatever the observable was last set to.

This form is similar to with. But it can be used to "clean up" values as follows:

import {value} from "can";

var age = value.returnedBy(function(lastSet){
  return lastSet == null ? 0 : +lastSet;
});
age.value //-> 0
age.value = "5";
age.value //-> 5

Parameters

  1. getter {function(lastSet)}:

    A function that returns the value being observed.

  2. context {Object}:

    An optional context that will be used as the this of getter.

  3. initialValue {Object}:

    The initial lastSet value.

Returns

{Object}:

An observable compatible with getValue and setValue. It also has a value property that can be used to get and set the value.

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