3

我正在尝试使用 Underscore.js 学习 Backbone.js 并遇到了麻烦。我使用 Grails 作为服务器框架,所以 Underscore.js 语法 <%= %> 是不可能的。我想将其更改为 {{}} 样式。我的 Javascript 被分隔在许多文件中,每个文件代表我需要的每个对象的视图或模型。这是我的视图的代码:

$(function () {

    _.templateSettings = {
        interpolate : /\{\{(.+?)\}\}/g,
        evaluate : /\{!(.+?)!\}/g
    };

    APP = window.APP || {};

    APP.PlaceView = Backbone.View.extend({
        initialize:function () {
            this.render();
        },
        el:"#place-form",
        formTemplate:_.template($('#search-template').html()),
        render:function () {
            //Pass variables in using Underscore.js Template
            var variables = { street:"Ulica" };

            this.$el.html(this.formTemplate({ "street":"Ulica" }));
        }
    });

    var view = new APP.PlaceView();
});

和模板:

<script type="text/template" id="search-template">
    <!-- Access template variables with {{ }} -->
    <label>{{ street }}</label>
    <input type="text" id="search_input"/>
    <input type="button" id="search_button" value="Search"/>
</script>

此代码引发 Uncaught SyntaxError: Unexpected token ) 错误。但是当我删除 _.templateSettings 部分时,一切正常,但我没有变量。

非常感谢。

4

1 回答 1

10

我相信它因为这条线而显示该错误:

<!-- Access template variables with {{ }} -->

Underscore 试图用一个不存在的变量替换它,从而引发错误。

于 2012-08-16T16:40:39.043 回答