From f362f949afbb8b2a67fdd5e9539effe0a8963bfc Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Fri, 15 Aug 2014 16:17:23 -0700 Subject: [PATCH] UI.data() / UI.data(view) / UI.data(element) Question: Keep UI.getElementData in docs, or just for back-compat? --- docs/client/api.js | 2 +- packages/blaze/lookup.js | 4 +-- packages/blaze/template.js | 4 +-- packages/blaze/view.js | 36 ++++++++++++++-------- packages/spacebars-tests/template_tests.js | 3 +- packages/ui/render_tests.js | 8 ++--- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/docs/client/api.js b/docs/client/api.js index 1e32196838..4e3acbd1a6 100644 --- a/docs/client/api.js +++ b/docs/client/api.js @@ -1985,7 +1985,7 @@ Template.api.ui_view = { descr: "Optional. A name for this type of View. See [`view.name`](#view_name)."}, {name: "renderFunction", type: "Function", - descr: "A function that returns [*render content*](#render_content). In this function, `this` is bound to the View." + descr: "A function that returns [*renderable content*](#renderable_content). In this function, `this` is bound to the View." } ] }; diff --git a/packages/blaze/lookup.js b/packages/blaze/lookup.js index ef6f5c3b6b..bb7288a38f 100644 --- a/packages/blaze/lookup.js +++ b/packages/blaze/lookup.js @@ -18,7 +18,7 @@ var bindIfIsFunction = function (x, target) { var bindDataContext = function (x) { if (typeof x === 'function') { return function () { - var data = Blaze.getCurrentData(); + var data = Blaze.data(); if (data == null) data = {}; return x.apply(data, arguments); @@ -67,7 +67,7 @@ Blaze.View.prototype.lookup = function (name, _options) { } else { return function () { var isCalledAsFunction = (arguments.length > 0); - var data = Blaze.getCurrentData(); + var data = Blaze.data(); if (lookupTemplate && ! (data && data[name])) { throw new Error("No such template: " + name); } diff --git a/packages/blaze/template.js b/packages/blaze/template.js index ba7068811e..1c3a71c2c7 100644 --- a/packages/blaze/template.js +++ b/packages/blaze/template.js @@ -68,7 +68,7 @@ Template.prototype.constructView = function (contentFunc, elseFunc) { // object. var inst = view._templateInstance; - inst.data = Blaze.getViewData(view); + inst.data = Blaze.data(view); if (view._domrange && !view.isDestroyed) { inst.firstNode = view._domrange.firstNode(); @@ -150,7 +150,7 @@ Template.prototype.events = function (eventMap) { eventMap2[k] = (function (k, v) { return function (event/*, ...*/) { var view = this; // passed by EventAugmenter - var data = Blaze.getElementData(event.currentTarget); + var data = Blaze.data(event.currentTarget); if (data == null) data = {}; var args = Array.prototype.slice.call(arguments); diff --git a/packages/blaze/view.js b/packages/blaze/view.js index aa87e9b8ee..89516cb366 100644 --- a/packages/blaze/view.js +++ b/packages/blaze/view.js @@ -547,11 +547,33 @@ Blaze._toText = function (htmljs, parentView, textMode) { return HTML.toText(Blaze._expand(htmljs, parentView), textMode); }; -Blaze.getCurrentData = function () { - var theWith = Blaze.getCurrentView('with'); +Blaze.data = function (elementOrView) { + var theWith; + if (! elementOrView) { + theWith = Blaze.getCurrentView('with'); + } else if (elementOrView instanceof Blaze.View) { + var view = elementOrView; + theWith = (view.name === 'with' ? view : + Blaze.getParentView(view, 'with')); + } else if (typeof elementOrView.nodeType === 'number') { + if (elementOrView.nodeType !== 1) + throw new Error("Expected DOM element"); + theWith = Blaze.getElementView(elementOrView, 'with'); + } else { + throw new Error("Expected DOM element or View"); + } + return theWith ? theWith.dataVar.get() : null; }; +// For back-compat +Blaze.getElementData = function (element) { + if (element.nodeType !== 1) + throw new Error("Expected DOM element"); + + return Blaze.data(element); +}; + // Gets the current view or its nearest ancestor of name // `name`. Blaze.getCurrentView = function (name) { @@ -607,16 +629,6 @@ Blaze.getElementView = function (elem, name) { } }; -Blaze.getElementData = function (elem) { - var theWith = Blaze.getElementView(elem, 'with'); - return theWith ? theWith.dataVar.get() : null; -}; - -Blaze.getViewData = function (view) { - var theWith = Blaze.getParentView(view, 'with'); - return theWith ? theWith.dataVar.get() : null; -}; - Blaze._addEventMap = function (view, eventMap, thisInHandler) { thisInHandler = (thisInHandler || null); var handles = []; diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index b855943a41..b8efaad75e 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -2419,6 +2419,7 @@ Tinytest.add( var span = div.querySelector('SPAN'); test.isTrue(span); test.equal(UI.getElementData(span), {foo: "bar"}); + test.equal(UI.data(span), {foo: "bar"}); }); Tinytest.add( @@ -2616,7 +2617,7 @@ Tinytest.add('spacebars-tests - template_tests - current view in event handler', tmpl.events({ 'click span': function () { currentView = Blaze.getCurrentView(); - currentData = Blaze.getCurrentData(); + currentData = Blaze.data(); } }); diff --git a/packages/ui/render_tests.js b/packages/ui/render_tests.js index 674fddca92..44906ecd35 100644 --- a/packages/ui/render_tests.js +++ b/packages/ui/render_tests.js @@ -484,7 +484,7 @@ Tinytest.add("ui - render - templates and views", function (test) { parent.number); } - buf.push('created ' + Blaze.getCurrentData()); + buf.push('created ' + UI.data()); }; myTemplate.rendered = function () { @@ -507,19 +507,19 @@ Tinytest.add("ui - render - templates and views", function (test) { while (end !== start && ! nodeDescr(end)) end = end.previousSibling; - buf.push('dom-' + Blaze.getCurrentData() + + buf.push('dom-' + UI.data() + ' is ' + nodeDescr(start) +'..' + nodeDescr(end)); }; myTemplate.destroyed = function () { test.isFalse(Deps.active); - buf.push('destroyed ' + Blaze.getCurrentData()); + buf.push('destroyed ' + UI.data()); }; var makeView = function () { var number = counter++; - return Blaze.With(number, function () { + return UI.With(number, function () { return myTemplate.constructView(number); }); };