From 6ef51852c37fc6ba2d1e8ef6cc889b019be09e5c Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Wed, 12 Jun 2013 15:57:13 -0700 Subject: [PATCH] begin attempt at Component/template joint def --- packages/ui/component.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/ui/component.js b/packages/ui/component.js index f512f6353f..87817e22d6 100644 --- a/packages/ui/component.js +++ b/packages/ui/component.js @@ -1,5 +1,22 @@ +// @export UI +UI = { + isComponentClass: function (value) { + return (typeof value === 'function') && + (value === Component || + (value.protoype instanceof Component)); + }, + // Generated templates are put here. This object must + // be populated before any Components are actually + // created, i.e. as the compiled template scripts are + // evaluated. + _templates: {} +}; + var constructorsLocked = true; +var templatesAssigned = false; +var global = (function () { return this; })(); + // @export Component Component = function (args) { if (constructorsLocked) @@ -30,6 +47,19 @@ Component = function (args) { this._childUpdaters = {}; this.elements = {}; + if (! templatesAssigned) { + _.each(UI._templates, function (v, k) { + if (UI.isComponentClass(global[k])) { + if (k.prototype.hasOwnProperty('render')) + throw new Error( + 'Component "' + k + '" has both a render method ' + + 'implementation and a template of that name'); + k.prototype.render = v; + } + }); + templatesAssigned = true; + } + this.constructed(); };