diff --git a/packages/blaze/builtins.js b/packages/blaze/builtins.js index 9ec9b26952..61d984aa38 100644 --- a/packages/blaze/builtins.js +++ b/packages/blaze/builtins.js @@ -180,7 +180,7 @@ Blaze._TemplateWith = function (arg, contentBlock) { // Longer term, evaluate expressions in the proper lexical scope. var wrappedArgFunc = function () { var viewToEvaluateArg = null; - if (w.parentView && w.parentView.kind === 'InOuterTemplateScope') { + if (w.parentView && w.parentView.name === 'InOuterTemplateScope') { viewToEvaluateArg = w.parentView.originalParentView; } if (viewToEvaluateArg) { diff --git a/packages/blaze/template.js b/packages/blaze/template.js index bb6af83011..567ce545ff 100644 --- a/packages/blaze/template.js +++ b/packages/blaze/template.js @@ -1,20 +1,27 @@ -// # TODO +// [new] Blaze.Template([viewName], renderFunction) // -// Should be an actual helpers dict, so you can in theory name a -// helper anything. Test that you can. -// -// Finish adding things to UI. Take it out of the "templating" package. -// Merge Blaze and UI symbols? - - // `Blaze.Template` is the class of templates, like `Template.foo` in // Meteor, which is `instanceof Template`. // // `viewKind` is a string that looks like "Template.foo" for templates // defined by the compiler. -Blaze.Template = function (viewKind, viewRenderFunc) { - this.__kind = viewKind; - this.__render = viewRenderFunc; +Blaze.Template = function (viewName, renderFunction) { + if (! (this instanceof Blaze.Template)) + // called without `new` + return new Blaze.Template(viewName, renderFunction); + + if (typeof viewName === 'function') { + // omitted "viewName" argument + renderFunction = viewName; + viewName = ''; + } + if (typeof viewName !== 'string') + throw new Error("viewName must be a String (or omitted)"); + if (typeof renderFunction !== 'function') + throw new Error("renderFunction must be a function"); + + this.viewName = viewName; + this.renderFunction = renderFunction; this.__eventMaps = []; }; @@ -26,7 +33,7 @@ Blaze.isTemplate = function (t) { Template.prototype.constructView = function (contentFunc, elseFunc) { var self = this; - var view = Blaze.View(self.__kind, self.__render); + var view = Blaze.View(self.viewName, self.renderFunction); view.template = self; view.templateContentBlock = ( @@ -77,7 +84,7 @@ Template.prototype.constructView = function (contentFunc, elseFunc) { }; Template.updateTemplateInstance = function (view) { - // Populate `view.templateInstance.{firstNode,lastNode,data}` + // Populate `view._templateInstance.{firstNode,lastNode,data}` // on demand. var tmpl = view._templateInstance; if (! tmpl) { diff --git a/packages/blaze/view.js b/packages/blaze/view.js index 9a247ddf23..ac042b21d6 100644 --- a/packages/blaze/view.js +++ b/packages/blaze/view.js @@ -1,4 +1,4 @@ -/// [new] Blaze.View([kind], renderMethod) +/// [new] Blaze.View([name], renderMethod) /// /// Blaze.View is the building block of reactive DOM. Views have /// the following features: @@ -26,23 +26,23 @@ /// /// ...more lifecycle stuff /// -/// `kind` is an optional string tag identifying the View. The only +/// `name` is an optional string tag identifying the View. The only /// time it's used is when looking in the View tree for a View of a -/// particular kind; for example, data contexts are stored on Views -/// of kind "with". Kinds are also useful when debugging, so in -/// general it's good for functions that create Views to set the kind. -/// Templates have kinds of the form "Template.foo". -Blaze.View = function (kind, render) { +/// particular name; for example, data contexts are stored on Views +/// of name "with". Names are also useful when debugging, so in +/// general it's good for functions that create Views to set the name. +/// Views associated with templates have names of the form "Template.foo". +Blaze.View = function (name, render) { if (! (this instanceof Blaze.View)) // called without `new` - return new Blaze.View(kind, render); + return new Blaze.View(name, render); - if (typeof kind === 'function') { - // omitted "kind" argument - render = kind; - kind = ''; + if (typeof name === 'function') { + // omitted "name" argument + render = name; + name = ''; } - this.kind = kind; + this.name = name; this.render = render; this._callbacks = { @@ -534,9 +534,9 @@ Blaze.getCurrentData = function () { return theWith ? theWith.dataVar.get() : null; }; -// Gets the current view or its nearest ancestor of kind -// `kind`. -Blaze.getCurrentView = function (kind) { +// Gets the current view or its nearest ancestor of name +// `name`. +Blaze.getCurrentView = function (name) { var view = Blaze.currentView; // Better to fail in cases where it doesn't make sense // to use Blaze.getCurrentView(). There will be a current @@ -545,8 +545,8 @@ Blaze.getCurrentView = function (kind) { if (! view) throw new Error("There is no current view"); - if (kind) { - while (view && view.kind !== kind) + if (name) { + while (view && view.name !== name) view = view.parentView; return view || null; } else { @@ -566,18 +566,18 @@ Blaze.getCurrentTemplateView = function () { return view || null; }; -Blaze.getParentView = function (view, kind) { +Blaze.getParentView = function (view, name) { var v = view.parentView; - if (kind) { - while (v && v.kind !== kind) + if (name) { + while (v && v.name !== name) v = v.parentView; } return v || null; }; -Blaze.getElementView = function (elem, kind) { +Blaze.getElementView = function (elem, name) { var range = Blaze._DOMRange.forElement(elem); var view = null; while (range && ! view) { @@ -590,8 +590,8 @@ Blaze.getElementView = function (elem, kind) { } } - if (kind) { - while (view && view.kind !== kind) + if (name) { + while (view && view.name !== name) view = view.parentView; return view || null; } else { diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index 4f72c64cfe..7daca5ba08 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -229,7 +229,7 @@ Tinytest.add("spacebars-tests - template_tests - inclusion args 2", function (te // maybe use created callback on the template instead of this? var extendTemplateWithInit = function (template, initFunc) { - var tmpl = new Template(template.__kind+'-extended', template.__render); + var tmpl = new Template(template.viewName+'-extended', template.renderFunction); tmpl.constructView = function (/*args*/) { var view = Template.prototype.constructView.apply(this, arguments); initFunc(view); @@ -1173,7 +1173,7 @@ Tinytest.add('spacebars-tests - template_tests - inclusion helpers are isolated' var dep = new Deps.Dependency; var subtmpl = Template.spacebars_template_test_inclusion_helpers_are_isolated_subtemplate; // make a copy so we can set "rendered" without mutating the original - var subtmplCopy = new Template(subtmpl.__kind, subtmpl.__render); + var subtmplCopy = new Template(subtmpl.viewName, subtmpl.renderFunction); var R = new ReactiveVar(subtmplCopy); tmpl.foo = function () {