UI.data() / UI.data(view) / UI.data(element)

Question: Keep UI.getElementData in docs, or just for back-compat?
This commit is contained in:
David Greenspan
2014-08-15 16:17:23 -07:00
parent 577ff39c03
commit f362f949af
6 changed files with 35 additions and 22 deletions

View File

@@ -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."
}
]
};

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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 = [];

View File

@@ -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();
}
});

View File

@@ -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);
});
};