Skip to content

Commit

Permalink
Change where $idx and $len are set. Move it from the push method whic…
Browse files Browse the repository at this point in the history
…h is called by parts of the code that have nothing to do with secton iteration to the section method in the code explicitly for iteration. After iteration is done, remove $idx and $len so they won't pollute the context data after exiting the iteration. All tests pass after this change. No external doc change as end user behavior remains the same (except for $idx/$len no longer showing up in dumpContext after the iteration completes)
  • Loading branch information
rragan committed Aug 14, 2012
1 parent 7e3a258 commit e42ff4d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
10 changes: 4 additions & 6 deletions dist/dust-core-1.0.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ Context.prototype.getPath = function(cur, down) {
};

Context.prototype.push = function(head, idx, len) {
if( head ){
// loop index for a block section
head['$idx'] = idx;
// loop size for a block section
head['$len'] = len;
}
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
};

Expand Down Expand Up @@ -403,9 +397,13 @@ Chunk.prototype.section = function(elem, context, bodies, params) {
if (dust.isArray(elem)) {
if (body) {
var len = elem.length, chunk = this;
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
delete context.stack.head['$idx'];
delete context.stack.head['$len'];
return chunk;
}
} else if (elem === true) {
Expand Down
10 changes: 4 additions & 6 deletions dist/dust-full-1.0.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ Context.prototype.getPath = function(cur, down) {
};

Context.prototype.push = function(head, idx, len) {
if( head ){
// loop index for a block section
head['$idx'] = idx;
// loop size for a block section
head['$len'] = len;
}
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
};

Expand Down Expand Up @@ -403,9 +397,13 @@ Chunk.prototype.section = function(elem, context, bodies, params) {
if (dust.isArray(elem)) {
if (body) {
var len = elem.length, chunk = this;
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
delete context.stack.head['$idx'];
delete context.stack.head['$len'];
return chunk;
}
} else if (elem === true) {
Expand Down
10 changes: 4 additions & 6 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@ Context.prototype.getPath = function(cur, down) {
};

Context.prototype.push = function(head, idx, len) {
if( head ){
// loop index for a block section
head['$idx'] = idx;
// loop size for a block section
head['$len'] = len;
}
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
};

Expand Down Expand Up @@ -395,9 +389,13 @@ Chunk.prototype.section = function(elem, context, bodies, params) {
if (dust.isArray(elem)) {
if (body) {
var len = elem.length, chunk = this;
context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
delete context.stack.head['$idx'];
delete context.stack.head['$len'];
return chunk;
}
} else if (elem === true) {
Expand Down

0 comments on commit e42ff4d

Please sign in to comment.