Skip to content

Commit f362f94

Browse files
committed
UI.data() / UI.data(view) / UI.data(element)
Question: Keep UI.getElementData in docs, or just for back-compat?
1 parent 577ff39 commit f362f94

6 files changed

Lines changed: 35 additions & 22 deletions

File tree

docs/client/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ Template.api.ui_view = {
19851985
descr: "Optional. A name for this type of View. See [`view.name`](#view_name)."},
19861986
{name: "renderFunction",
19871987
type: "Function",
1988-
descr: "A function that returns [*render content*](#render_content). In this function, `this` is bound to the View."
1988+
descr: "A function that returns [*renderable content*](#renderable_content). In this function, `this` is bound to the View."
19891989
}
19901990
]
19911991
};

packages/blaze/lookup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var bindIfIsFunction = function (x, target) {
1818
var bindDataContext = function (x) {
1919
if (typeof x === 'function') {
2020
return function () {
21-
var data = Blaze.getCurrentData();
21+
var data = Blaze.data();
2222
if (data == null)
2323
data = {};
2424
return x.apply(data, arguments);
@@ -67,7 +67,7 @@ Blaze.View.prototype.lookup = function (name, _options) {
6767
} else {
6868
return function () {
6969
var isCalledAsFunction = (arguments.length > 0);
70-
var data = Blaze.getCurrentData();
70+
var data = Blaze.data();
7171
if (lookupTemplate && ! (data && data[name])) {
7272
throw new Error("No such template: " + name);
7373
}

packages/blaze/template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Template.prototype.constructView = function (contentFunc, elseFunc) {
6868
// object.
6969
var inst = view._templateInstance;
7070

71-
inst.data = Blaze.getViewData(view);
71+
inst.data = Blaze.data(view);
7272

7373
if (view._domrange && !view.isDestroyed) {
7474
inst.firstNode = view._domrange.firstNode();
@@ -150,7 +150,7 @@ Template.prototype.events = function (eventMap) {
150150
eventMap2[k] = (function (k, v) {
151151
return function (event/*, ...*/) {
152152
var view = this; // passed by EventAugmenter
153-
var data = Blaze.getElementData(event.currentTarget);
153+
var data = Blaze.data(event.currentTarget);
154154
if (data == null)
155155
data = {};
156156
var args = Array.prototype.slice.call(arguments);

packages/blaze/view.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,33 @@ Blaze._toText = function (htmljs, parentView, textMode) {
547547
return HTML.toText(Blaze._expand(htmljs, parentView), textMode);
548548
};
549549

550-
Blaze.getCurrentData = function () {
551-
var theWith = Blaze.getCurrentView('with');
550+
Blaze.data = function (elementOrView) {
551+
var theWith;
552+
if (! elementOrView) {
553+
theWith = Blaze.getCurrentView('with');
554+
} else if (elementOrView instanceof Blaze.View) {
555+
var view = elementOrView;
556+
theWith = (view.name === 'with' ? view :
557+
Blaze.getParentView(view, 'with'));
558+
} else if (typeof elementOrView.nodeType === 'number') {
559+
if (elementOrView.nodeType !== 1)
560+
throw new Error("Expected DOM element");
561+
theWith = Blaze.getElementView(elementOrView, 'with');
562+
} else {
563+
throw new Error("Expected DOM element or View");
564+
}
565+
552566
return theWith ? theWith.dataVar.get() : null;
553567
};
554568

569+
// For back-compat
570+
Blaze.getElementData = function (element) {
571+
if (element.nodeType !== 1)
572+
throw new Error("Expected DOM element");
573+
574+
return Blaze.data(element);
575+
};
576+
555577
// Gets the current view or its nearest ancestor of name
556578
// `name`.
557579
Blaze.getCurrentView = function (name) {
@@ -607,16 +629,6 @@ Blaze.getElementView = function (elem, name) {
607629
}
608630
};
609631

610-
Blaze.getElementData = function (elem) {
611-
var theWith = Blaze.getElementView(elem, 'with');
612-
return theWith ? theWith.dataVar.get() : null;
613-
};
614-
615-
Blaze.getViewData = function (view) {
616-
var theWith = Blaze.getParentView(view, 'with');
617-
return theWith ? theWith.dataVar.get() : null;
618-
};
619-
620632
Blaze._addEventMap = function (view, eventMap, thisInHandler) {
621633
thisInHandler = (thisInHandler || null);
622634
var handles = [];

packages/spacebars-tests/template_tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,7 @@ Tinytest.add(
24192419
var span = div.querySelector('SPAN');
24202420
test.isTrue(span);
24212421
test.equal(UI.getElementData(span), {foo: "bar"});
2422+
test.equal(UI.data(span), {foo: "bar"});
24222423
});
24232424

24242425
Tinytest.add(
@@ -2616,7 +2617,7 @@ Tinytest.add('spacebars-tests - template_tests - current view in event handler',
26162617
tmpl.events({
26172618
'click span': function () {
26182619
currentView = Blaze.getCurrentView();
2619-
currentData = Blaze.getCurrentData();
2620+
currentData = Blaze.data();
26202621
}
26212622
});
26222623

packages/ui/render_tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ Tinytest.add("ui - render - templates and views", function (test) {
484484
parent.number);
485485
}
486486

487-
buf.push('created ' + Blaze.getCurrentData());
487+
buf.push('created ' + UI.data());
488488
};
489489

490490
myTemplate.rendered = function () {
@@ -507,19 +507,19 @@ Tinytest.add("ui - render - templates and views", function (test) {
507507
while (end !== start && ! nodeDescr(end))
508508
end = end.previousSibling;
509509

510-
buf.push('dom-' + Blaze.getCurrentData() +
510+
buf.push('dom-' + UI.data() +
511511
' is ' + nodeDescr(start) +'..' +
512512
nodeDescr(end));
513513
};
514514

515515
myTemplate.destroyed = function () {
516516
test.isFalse(Deps.active);
517-
buf.push('destroyed ' + Blaze.getCurrentData());
517+
buf.push('destroyed ' + UI.data());
518518
};
519519

520520
var makeView = function () {
521521
var number = counter++;
522-
return Blaze.With(number, function () {
522+
return UI.With(number, function () {
523523
return myTemplate.constructView(number);
524524
});
525525
};

0 commit comments

Comments
 (0)