From 2afefa06f448b4b5f678f4294b44ea498772e615 2013-10-23 02:45:39
From: MinRK <benjaminrk@gmail.com>
Date: 2013-10-23 02:45:39
Subject: [PATCH] Add CommManager.new_comm

Javascript-side version for creating and connecting Comms in one call

Without a `get_ipython()`-like global handle,
Comm constructor can't do the same magic as the IPython one.

---

diff --git a/IPython/html/static/services/kernels/js/comm.js b/IPython/html/static/services/kernels/js/comm.js
index 87bb62b..29c64b9 100644
--- a/IPython/html/static/services/kernels/js/comm.js
+++ b/IPython/html/static/services/kernels/js/comm.js
@@ -40,6 +40,15 @@ var IPython = (function (IPython) {
         }
     };
     
+    CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id) {
+        // Create a new Comm, register it, and open its Kernel-side counterpart
+        // Mimics the auto-registration in `Comm.__init__` in the IPython Comm
+        var comm = new Comm(target_name, comm_id);
+        this.register_comm(comm);
+        comm.open(data, callbacks, metadata);
+        return comm;
+    };
+    
     CommManager.prototype.register_target = function (target_name, f) {
         // Register a target function for a given target name
         this.targets[target_name] = f;
@@ -72,7 +81,7 @@ var IPython = (function (IPython) {
             console.log("Available targets are: ", this.targets);
             return;
         }
-        var comm = new Comm(content.comm_id);
+        var comm = new Comm(content.target_name, content.comm_id);
         this.register_comm(comm);
         try {
             f(comm, msg);
@@ -114,9 +123,9 @@ var IPython = (function (IPython) {
     // Comm base class
     //-----------------------------------------------------------------------
     
-    var Comm = function (comm_id, target_name) {
-        this.comm_id = comm_id || new IPython.utils.uuid();
+    var Comm = function (target_name, comm_id) {
         this.target_name = target_name;
+        this.comm_id = comm_id || IPython.utils.uuid();
         this._msg_callback = this._close_callback = null;
     };