diff --git a/notebook/static/base/js/promises.js b/notebook/static/base/js/promises.js new file mode 100644 index 0000000000..7513c6d158 --- /dev/null +++ b/notebook/static/base/js/promises.js @@ -0,0 +1,32 @@ +// Copyright (c) Jupyter Development Team. +// Distributed under the terms of the Modified BSD License. + +// Give us an object to bind all events to. This object should be created +// before all other objects so it exists when others register event handlers. +// To register an event handler: +// +// require(['base/js/events'], function (events) { +// events.on("event.Namespace", function () { do_stuff(); }); +// }); + +define(['base/js/events', 'base/js/namespace'], function(events, Jupyter) { + "use strict"; + + // Promise to be resolved when the application is initialized. + // The value is the name of the app on the current page. + var app_initialized = new Promise(function(resolve, reject) { + events.on('app_initialized.NotebookApp', function() { + resolve('NotebookApp'); + }); + events.on('app_initialized.DashboardApp', function() { + resolve('DashboardApp'); + }); + }); + + var promises = { + app_initialized: app_initialized + }; + Jupyter.promises = promises; + + return promises; +}); diff --git a/notebook/static/custom/custom.js b/notebook/static/custom/custom.js index 17b06f49da..4af700f6c5 100644 --- a/notebook/static/custom/custom.js +++ b/notebook/static/custom/custom.js @@ -20,9 +20,10 @@ * Instances are created after the loading of this file and might need to be accessed using events: * define([ * 'base/js/namespace', - * 'base/js/events' - * ], function(IPython, events) { - * events.on("app_initialized.NotebookApp", function () { + * 'base/js/promises' + * ], function(IPython, promises) { + * promises.app_initialized.then(function (appName) { + * if (appName !== 'NotebookApp') return; * IPython.keyboard_manager.... * }); * }); @@ -34,9 +35,10 @@ * * define([ * 'base/js/namespace', - * 'base/js/events' - * ], function(IPython, events) { - * events.on('app_initialized.NotebookApp', function(){ + * 'base/js/promises' + * ], function(IPython, promises) { + * promises.app_initialized.then(function (appName) { + * if (appName !== 'NotebookApp') return; * IPython.toolbar.add_buttons_group([ * { * 'label' : 'run qtconsole', diff --git a/notebook/static/notebook/js/main.js b/notebook/static/notebook/js/main.js index fec68fd708..213bc9c1bd 100644 --- a/notebook/static/notebook/js/main.js +++ b/notebook/static/notebook/js/main.js @@ -29,6 +29,7 @@ require([ 'base/js/utils', 'base/js/page', 'base/js/events', + 'base/js/promises', 'auth/js/loginwidget', 'notebook/js/maintoolbar', 'notebook/js/pager', @@ -52,6 +53,7 @@ require([ utils, page, events, + promises, loginwidget, maintoolbar, pager, diff --git a/notebook/static/tree/js/main.js b/notebook/static/tree/js/main.js index 40580c2f32..c8c9abda23 100644 --- a/notebook/static/tree/js/main.js +++ b/notebook/static/tree/js/main.js @@ -26,6 +26,7 @@ require([ 'base/js/namespace', 'base/js/dialog', 'base/js/events', + 'base/js/promises', 'base/js/page', 'base/js/utils', 'services/config', @@ -41,6 +42,7 @@ require([ IPython, dialog, events, + promises, page, utils, config,