1

我之前遇到过这个错误并尝试了我在 SO 上找到的解决方案,但在这种情况下我无法通过尝试我找到的解决方案来解决它。我有一个 question_template 放在我的索引文件的标题中,文件底部带有 js 脚本标签。在视图的初始化程序中,我使用 jQuery html 函数获取模板,控制台日志显示模板是从 index.html 中检索的。但是,当我尝试将其插入下划线 _.template 时,它​​会触发无法调用未定义错误的替换

 var QuestionView = Backbone.View.extend({
el: $(".east"), 
initialize: function(){


    var template = $('#question_template').html();
    console.log(template);
    this.template = _.template(template);   #error triggered


},

由于我能够记录模板,我看不出我的问题是什么?这是下划线代码的一部分。

text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });

第一个问题:“文本”代表什么,在我的情况下,它是未定义的?我会认为“文本”是模板,但既然我可以记录我的模板,它是如何未定义的?

我还准备好包含在文档中的所有 js 代码(包括问题视图的初始化),这在有关此问题的其他 SO 问题中是解决方案

$(function() {


  ...code ommitted...
  var question_view   = new QuestionView({ model: game});



});

第二个问题:还有什么我可以尝试的

更新 注意,我随后将模型数据传递给模板,但由于触发了错误,它永远不会那么远

**$(this.el).html(this.template(response));** 

我分三个步骤准备模板

1. var template = $('#question_template').html();
        console.log(template);
2.      this.template = _.template(template);

3. $(this.el).html(this.template(response)); 
4

1 回答 1

0

您应该将模型数据传递给模板,

this.template = _.template(template, this.model.toJSON());

于 2013-02-02T21:47:07.637 回答