Git Product home page Git Product logo

lookoutjs's Introduction

Lookout.js (WIP)

A tiny observable library in JS using native ES5 getters and setters. 541 bytes minified and gzipped.

This library is a work in progress. Currently the observable functionality is limited to the first 'level' of an object, meaning nested properties do not have discrete watch functionality. It's up for debate whether it should support nesting of properties or not.

Todo

  1. Add test.
  2. Add/remove functionality?
  • Since the objects are probably used to store state and value data, the need for this may be out of scope, since a user should simply define those properties when creating the Lookout object.

Getting Started

Lookout is packaged with UMD, so you can include it via require() or as a script tag in your markup.

npm i lookoutjs
var lookout = require('lookoutjs');

API

The library is lazy, i.e. it won't build listener queues or fire any callbacks until they are defined.

Simply pass the object you want to observe to Lookout, and then call watch() on a property to define a callback function to fire when that value changes.

// Create observable object
var user = lookout({
  firstName: 'Eric',
  lastName: 'Bailey',
  meta: {
    birthday: '1992-02-14',
    height: 73
  }
});

// Assign callbacks to keys in the object
user.watch('firstName', function(val){
  console.log('First name changed to '+val);
});
user.watch('firstName', function(val){
  console.log('You can assign more than one listener callback function to a value');
});
user.watch('meta', function(val){
  console.log('Meta data updated!');
});

// Change key values
user.firstName = 'Ryan';
user.meta = {
  birthday: '1990-05-08',
  height: 72
};
user.meta.height = 65;

// Result
// 'First name changed to Ryan'
// 'You can assign more than...'
// 'Meta data updated!'
// 'Meta data updated!'

lookoutjs's People

Contributors

estrattonbailey avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.