BorisMoore/jsrender — Issue #66: No way to access previous Items data?
Author: srb-git-admin
Created: 2012-01-25 11:13:30+00:00
State: open

Hi, I hadn't found a way to access the previously rendered . I wanted to compare Months between items in my list and upon a change in month I would add a new Span.

I was able to accomplish this by modifying the jsrender code as follows...

in the Render function

render: render = function (data, tmpl, context, parentView, path, tagName) {
...
if ($.isArray(data)) {
... for (i = 0, l = data.length; i < l; i++) {

//******Added this code to allow access to the previous item*****
var prev = (i > 0) ? dataItem : null;
dataItem = data[i];
dataItem.previous = prev;

content = dataItem ? tmpl(dataItem, new View(context, path, arrayView, dataItem, tmpl, this)) : "";
....

In my Template, i use it as follows

{{*
if ($data.previous !=null &&
(new Date($data.DateOfEventString)).getYear() !=
(new Date($data.previous.DateOfEventString)).getYear()) {
}}

{{= YearLabel }}

{{*
}
}}