diff --git a/lib/widgets/node.js b/lib/widgets/node.js index 7125c7a..77f77f0 100644 --- a/lib/widgets/node.js +++ b/lib/widgets/node.js @@ -15,6 +15,7 @@ var EventEmitter = require('../events').EventEmitter; */ function Node(options) { + var self = this; var Screen = require('./screen'); if (!(this instanceof Node)) { @@ -38,9 +39,20 @@ function Node(options) { while (this.screen && this.screen.type !== 'screen') { this.screen = this.screen.parent; } - } else if (Screen.global) { - // Should never happen: - this.screen = Screen.global; + } else if (Screen.total) { + // This _should_ work in most cases as long as the element is appended + // synchronously after the screen's creation. Throw error if not. + this.screen = Screen.instances[Screen.instances.length - 1]; + process.nextTick(function() { + if (!self.parent) { + throw new Error('Element (' + self.type + ')' + + ' was not appended synchronously after the' + + ' screen\'s creation. Please set a `parent`' + + ' or `screen` option in the element\'s constructor' + + ' if you are going to use multiple screens and' + + ' append the element later.'); + } + }); } else { throw new Error('No active screen.'); } @@ -72,6 +84,10 @@ Node.prototype.type = 'node'; Node.prototype.insert = function(element, i) { var self = this; + if (element.screen && element.screen !== this.screen) { + throw new Error('Cannot switch a node\'s screen.'); + } + element.detach(); element.parent = this; element.screen = this.screen;