Skip to content

Commit

Permalink
event callback gets "template" obj
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreensp committed Aug 10, 2012
1 parent fe1e65c commit effc8cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
12 changes: 6 additions & 6 deletions packages/spark/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@ Spark.Landmark = function () {
};

_.extend(Spark.Landmark.prototype, {
firstNode: function () {
return this._range.firstNode();
},
lastNode: function () {
return this._range.lastNode();
},
find: function (selector) {
var r = this._range;
return DomUtils.findClipped(r.containerNode(), selector,
Expand All @@ -947,12 +953,6 @@ _.extend(Spark.Landmark.prototype, {
var r = this._range;
return DomUtils.findAllClipped(r.containerNode(), selector,
r.firstNode(), r.lastNode());
},
firstNode: function () {
return this._range.firstNode();
},
lastNode: function () {
return this._range.lastNode();
}
});

Expand Down
26 changes: 25 additions & 1 deletion packages/templating/deftemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,35 @@
});
});

// take an event map with `function (event, template)` handlers
// and produce one with `function (event, landmark)` handlers
// for Spark, by inserting logic to create the template object.
var wrapEventMap = function (oldEventMap) {
var newEventMap = {};
_.each(oldEventMap, function (handler, key) {
newEventMap[key] = function (event, landmark) {
var template = {
find: function (selector) {
return landmark.find(selector);
},
findAll: function (selector) {
return landmark.findAll(selector);
},
firstNode: landmark.firstNode(),
lastNode: landmark.lastNode(),
data: templateInstanceData[landmark.id]
};
return handler.call(this, event, template);
};
});
return newEventMap;
};

// events need to be inside the landmark, not outside, so
// that when an event fires, you can retrieve the enclosing
// landmark to get the template data
if (tmpl.events)
html = Spark.attachEvents(tmpl.events, html);
html = Spark.attachEvents(wrapEventMap(tmpl.events), html);
return html;
});

Expand Down

0 comments on commit effc8cb

Please sign in to comment.