Wrap a function and notify any subscribers when any observables inside have changed
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
specs
src
.gitattributes
.gitignore
.jshintrc
Gruntfile.js
knockout.wrap.js
knockout.wrap.min.js
package.json
readme.md

readme.md

knockout.wrap

Wrap a function and notify any subscribers when any observables inside have changed.

(Basically a ko.computed that doesn't actually calculate on change)

Example

var a = ko.observable(2),
    b = ko.observable(2);

function expensiveOperation() {
  console.log(a() + b());
}

var wrapped = ko.wrap(expensiveOperation);
wrapped.subscribe(function() {
  console.log('time to update');
});

// Call first time to identify dependencies
wrapped() // -> 4

// Some inside dependency updates, notify subscribers
a(3);     // -> time to update

// Don't call expensive operation unless explicitly called
wrapped() // -> 5

About

  • Author: Tim Hall
  • License: MIT
  • Dependencies: knockout