模板未在主干中加载.js(类型错误:文本未定义)



我正在学习骨干.js而且我几乎是刚开始。我想通过下划线模板方法添加一个模板,但它对我不起作用。我搜索了此错误,但无法自己修复。如果它没有显示模板,我该如何继续前进。需要一些帮助的家伙。

这是代码(此代码来自addyosmani的书 backbone-fundamentals):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>testing</title>
</head>
<body>
<script src="scripts/jquery.js"></script>
<script src="scripts/underscore.js"></script>
<script src="scripts/backbone.js"></script>
<script>

    var TodoView = Backbone.View.extend({
    tagName: 'li',
    // Cache the template function for a single item.
    todoTpl: _.template( $('#item-template').html() ),
    events: {
    'dblclick label': 'edit',
    'keypress .edit': 'updateOnEnter',
    'blur .edit': 'close'
    },
    // Re-render the titles of the todo item.
    render: function() {
    this.$el.html( this.todoTpl( this.model.toJSON() ) );
    this.input = this.$('.edit');
    return this;
    },
    edit: function() {
    // executed when todo label is double clicked
    },
    close: function() {
    // executed when todo loses focus
    },
    updateOnEnter: function( e ) {
    // executed on each keypress when in todo edit mode,
    // but we'll wait for enter to get in action
    }
    });

    var todoView = new TodoView();
    // logs reference to a DOM element that cooresponds to the view instance
    console.log(todoView.el);

如果模板是在脚本之后定义的,它将不起作用。

将入口点包装成

$(function(){
   var todoView = new TodoView();
});

所以你不会得到这种错误。

我遇到了同样的错误。确保页面上存在具有已定义 id 的模板。就我而言,我为模板使用了错误的id,这是错误"类型错误:n未定义"的原因。

相关内容

最新更新