Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temporary fixed mustache index calculation with simple can.List when remove first item of list #613

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions view/mustache/mustache.js
Expand Up @@ -1899,9 +1899,10 @@ function( can ){
// Create a compute that listens to whenever the index of the item in our list changes.
var index = function() {
var exprResolved = Mustache.resolve(expr),
fromIndex = key < (exprResolved).attr('length') ? key : undefined;

return (exprResolved).indexOf(item, fromIndex);
fromIndex = key < (exprResolved).attr('length') ? key : undefined,
index = (exprResolved).indexOf(item, fromIndex);
console.log(item,index);
return index === -1 ? 0 : index;
};
return options.fn( options.scope.add({"@index": index}).add(item) );
});
Expand Down
14 changes: 14 additions & 0 deletions view/mustache/mustache_test.js
Expand Up @@ -2410,6 +2410,20 @@ test("Rendering live bound indicies with #each, @index and a simple can.List", f
equal(lis[2].innerHTML, '2 e', "fifth item now the 3rd item");
});

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

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

// remove first item
list.shift();
equal(lis.length, 2, "two lis");

equal(lis[0].innerHTML, '0 b', "second item now the 1st item");
equal(lis[1].innerHTML, '1 c', "third item now the 2nd item");
});

test('Rendering keys of an object with #each and @key', function() {
delete can.Mustache._helpers['foo'];
var template = can.view.mustache("<ul>{{#each obj}}<li>{{@key}} {{.}}</li>{{/each}}</ul>");
Expand Down