Getting Started

Make sure you grab the lastest copy of Radio.js and include it in your project to get started.

Some basic samples to get the idea.

//create topic called changeTabEvent and subscribe myFunction
radio('changeTabEvent').subscribe(myfunction);

//publish to the topic changeTabEvent
radio('changeTabEvent').broadcast(data);

//unsubscribe myFunction from the topic changeTabEvent
radio('changeTabEvent').unsubscribe(myFunction);

//do all of the above in one line via chaining
radio('changeTabEvent').subscribe(myFunction).broadcast(data).unsubscribe(myFunction);

radio()

Every radio call starts with the radio() method which accepts a string to set the current topic.

//creates and selects the topic to changeTabEvent
radio('changeTabEvent');

.subscribe()

Add subscribers to a topic.

//subscribe callback
radio('changeTabEvent').subscribe(callback);
//subscribe callback with context
radio('changeTabEvent').subscribe([callback,context]);

You can subscribe multiple callbacks at once.

//subscribe three callbacks at once
radio('changeTabEvent').subscribe(callback, callback2, [callback3.init, callback3]);

//subscribe it via chaining if you want
radio('changeTabEvent').subscribe(callback).subscribe(callback2).subscribe([callback3.init, callback3]);

Callbacks with context

With Radio you can set the context of 'this' when your callback is invoked by adding your callback as an array with context. This is particularly useful if you are namespacing your project under one global object.

radio('changeTabEvent').subscribe([callback, context]);

Example that demonstrates why you may want to do this.

//your global namespaced application object
var myApp = {
	init: function() {
		alert(this);
		this.run();
	},
	run: function() {
		//do something
	}
}

//first subscribe it without setting context
//this will fail because 'this' is set to the window object
radio('changeTabEvent').subscribe(myApp.init).broadcast();

//subscribe it with context. 
//this will work because value of 'this' is set to myApp
radio('changeTabEvent').subscribe([myApp.init, myApp]).broadcast();

.broadcast()

Publish a message to all subscribers in a selected topic. All parameters get passed to the subscribers.

radio('changeTabEvent').subscribe(function(data1, data2) {
	//do something with data1 and data2
});
radio('changeTabEvent').broadcast(data1, data2);

.unsubscribe()

Remove subscribers from a topic.

//first subscribe a subscriber
radio('changeTabEvent').subscribe(callback);
//now unsubscribe it
radio('changeTabEvent').unsubscribe(callback);

Remove multiples callback at once.

//unsubscribe multiple callbacks at once
radio('changeTabEvent').unsubscribe(callback1, callback2, callback3);

Ender Integration

Radio support ender and provides an ender bridge. If you are using ender to consume Radio.js, just use the radio object like:

$.radio('changeTabEvent').subscribe(callback1).broadcast('test').unsubscribe(callback1);
More samples can be found in the integration tests (thank you to rvagg).