【问题标题】:Underscore.js - Uncaught SyntaxError: Unexpected token ) while generating template with variablesUnderscore.js - Uncaught SyntaxError: Unexpected token ) 同时生成带有变量的模板
【发布时间】:2012-11-30 07:53:02
【问题描述】:

我正在尝试使用 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 部分时,一切正常,但我没有变量。

非常感谢。

【问题讨论】:

  • 我得到了完全相同的错误,我正在使用把手的语法,因为我正在使用 HAML,知道你是如何解决这个问题的吗?

标签: backbone.js underscore.js


【解决方案1】:

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

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

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

【讨论】:

  • 这快把我逼疯了!它不应该在示例代码中
猜你喜欢
  • 1970-01-01
  • 2012-05-17
  • 2019-02-10
  • 2014-07-08
  • 2011-03-09
  • 2014-01-06
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多