Skip to content

Commit

Permalink
Fixes #1078 - add offset support to @Index
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Marchán committed Jul 2, 2014
1 parent 75cd39e commit aca6b5d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
17 changes: 12 additions & 5 deletions view/mustache/mustache.js
Expand Up @@ -1358,7 +1358,7 @@ steal('can/util',
}
}
} else if (arg && isLookup(arg)) {
args.push(Mustache.get(arg.get, scopeAndOptions, false, true));
args.push(Mustache.get(arg.get, scopeAndOptions, false, true, true));
} else {
args.push(arg);
}
Expand Down Expand Up @@ -1511,7 +1511,7 @@ steal('can/util',
* @param {Object} context The context to use for checking for a reference if it doesn't exist in the object.
* @param {Boolean} [isHelper] Whether the reference is seen as a helper.
*/
Mustache.get = function (key, scopeAndOptions, isHelper, isArgument) {
Mustache.get = function (key, scopeAndOptions, isHelper, isArgument, isLookup) {

// Cache a reference to the current context and options, we will use them a bunch.
var context = scopeAndOptions.scope.attr('.'),
Expand Down Expand Up @@ -1554,7 +1554,7 @@ steal('can/util',
//!steal-remove-end

// Use helper over the found value if the found value isn't in the current context
if ((initialValue === undefined || computeData.scope !== scopeAndOptions.scope) && Mustache.getHelper(key, options)) {
if (!isLookup && (initialValue === undefined || computeData.scope !== scopeAndOptions.scope) && Mustache.getHelper(key, options)) {
return key;
}

Expand Down Expand Up @@ -2053,7 +2053,7 @@ steal('can/util',
console.log(expr, options.context);
}
}
}
},
/**
* @function can.mustache.helpers.elementCallback {{(el)->CODE}}
*
Expand Down Expand Up @@ -2116,7 +2116,14 @@ steal('can/util',
* </ul>
*
*/
//
"@index": function(offset, options) {
if (!options) {
options = offset;
offset = 0;
}
var index = options.scope.attr("@index");
return ""+((can.isFunction(index) ? index() : index) + offset);
}
/**
* @function can.mustache.helpers.key {{@key}}
*
Expand Down
31 changes: 31 additions & 0 deletions view/mustache/mustache_test.js
Expand Up @@ -2904,6 +2904,37 @@ steal("can/model", "can/view/mustache", "can/test", "can/view/mustache/spec/spec
}
});

test("Rendering indicies of an array with @index + offset (#1078)", function () {
var template = can.view.mustache("<ul>{{#each list}}<li>{{@index 5}} {{.}}</li>{{/each}}</ul>");
var list = [0, 1, 2, 3];

var lis = template({
list: list
})
.childNodes[0].getElementsByTagName('li');

for (var i = 0; i < lis.length; i++) {
equal(lis[i].innerHTML, (i+5 + ' ' + i), 'rendered index and value are correct');
}
});

test("Passing indices into helpers as values", function () {
var template = can.view.mustache("<ul>{{#each list}}<li>{{test @index}} {{.}}</li>{{/each}}</ul>");
var list = [0, 1, 2, 3];

var lis = template({
list: list
}, {
test: function(index) {
return ""+index;
}
}).childNodes[0].getElementsByTagName('li');

for (var i = 0; i < lis.length; i++) {
equal(lis[i].innerHTML, (i + ' ' + i), 'rendered index and value are correct');
}
});

test("Rendering live bound indicies with #each, @index and a simple can.List", function () {
var list = new can.List(['a', 'b', 'c']);
var template = can.view.mustache("<ul>{{#each list}}<li>{{@index}} {{.}}</li>{{/each}}</ul>");
Expand Down
8 changes: 8 additions & 0 deletions view/stache/mustache_helpers.js
Expand Up @@ -64,6 +64,14 @@ steal("can/util", "./utils.js","can/view/live",function(can, utils, live){
return result;

},
"@index": function(offset, options) {
if (!options) {
options = offset;
offset = 0;
}
var index = options.scope.attr("@index");
return ""+((can.isFunction(index) ? index() : index) + offset);
},
'if': function (expr, options) {
var value;
// if it's a function, wrap its value in a compute
Expand Down
31 changes: 31 additions & 0 deletions view/stache/stache_test.js
Expand Up @@ -2600,6 +2600,37 @@ steal("can/view/stache", "can/view","can/test","can/view/mustache/spec/specs",fu
}
});

test("Rendering indicies of an array with @index + offset (#1078)", function () {
var template = can.stache("<ul>{{#each list}}<li>{{@index 5}} {{.}}</li>{{/each}}</ul>");
var list = [0, 1, 2, 3];

var lis = template({
list: list
})
.childNodes[0].getElementsByTagName('li');

for (var i = 0; i < lis.length; i++) {
equal(lis[i].innerHTML, (i+5 + ' ' + i), 'rendered index and value are correct');
}
});

test("Passing indices into helpers as values", function () {
var template = can.view.stache("<ul>{{#each list}}<li>{{test @index}} {{.}}</li>{{/each}}</ul>");
var list = [0, 1, 2, 3];

var lis = template({
list: list
}, {
test: function(index) {
return ""+index;
}
}).childNodes[0].getElementsByTagName('li');

for (var i = 0; i < lis.length; i++) {
equal(lis[i].innerHTML, (i + ' ' + i), 'rendered index and value are correct');
}
});

test("Rendering live bound indicies with #each, @index and a simple can.List", function () {
var list = new can.List(['a', 'b', 'c']);
var template = can.stache("<ul>{{#each list}}<li>{{@index}} {{.}}</li>{{/each}}</ul>");
Expand Down

0 comments on commit aca6b5d

Please sign in to comment.