Look at that POJO
Observe POJOs while using POJOs like POJOs
There are plenty of libraries out there that let you maintain models and listen to change events on their properties, like Backbone's Model class. But how annoying is it having to use their "get" and "set" methods, right?
This library does away with that nonsense and lets you use your plain old javascript objects just like you used to, while still being able to listen carefully on changes at any part of the object.
The basics
var look_at_that =var pojo =some: 'plain'old: 'properties'in:a: 'plain'old:javascript: 'object'// The original POJO remains untouched// We interact with the observable object as if it was the original POJOvar observable =// For all intents and purposes, it behaves the same as the POJOJSON === JSON // true// Except that we can listen to changes in any part of the objectobservableonobservableinonobservableinon// All change events above fire when we set this property's valueobservableina = 5// Only the first two change events fire when we change this property's valueobservablein =this: 'becomes'observable: 'too'// Only the first change event fires when we set thisobservableold = 10
What works and what doesn't
var look_at_that =observable =// WORKS: Listening for a property that doesn't exist yetobservableonobservabledoes_not_exist = false// DOESN'T WORK: Changing a key that didn't exist and listening on a parent objectobservableonobservablesomething = 'not cool'// WORKS: Changing a key that didn't exist using .set() and listening on a parent objectobservableonobservable// DOESN'T WORK: Listening to a key change and changing something within the key's objectobservableonobservableobjectkey = 'a different value'// WORKS: Listening on a property that was added after creationobservableexists =new: 'object'observableexistsnewonobservableexistsnew = 'woo!'// DOESN'T WORK: Listening to a deep property, and then setting the parent object to a new objectobservableobjectonobservableobject =key: 'new value'// WORKS: Listening to a deep property, then setting the parent object deeply, retaining all instrumentation and firing all relevant eventsobservableobjectonobservableobjectset// DOESN'T WORK: Trying to listen for changes on things that aren't technically Objectsobservableexistson// WORKS: Nesting observables in observablesother_observable =observableobject = other_observableobservableonother_observableonobservableobjectother = 'something'other_observableother === 'something' // true// DOESNT WORK: Things that aren't plain old javascript objects// (this is sort of a feature... we could return an instrumented object that looks like your specialised object at face value, but it won't behave the same when you run its methods or look for properties that belong in its prototype)observablenon_pojoonobservablenon_pojo instanceof String // This is true// WORKS: Arraysobservablearrayonobservablearray // Works for any destructive array functionobservablearray // Works for any index assignmentsobservablearray0key = true // Nested objects are observable
Keeping quiet
observableonobservableset
Removing handlers
remove = observableonobservableproperty = 'value' // Change event is firedobservableproperty = 'value' // Nothing is fired
Triggering events
observableonobservableonobservable // object change triggeredobservable // key change triggered