Implement UI.getElementData

Replaces Spark.getDataContext
This commit is contained in:
Avital Oliver
2014-03-13 14:06:24 -07:00
parent b9370da58d
commit b2bb353a10
4 changed files with 29 additions and 9 deletions

View File

@@ -13,21 +13,18 @@ UI.body.rendered = function () {
var newRank;
if (!before) { // moving to the top of the list
newRank = SimpleRationalRanks.beforeFirst(
after.$ui.component.data().rank);
newRank = SimpleRationalRanks.beforeFirst(UI.getElementData(after).rank);
} else if (!after) { // moving to the bottom of the list
newRank = SimpleRationalRanks.afterLast(
before.$ui.component.data().rank);
newRank = SimpleRationalRanks.afterLast(UI.getElementData(before).rank);
} else {
newRank = SimpleRationalRanks.between(
before.$ui.component.data().rank,
after.$ui.component.data().rank);
UI.getElementData(before).rank,
UI.getElementData(after).rank);
}
Items.update(el.$ui.component.data()._id, {$set: {rank: newRank}});
Items.update(UI.getElementData(el)._id, {$set: {rank: newRank}});
}
});
};

View File

@@ -329,3 +329,11 @@ UI.Component.notifyParented = function () {
UI.Component.preserve = function () {
Meteor._debug("The 'preserve' method on templates is now unnecessary and deprecated.");
};
// Gets the data context of the enclosing component that rendered a
// given element
UI.getElementData = function (el) {
var comp = UI.DomRange.getContainingComponent(el);
return comp && getComponentData(comp);
};

View File

@@ -1107,4 +1107,4 @@ DomRange.prototype.on = function (events, selector, handler) {
};
UI.DomRange = DomRange;
UI.DomRange = DomRange;

View File

@@ -684,3 +684,18 @@ Tinytest.add("ui - UI.render", function (test) {
document.body.removeChild(div);
});
Tinytest.add("ui - UI.getDataContext", function (test) {
var div = document.createElement("DIV");
var tmpl = UI.Component.extend({
render: function () {
return SPAN();
}
});
UI.insert(UI.renderWithData(tmpl, {foo: "bar"}), div);
var span = $(div).children('SPAN')[0];
test.isTrue(span);
test.equal(UI.getElementData(span), {foo: "bar"});
});