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

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

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta charset="utf-8">
  5. <title>testing</title>
  6. </head>
  7. <body>
  8. <script src="scripts/jquery.js"></script>
  9. <script src="scripts/underscore.js"></script>
  10. <script src="scripts/backbone.js"></script>
  11. <script>
  12.  
  13.  
  14. var TodoView = Backbone.View.extend({
  15. tagName: 'li',// Cache the template function for a single item.
  16. todoTpl: _.template( $('#item-template').html() ),events: {
  17. 'dblclick label': 'edit','keypress .edit': 'updateOnEnter','blur .edit': 'close'
  18. },// Re-render the titles of the todo item.
  19.  
  20. render: function() {
  21. this.$el.html( this.todoTpl( this.model.toJSON() ) );
  22. this.input = this.$('.edit');
  23. return this;
  24. },edit: function() {
  25. // executed when todo label is double clicked
  26. },close: function() {
  27. // executed when todo loses focus
  28. },updateOnEnter: function( e ) {
  29. // executed on each keypress when in todo edit mode,// but we'll wait for enter to get in action
  30. }
  31.  
  32. });
  33.  
  34.  
  35. var todoView = new TodoView();
  36. // logs reference to a DOM element that cooresponds to the view instance
  37. console.log(todoView.el);

解决方法

如果在脚本之后定义模板,它将无法工作.

将您的切入点包装进去

  1. $(function(){
  2. var todoView = new TodoView();
  3. });

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

猜你在找的JavaScript相关文章