0

所以我向 HomeView 添加了一个方法,如下所示:

App.HomeView = Ember.View.extend({
    isFailed: function () {
        console.log(this);
        return "FAILED" === this.get("state");
    }
});

在 HTML 中,这是主页模板的外观。如您所见,我在循环中调用 isFailed 函数,由于某种原因,它从未被调用(日志中没有任何内容):

<script type="text/x-handlebars" id="home">
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Status</th>
            <th>Foo</th>
            <th>Bar</th>
        </tr>
        </thead>

        <tbody>

        {{#each}}
        <tr>
            <td>{{id}}</td>
            <td>
                {{#if isFailed }}
                FAILED
                {{/if}}
            </td>
            <td>bar</td>
            <td>foobar</td>
        </tr>
        {{/each}}

        </tbody>
    </table>
</script>

知道为什么没有调用 isFailed 函数吗?

jsfiddle:http: //jsfiddle.net/dWxTn/2/

4

1 回答 1

1

Handlebars 是一个无逻辑的模板引擎,它永远不会执行代码。它只使用值并打印它们。

这里isFailed很有可能总是返回truthy,因为它是一个函数。

要使用它,请使用Ember 计算属性

isFailed: function () {
    console.log(this);
    return "FAILED" === this.get("state");
}.property("state")

这种方式isFailed将是每次state更改时自动更新的布尔值

于 2013-10-18T14:27:04.033 回答