##// END OF EJS Templates
Using $.proxy to clean up callbacks.
Brian E. Granger -
Show More
@@ -28,22 +28,22 b' var IPython = (function (IPython) {'
28 28 return msg;
29 29 }
30 30
31 Kernel.prototype.start_kernel = function (callback, context) {
31 Kernel.prototype.start_kernel = function (callback) {
32 32 var that = this;
33 33 $.post(this.base_url,
34 34 function (kernel_id) {
35 that._handle_start_kernel(kernel_id, callback, context);
35 that._handle_start_kernel(kernel_id, callback);
36 36 },
37 37 'json'
38 38 );
39 39 };
40 40
41 41
42 Kernel.prototype._handle_start_kernel = function (kernel_id, callback, context) {
42 Kernel.prototype._handle_start_kernel = function (kernel_id, callback) {
43 43 this.kernel_id = kernel_id;
44 44 this.kernel_url = this.base_url + "/" + this.kernel_id;
45 45 this._start_channels();
46 callback.call(context);
46 callback();
47 47 };
48 48
49 49
@@ -367,50 +367,53 b' var IPython = (function (IPython) {'
367 367
368 368 Notebook.prototype.start_kernel = function () {
369 369 this.kernel = new IPython.Kernel();
370 this.kernel.start_kernel(this._kernel_started, this);
370 this.kernel.start_kernel($.proxy(this.kernel_started, this));
371 371 };
372 372
373 373
374 Notebook.prototype._kernel_started = function () {
375 console.log("Kernel started: ", this.kernel.kernel_id);
376 var that = this;
374 Notebook.prototype.handle_shell_reply = function (e) {
375 reply = $.parseJSON(e.data);
376 // console.log(reply);
377 var msg_type = reply.header.msg_type;
378 var cell = this.cell_for_msg(reply.parent_header.msg_id);
379 if (msg_type === "execute_reply") {
380 cell.set_input_prompt(reply.content.execution_count);
381 };
382 };
383
377 384
378 this.kernel.shell_channel.onmessage = function (e) {
379 reply = $.parseJSON(e.data);
380 // console.log(reply);
381 var msg_type = reply.header.msg_type;
382 var cell = that.cell_for_msg(reply.parent_header.msg_id);
383 if (msg_type === "execute_reply") {
384 cell.set_input_prompt(reply.content.execution_count);
385 Notebook.prototype.handle_iopub_reply = function (e) {
386 reply = $.parseJSON(e.data);
387 var content = reply.content;
388 // console.log(reply);
389 var msg_type = reply.header.msg_type;
390 var cell = this.cell_for_msg(reply.parent_header.msg_id);
391 if (msg_type === "stream") {
392 cell.expand();
393 cell.append_stream(content.data + "\n");
394 } else if (msg_type === "display_data") {
395 cell.expand();
396 cell.append_display_data(content.data);
397 } else if (msg_type === "pyout") {
398 cell.expand();
399 cell.append_pyout(content.data, content.execution_count)
400 } else if (msg_type === "pyerr") {
401 cell.expand();
402 cell.append_pyerr(content.ename, content.evalue, content.traceback);
403 } else if (msg_type === "status") {
404 if (content.execution_state === "busy") {
405 this.kernel.status_busy();
406 } else if (content.execution_state === "idle") {
407 this.kernel.status_idle();
385 408 };
386 };
409 }
410 };
387 411
388 this.kernel.iopub_channel.onmessage = function (e) {
389 reply = $.parseJSON(e.data);
390 var content = reply.content;
391 // console.log(reply);
392 var msg_type = reply.header.msg_type;
393 var cell = that.cell_for_msg(reply.parent_header.msg_id);
394 if (msg_type === "stream") {
395 cell.expand();
396 cell.append_stream(content.data + "\n");
397 } else if (msg_type === "display_data") {
398 cell.expand();
399 cell.append_display_data(content.data);
400 } else if (msg_type === "pyout") {
401 cell.expand();
402 cell.append_pyout(content.data, content.execution_count)
403 } else if (msg_type === "pyerr") {
404 cell.expand();
405 cell.append_pyerr(content.ename, content.evalue, content.traceback);
406 } else if (msg_type === "status") {
407 if (content.execution_state === "busy") {
408 that.kernel.status_busy();
409 } else if (content.execution_state === "idle") {
410 that.kernel.status_idle();
411 };
412 }
413 };
412
413 Notebook.prototype.kernel_started = function () {
414 console.log("Kernel started: ", this.kernel.kernel_id);
415 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
416 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
414 417 };
415 418
416 419
General Comments 0
You need to be logged in to leave comments. Login now