骨干渲染功能:意外的令牌ILLEGAL

时间:2013-06-04 05:10:07

标签: javascript backbone.js underscore.js

尝试渲染backbone.js模板时出现以下错误:

Uncaught SyntaxError: Unexpected token ILLEGAL

从以下代码,第二行,调用html:

render: function() {
        $(this.el).html(_.template(contactTemplate, {
            model: this.model.toJSON(),
        }));
        return this;
    }

我不明白非法角色是什么或正在发生什么,我们非常感谢任何帮助。

编辑: 谢谢你的帮助,你是对的,我的模板有问题,原来我有:

<p><a href="#profile/<%=model.accountId%">View</a></p>

而不是

<p><a href="#profile/<%=model.accountId%>">View</a></p>

编码的乐趣:)。

2 个答案:

答案 0 :(得分:1)

我认为Backbone一定会让你有点疯狂的对象!

render: function() {
        $(this.el).html(_.template(contactTemplate, {
            model: this.model.toJSON(),
        }));
        return this;
    }

只有在您的模板指定了<%= model.field1 %>字段时,才会起作用(我认为)。试试这个:

render: function() {
        $(this.el).html(_.template(contactTemplate, this.model.toJSON()));
        return this;
    }

答案 1 :(得分:0)

当您尝试从模型中访问未定义的字段时,会出现该错误。通过查看代码,当您尝试获取json值时,模板必须如下所示:

<b> the value of field AAA is <%= model.AAA %> </b>

如果你想避免使用模型,只需致电:

 _.template(contactTemplate, this.model.toJSON() )

然后你可以做类似

的事情
<b> the value of field AAA is <%= AAA %> </b>
相关问题