DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 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-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
        • types
          • getTrapped
          • Observed
        • static
          • add
          • addAll
          • ignore
          • isRecording
          • trap
        • prototype
          • start
          • stop
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • 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-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • 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-namespace
      • can-reflect
      • 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-observation

  • npm package badge
  • Star
  • Edit on GitHub

Provides a mechanism to notify when an observable has been read and a way to observe those reads called within a given function.

new Observation(func, context, compute)

Creates an observation of a given function called with this as a given context. Calls back compute when the return value of func changes.

Parameters

  1. func {function}:

    The function whose value is being observed.

  2. context {*}:

    What this should be when func is called.

  3. updated {function(newValue, oldValue, batchNum)}:

    A function to call when func's return value changes.

Use

Instances of Observation are rarely created directly. Instead, use can-compute's more friendly API to observe when a function's value changes. can-compute uses can-observation internally.

Observation's static methods like: add, ignore, and trap are used more commonly to control which observable events a compute will listen to.

To use can-observation directly, create something observable (supports addEventListener) and calls add like:

var Observation = require("can-observation");
var assign = require("can-util/js/assign/assign");
var canEvent = require("can-event");

var me = assign({}, canEvent);

var name = "Justin";
Object.defineProperty(me,"name",{
  get: function(){
    Observation.add(this,"name");
    return name;
  },
  set: function(newVal) {
    var oldVal = name;
    name = newVal;
    this.dispatch("name", newVal, oldVal);
  }
})

Next, create an observation instance with a function that reads the observable value:

var observation = new Observation(function(){
  return "Hello "+me.name;
}, null, function(newVal, oldVal, batchNum){
  console.log(newVal);
})

Finally, call observation.start() to start listening and be notified of changes:

observation.start();
observation.value   //-> "Hello Justin";
me.name = "Ramiya"; // console.logs -> "Hello Ramiya"

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

On this page

Get help

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