Junction Tips and Tricks
Debugging
There are two important tips to help debug your Junction application. First, don't forget that Junction uses XMPP's Multi-User Chat profile for communication. So, using a standard XMPP client, you can join the room and send/receive messages to your activity.
Second, you can force your activity to be in a fixed session in one of two ways: Either use a URI to bind your actor to a Junction activity, or specify a session ID on your Activity Script:
var ascript = {ad: "my.demo.app", ... } ascript.sessionID = "fixedDebugSession";
Javascript
Often, the structure of your Junction application may take you down the path of writing something like the following:
var actor = {}; actor.onMessageReceived = function(msg,header) { if (msg.action == "update") { // do something } if (msg.action == "foo") { // do something else } if (msg.action == "bar") { // do something more } };For larger apps, this can make your code pretty difficult to maintain.
One alternative is to do something like the following:
function MyActor() { this.onUpdate = function(msg,header) { // do something } this.onFoo = function(msg,header) { // do something else } this.onBar = function(msg,header) { // do something more } this.onMessageReceived = function(msg,header) { if (msg.action in this.registry) { this.registry[msg.action](msg,header); } }; this.registry = {}; this.registry["update"] = this.onUpdate; this.registry["foo"] = this.onFoo; this.registry["bar"] = this.onBar; }
There are countless ways to be creative with metaprogramming, and we'd love to hear your solution.