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

前端之家收集整理的这篇文章主要介绍了模板未在backbone.js中加载(TypeError:text未定义)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在学习 backbone.js,而且我刚开始学习.我想通过 underscore模板方法添加模板,但它不适用于我.我搜索了这个错误,但无法自己解决.如果没有显示模板,我该如何前进?需要一些帮助的人.

这是代码(此代码来自addyosmani的书籍骨干基础知识):

<!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();
});

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

猜你在找的JavaScript相关文章