From 1365e63ff8c72506d3c0cc3a2fcc1f989aeff2ca Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Mon, 9 Jun 2014 12:18:48 -0700 Subject: [PATCH] Start to make custom block helpers work UI.contentBlock --- packages/spacebars-compiler/codegen.js | 4 ++-- packages/spacebars-compiler/compiler.js | 9 --------- packages/templating/scanner_tests.js | 2 +- packages/ui/template.js | 27 ++++++++++++++++--------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/spacebars-compiler/codegen.js b/packages/spacebars-compiler/codegen.js index 9f04aa1a5e..91eabb6197 100644 --- a/packages/spacebars-compiler/codegen.js +++ b/packages/spacebars-compiler/codegen.js @@ -23,8 +23,8 @@ var builtInUIPaths = { // function for the template in which `UI.contentBlock` (or // `UI.elseBlock`) is invoked. `template` is a reference to the // template itself. - 'contentBlock': 'template.__content', - 'elseBlock': 'template.__elseContent', + 'contentBlock': 'self.__contentBlock', + 'elseBlock': 'self.__elseBlock', // `Template` is the global template namespace. If you define a // template named `foo` in Spacebars, it gets defined as diff --git a/packages/spacebars-compiler/compiler.js b/packages/spacebars-compiler/compiler.js index 35d6c5fbdf..d9949b1fb9 100644 --- a/packages/spacebars-compiler/compiler.js +++ b/packages/spacebars-compiler/compiler.js @@ -49,16 +49,7 @@ SpacebarsCompiler.codeGen = function (parseTree, options) { {codegen: codegen})).visit(tree); var code = '(function () { '; - if (isTemplate) { - // support `{{> UI.contentBlock}}` and `{{> UI.elseBlock}}` with - // lexical scope by creating a local variable in the - // template's render function. - code += 'var template = this; '; - } if (isTemplate || isBody) { - // XXX This should replace `var template` and `var self` and become - // `var self`. When we're compiling a render method for a component, - // there is a "this," but otherwise, there isn't. code += 'var self = this; '; } code += 'return '; diff --git a/packages/templating/scanner_tests.js b/packages/templating/scanner_tests.js index 19d3eb98f5..caf10fd57a 100644 --- a/packages/templating/scanner_tests.js +++ b/packages/templating/scanner_tests.js @@ -31,7 +31,7 @@ Tinytest.add("templating - html scanner", function (test) { // arguments are quoted strings like '"hello"' var simpleTemplate = function (templateName, content) { - return '\nTemplate.__define__(' + templateName + ', (function() {\n var template = this;\n var self = this;\n return ' + content + ';\n}));\n'; + return '\nTemplate.__define__(' + templateName + ', (function() {\n var self = this;\n return ' + content + ';\n}));\n'; }; var checkResults = function(results, expectJs, expectHead) { diff --git a/packages/ui/template.js b/packages/ui/template.js index df89b5999d..9a0f084a28 100644 --- a/packages/ui/template.js +++ b/packages/ui/template.js @@ -37,17 +37,24 @@ UI.TemplateComponent = Blaze.Component.extend({ constructor: function (dataFunc, contentFunc, elseFunc) { UI.TemplateComponent.__super__.constructor.call(this); - if (dataFunc) - this.dataFunc = dataFunc; - if (contentFunc) - this.contentFunc = contentFunc; - if (elseFunc) - this.elseFunc = elseFunc; + if (dataFunc) { + this.__dataFunc = dataFunc; + } + if (contentFunc) { + this.__contentBlock = Blaze.Component.extend({ + render: function () { return contentFunc(); } + }).prototype; + } + if (elseFunc) { + this.__elseBlock = Blaze.Component.extend({ + render: function () { return elseFunc(); } + }).prototype; + } }, render: function () { var self = this; - if (self.dataFunc) { - return Blaze.With(self.dataFunc, function () { + if (self.__dataFunc) { + return Blaze.With(self.__dataFunc, function () { return self.renderTemplate(); }); } else { @@ -105,5 +112,7 @@ UI.TemplateComponent = Blaze.Component.extend({ extend: function () { throw new Error( "Component#extend was part of a private API that has been removed"); - } + }, + __contentBlock: null, + __elseBlock: null });