Ember JS - Handlebars模板 - @index未定义#each

时间:2015-06-04 08:57:15

标签: javascript templates ember.js ember-cli handlebars.js

我找了一个重复的问题,但在SO上找不到。因此,它就在这里。

我的模板中有一个#each块,它可以成功打印数组中的值但是当我尝试使用@index中的{{@index}}来获取当前索引时,Router.map(function() { this.resource("books", function(){ this.route("about", { path : "/:bookId"}, function(){ }); }); }); ,它是未定义的。

official handlebars docs我找到了获取当前索引的方法。

路由器(router.js):

var BooksRoute = Em.Route.extend({
    model: function() {
        return [{
            name: "Harry Potter & The Deathly Hallows"
        }, {
            name: "What If?"
        }, {
            name: "Diary of a Wimpy Kid"
        }, {
            name: "The Martian"
        }];
    }
});

export default BooksRoute;

路线(routes / books.js):

<div class="page-header">
    <h1>All Books!</h1>
    <ul>
        {{#each book in model}}
            {{@index}}
            <li>{{#link-to "books.about"}} {{book.name}} {{/link-to}}</li>
        {{/each}}
    </ul>
</div>

模板(templates / books / index.hbs):

:bookId

我希望能够做的是生成每本书的链接,当前索引用作/books/1,例如{{#each model as |book index|}}{{#each model as |book index|}}

我的代码有什么问题吗?我怎样才能使它发挥作用?

更新: 我也试过{{@index}}而没有运气。

适用于{{index}}。问题是我使用新方法import csv def read_lines(file_name, start, end): with open(file_name, 'rb') as csvfile: csvreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in csvreader: if csvreader.line_num >= start and csvreader.line_num <= end: print ', '.join(row) else: continue read_lines('test.csv', 10,12) 而不是csvreader.line_num: The number of lines read from the source iterator. This is not the same as the number of records returned, as records can span multiple lines.

1 个答案:

答案 0 :(得分:6)

Since ember 1.11.0 index support has been added to the each helper.您可以像

一样使用它
{{#each model as |book index|}}
  {{index}}
        <li>{{#link-to "books.about" index}} {{book.name}} {{/link-to}}</li>
{{/each}}

Here is a working demo.