##// 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,30 +367,27 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;
377
378 this.kernel.shell_channel.onmessage = function (e) {
374 Notebook.prototype.handle_shell_reply = function (e) {
379 375 reply = $.parseJSON(e.data);
380 376 // console.log(reply);
381 377 var msg_type = reply.header.msg_type;
382 var cell = that.cell_for_msg(reply.parent_header.msg_id);
378 var cell = this.cell_for_msg(reply.parent_header.msg_id);
383 379 if (msg_type === "execute_reply") {
384 380 cell.set_input_prompt(reply.content.execution_count);
385 381 };
386 382 };
387 383
388 this.kernel.iopub_channel.onmessage = function (e) {
384
385 Notebook.prototype.handle_iopub_reply = function (e) {
389 386 reply = $.parseJSON(e.data);
390 387 var content = reply.content;
391 388 // console.log(reply);
392 389 var msg_type = reply.header.msg_type;
393 var cell = that.cell_for_msg(reply.parent_header.msg_id);
390 var cell = this.cell_for_msg(reply.parent_header.msg_id);
394 391 if (msg_type === "stream") {
395 392 cell.expand();
396 393 cell.append_stream(content.data + "\n");
@@ -405,12 +402,18 b' var IPython = (function (IPython) {'
405 402 cell.append_pyerr(content.ename, content.evalue, content.traceback);
406 403 } else if (msg_type === "status") {
407 404 if (content.execution_state === "busy") {
408 that.kernel.status_busy();
405 this.kernel.status_busy();
409 406 } else if (content.execution_state === "idle") {
410 that.kernel.status_idle();
407 this.kernel.status_idle();
411 408 };
412 409 }
413 410 };
411
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