DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
  • Infrastructure
    • can-construct
    • can-control
    • can-event
    • can-event/async/async
    • can-event/batch/batch
    • can-observation
      • types
        • getTrapped
        • Observed
      • static
        • add
        • addAll
        • ignore
        • isRecording
        • trap
      • prototype
        • start
        • stop
    • can-simple-map
    • can-util
    • can-view-callbacks
    • can-view-live
    • can-view-model
    • can-view-nodelist
    • can-view-parser
    • can-view-scope
    • can-view-target
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Infrastructure
  • /
  • can-observation
  • / On this page
    • can-observation

      module

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

      • npm package badge
      • Star
      • source

      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.0.0.