dmolin / tash

All Purpose Pub/Sub Javascript Utility Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tash

A Publish/Subscribe common JavaScript Library

Tash is a small, simple Pub/Sub Javascript library, defining a set of namespaced functions, useful for enhancing loosely coupled communication between reactive components. With Tash, objects communicate publishing messages and reacting to messages sent by other objects. Its intent is to group in a reusable library the basic patterns and functions that should be the base of any Javascript application.

Tash offers:

  • A namespacing system (module pattern)
  • A publish/subscribe system (synchronous and asynchronous)
  • other common utilities under development

Targeted platforms

Tash currently targets the following platforms:

  • Microsoft Internet Explorer for Windows, version 7.0 and higher
  • Mozilla Firefox
  • Apple Safari
  • Opera 11 and higher
  • Chrome

Note to the reader

This library is still in development and currently in version 0.0.1. I've still to add aggregation/minification support and normalize the code. The library, as it is now, is not yet ready to be used in production systems but only for personal or experimental use.

Using Tash

To use Tash in your application, download the latest release and copy dist/tash-0.0.1[-min].js to a suitable location. Then include it in your HTML like so:

<script type="text/javascript" src="/path/to/tash-0.0.1-min.js"></script>

publishing/suscribing events

Publishing/Subscribing represents the main feature of this library. In essence, it allows you to build extremely loosely coupled components, capable of emitting events and reacting to events generated by other components in the system. Think about an Ajax call that retrieves some kind of data and then publish an event, allowing all the views in the page to automatically be notified and update with the newly retrieved values; this is a no-brainer using a Pub/Sub approach.

Publishing/Subscribing is done relative to a namespace. In order to publish/subscribe, it's only necessary:

  • to declare what event name we want to use (namespaced if we want)
  • publish or subscribe

Requiring an event before usage

In our page, first we have to declare what event are we going to deal with:

tash.events.require( "eventName" );

The name of the event can be namespaced, using "." for separating scopes, as in:

tash.events.require( "account.UserJustLoggedIn" );

The call to the require function will 'create' the requested namespace, if not already existing (multiple calls are perfectly fine). The event name then becomes a namespace, with 3 new functions created for us:

- publish( event data );
- subscribe( callback, [scope] );
- unsubscribe( subscriptionHandle );

All the functions are automatically generated and bound to the namespace we required. So, following the previous example, after the call to require() we have:

typeof account.UserJustLoggedIn.publish     => "function"
typeof account.UserJustLoggedIn.subscribe   => "function"
typeof account.UserJustLoggedIn.unsubscribe => "function"

Publishing an Event

Publishing an event is simply a matter of calling {your new namespace}.publish( optional data ).

If we required "account.UserJustLoggedIn", we can call:

account.UserJustLoggedIn.publish();

or

account.UserJustLoggedIn.publish( <whatever data we want to publish>, ... );

This call will immediately notify all subscribers. At this time, the event system is synchronous, thus at the end of this call all the subscribers will've been notified (an Asynchronous interface is presently under development).

Subscribing to an Event

Subscribing is really that simple: just require the namespace (if not already done) and then call its own subscribe method:

var subscription = account.UserJustLoggedIn.subscribe( function( <arguments expected to be received from the corresponding publish call> ) {
    //do what we want...
})

UnSubscribing from an Event

Unsubscribing is even simpler:

account.UserJustLoggedIn.unsubscribe( subscription );

About

All Purpose Pub/Sub Javascript Utility Library


Languages

Language:JavaScript 99.8%Language:Shell 0.2%