Git Product home page Git Product logo

watcher.js's Introduction

watcher.js

Easily watch for changes on your objects, based on Object.prototype.watch

Works in IE7+ and requires no additional shims

Installation

This can be installed via Bower with bower install watcher.js.

Alternatively, you can download the zip.

Examples

The Watcher constructor accepts only 1 argument, an Object. This can be a literal Object or an instance of a custom classish type thing.

var Person = function(name, age) {
  this.name = name;
  this.age = age;
};

Person.prototype.greet = function() {
  console.log('Hi! My name is ', this.name);
};

var stephen = new Person('Stephen', 26);

stephen = new Watcher(stephen);
stephen.greet();
// "Hi! My name is Stephen"

Watching for changes is very simple, simply watch a specific property and specify a callback to be fired whenever its value changes.

stephen.watch('age', function() {
  console.log('Hey ', this.get('name'), ', is it your birthday?');
});

stephen.set('age', 27);
// "Hey Stephen, is it your birthday?"

You can add as many watchers to a single property as you'd like

stephen.watch('age', function(age) {
  if (age > 39) {
    console.log('Looks like some one is over the hill!');
  }
});

stephen.set('age', 40);
// "Hey, Stephen, is it your birthday?"
// "Looks like some one is over the hill!"

Easily remove a single watcher by passing in the handler's reference, or remove all the handlers by passing in just the property name!

stephen.unwatch('age');
stephen.set('age', 99);
// *crickets*

watcher.js's People

Contributors

callmehiphop avatar stephenplusplus avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.