From 20a67449f56616b4b3024870af8a226233bde9c0 Mon Sep 17 00:00:00 2001 From: Alexander Gugel Date: Thu, 16 Jul 2015 16:47:06 +0200 Subject: [PATCH] refact: Simplify and make Scene more flexible The `FamousEngine` is supposed to expose an API for adding and removing scenes. While the corresponding methods have been outlined, the Scene class itself doesn't reflect the ability to re-mount the scene to a different selector. * The Scene constructor accepts an updater and a selector * Scene#_globalUpdater is inconsistent with the inherited, Node-specific (theoretically) Node#_updater * The Scene updater is being set as via the constructor (instead of Node#_setUpdater) * The Scene constructor requests the context size * The scene constructor no longer accepts any arguments * The scene itself is not bound to a specific selector, but instead can be mounted to an arbitrary path (just like nodes) * Scene constructor logic has been moved to Scene#mount * Less redundancy * More flexibility: Scene can request size at a later point in time instead of upon instantiation * Scene constructor does not have side effects * Scene updater (Scene#_globalUpdater) can be changed via Dispatch * => FamousEngine#removeScene and FamousEngine#addScene can be implemented --- core/FamousEngine.js | 7 +++++-- core/Scene.js | 43 ++++++++++++------------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/core/FamousEngine.js b/core/FamousEngine.js index 1068e4f0..0fc4caf7 100644 --- a/core/FamousEngine.js +++ b/core/FamousEngine.js @@ -363,8 +363,11 @@ FamousEngine.prototype.createScene = function createScene (selector) { selector = selector || 'body'; if (this._scenes[selector]) this._scenes[selector].dismount(); - this._scenes[selector] = new Scene(selector, this); - return this._scenes[selector]; + + var scene = new Scene(); + this._scenes[selector] = scene; + scene.mount(selector); + return scene; }; /** diff --git a/core/Scene.js b/core/Scene.js index d63a8a05..058c1cbe 100644 --- a/core/Scene.js +++ b/core/Scene.js @@ -39,33 +39,9 @@ var SizeSystem = require('./SizeSystem'); * @class Scene * @constructor * @extends Node - * - * @param {String} selector a string which is a dom selector - * signifying which dom element the context - * should be set upon - * @param {Famous} updater a class which conforms to Famous' interface - * it needs to be able to send methods to - * the renderers and update nodes in the scene graph */ -function Scene (selector, updater) { - if (!selector) throw new Error('Scene needs to be created with a DOM selector'); - if (!updater) throw new Error('Scene needs to be created with a class like Famous'); - - Node.call(this); // Scene inherits from node - - this._globalUpdater = updater; // The updater that will both - // send messages to the renderers - // and update dirty nodes - - this._selector = selector; // reference to the DOM selector - // that represents the element - // in the dom that this context - // inhabits - - this.mount(selector); // Mount the context to itself - // (it is its own parent) - - this.show(); // the context begins shown (it's already present in the dom) +function Scene () { + Node.call(this); } // Scene inherits from node @@ -79,7 +55,8 @@ Scene.NO_DEFAULT_COMPONENTS = true; * @return {String} dom selector */ Scene.prototype.getSelector = function getSelector () { - return this._selector; + console.warn('Scene#getSelector is deprecated, use Scene#getLocation or Scene#getId instead'); + return this._id; }; /** @@ -121,7 +98,7 @@ Scene.prototype.onReceive = function onReceive (event, payload) { payload[1], payload[2] ? payload[2] : 0); - this._updater.message(Commands.WITH).message(this._selector).message(Commands.READY); + this._updater.message(Commands.WITH).message(this._id).message(Commands.READY); } }; @@ -130,16 +107,20 @@ Scene.prototype.mount = function mount (path) { if (this.isMounted()) throw new Error('Scene is already mounted at: ' + this.getLocation()); - this._globalUpdater // message a request for the dom + Dispatch.mount(path, this); + + this._updater // message a request for the dom .message(Commands.NEED_SIZE_FOR) // size of the context so that - .message(this._selector); // the scene graph has a total size + .message(path); // the scene graph has a total size - Dispatch.mount(path, this); this._id = path; this._mounted = true; this._parent = this; TransformSystem.registerTransformAtPath(path); SizeSystem.registerSizeAtPath(path); + + // the context begins shown (it's already present in the dom) + this.show(); }; module.exports = Scene;