模板未在backbone.js中加载(TypeError:文本未定义)

2023-12-30

我在学主干.js http://backbonejs.org/我还处于起步阶段。我想通过添加模板下划线 http://underscorejs.org/#template模板方法,但它对我不起作用。我搜索了这个错误,但无法自行修复。如果没有显示模板,我该如何继续。需要一些帮助。

这是代码(该代码来自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();
});

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

模板未在backbone.js中加载(TypeError:文本未定义) 的相关文章

随机推荐