From 73126bf5f12f24eb61516f7312668db9d15aae8e 2011-07-21 03:42:35 From: Brian E. Granger Date: 2011-07-21 03:42:35 Subject: [PATCH] Using $.proxy to clean up callbacks. --- diff --git a/IPython/frontend/html/notebook/static/js/kernel.js b/IPython/frontend/html/notebook/static/js/kernel.js index ebde1d0..b64c461 100644 --- a/IPython/frontend/html/notebook/static/js/kernel.js +++ b/IPython/frontend/html/notebook/static/js/kernel.js @@ -28,22 +28,22 @@ var IPython = (function (IPython) { return msg; } - Kernel.prototype.start_kernel = function (callback, context) { + Kernel.prototype.start_kernel = function (callback) { var that = this; $.post(this.base_url, function (kernel_id) { - that._handle_start_kernel(kernel_id, callback, context); + that._handle_start_kernel(kernel_id, callback); }, 'json' ); }; - Kernel.prototype._handle_start_kernel = function (kernel_id, callback, context) { + Kernel.prototype._handle_start_kernel = function (kernel_id, callback) { this.kernel_id = kernel_id; this.kernel_url = this.base_url + "/" + this.kernel_id; this._start_channels(); - callback.call(context); + callback(); }; diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index 2daebda..337052b 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -367,50 +367,53 @@ var IPython = (function (IPython) { Notebook.prototype.start_kernel = function () { this.kernel = new IPython.Kernel(); - this.kernel.start_kernel(this._kernel_started, this); + this.kernel.start_kernel($.proxy(this.kernel_started, this)); }; - Notebook.prototype._kernel_started = function () { - console.log("Kernel started: ", this.kernel.kernel_id); - var that = this; + Notebook.prototype.handle_shell_reply = function (e) { + reply = $.parseJSON(e.data); + // console.log(reply); + var msg_type = reply.header.msg_type; + var cell = this.cell_for_msg(reply.parent_header.msg_id); + if (msg_type === "execute_reply") { + cell.set_input_prompt(reply.content.execution_count); + }; + }; + - this.kernel.shell_channel.onmessage = function (e) { - reply = $.parseJSON(e.data); - // console.log(reply); - var msg_type = reply.header.msg_type; - var cell = that.cell_for_msg(reply.parent_header.msg_id); - if (msg_type === "execute_reply") { - cell.set_input_prompt(reply.content.execution_count); + Notebook.prototype.handle_iopub_reply = function (e) { + reply = $.parseJSON(e.data); + var content = reply.content; + // console.log(reply); + var msg_type = reply.header.msg_type; + var cell = this.cell_for_msg(reply.parent_header.msg_id); + if (msg_type === "stream") { + cell.expand(); + cell.append_stream(content.data + "\n"); + } else if (msg_type === "display_data") { + cell.expand(); + cell.append_display_data(content.data); + } else if (msg_type === "pyout") { + cell.expand(); + cell.append_pyout(content.data, content.execution_count) + } else if (msg_type === "pyerr") { + cell.expand(); + cell.append_pyerr(content.ename, content.evalue, content.traceback); + } else if (msg_type === "status") { + if (content.execution_state === "busy") { + this.kernel.status_busy(); + } else if (content.execution_state === "idle") { + this.kernel.status_idle(); }; - }; + } + }; - this.kernel.iopub_channel.onmessage = function (e) { - reply = $.parseJSON(e.data); - var content = reply.content; - // console.log(reply); - var msg_type = reply.header.msg_type; - var cell = that.cell_for_msg(reply.parent_header.msg_id); - if (msg_type === "stream") { - cell.expand(); - cell.append_stream(content.data + "\n"); - } else if (msg_type === "display_data") { - cell.expand(); - cell.append_display_data(content.data); - } else if (msg_type === "pyout") { - cell.expand(); - cell.append_pyout(content.data, content.execution_count) - } else if (msg_type === "pyerr") { - cell.expand(); - cell.append_pyerr(content.ename, content.evalue, content.traceback); - } else if (msg_type === "status") { - if (content.execution_state === "busy") { - that.kernel.status_busy(); - } else if (content.execution_state === "idle") { - that.kernel.status_idle(); - }; - } - }; + + Notebook.prototype.kernel_started = function () { + console.log("Kernel started: ", this.kernel.kernel_id); + this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this); + this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this); };