JavaScript Observable API’s

Observable objects are core part of my tool kit. Observables let you decouple objects from other objects, models from your UI for example.

I made a little library called microObservables to make normal JavaScript objects into observables. It pretty straight forward to use and you can just copy it into your project. It contains a single function observable. Just pass a reference to an object to it and it will return an observable version of your object.

function MyObject () {
var self = observable(this);
}

Now you can bind to events from that object in a similar way to how you would with jQuery.

var x = new MyObject();
x.on("yourEventName", function () {
// Do something
});

And trigger events in a jQuery like way too.

x.trigger("yourEventName");

I find that with approach I can build applications that with relatively few external components. I have models which contain my business logic, a layer of jquery or zepto to bind the models to the UI using the events triggered by the observable models, and a template library for generating the views, usually mustache.js or micro templates. This lightweight stack is perfect for mobile applications web apps or PhoneGap apps where size matters.

To contrast this to other typical front end development stacks, backbone is over 30kb minified as is jQuery, where as when using microObservables, micro templates and zepto.js a largish application comes in at around 40kb including html and css. Size isn’t everything and I still use MVC frameworks, but when I need to get every last bit of performance out of a mobile or touch device I prefer to role my own.

This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to JavaScript Observable API’s

  1. bolshchikov says:

    What’s your motivation that you decided not to use the native observable of javascript or the corresponding polyfill of Object.observe?
    https://github.com/new-proimage/Object.observe

    • Hi bolshchikov

      I considered this, but decided that as it is currently only supported in Chrome (see http://kangax.github.io/es5-compat-table/es6/#Object.observe_%28part_of_b_ES7_/b_%29) as it is part of th eECMA Script 7 standard and ECMA Script 6 isn’t fully supported yet, that I would write my own.

      I chose not to use the poyfill for now, as I like to be able to use a more fluid interface and use a more event driven approach. As you have probably noticed the API looks a lot like, jquery, which makes it familiar and easy to pick up, but also means you can link you binding calls. For example,

      var obj = new MyObject();
      obj
      .on(“add”, function () { /*..*/ })
      .on(“remove”, function () { /*..*/ })

      I feel that this is a much nicer and simpler interface, until more people become familiar with Object.observe.

      What do you think?

      Thanks
      James

  2. I’m curious if you considered using Object.defineProperty to implement the observer pattern?

    • Hi Joey

      Honestly, I didn’t consider it. I do like the idea of not making the on and off functions enumerable, but I wonder what real advantage that would give. Maybe I will have a ply around with the idea and see if I can come up with any places that it would improve or simplify a project.

      Do you have any use cases it would simplify?

      Thanks
      James

Leave a comment