模板Backbone.js的不加载(类型错误:文字是不确定的) [英] template not loading in backbone.js ( TypeError: text is undefined )
本文介绍了模板Backbone.js的不加载(类型错误:文字是不确定的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我学习 Backbone.js的我就是pretty多在开始。我想补充通过强调模板方法模板,但它不是为我工作。我搜索了这个错误,但无法修复它自己。我怎样才能前进,如果它没有显示的模板。需要一些帮助球员。
下面是code(这code是addyosmani的书骨干基本面):
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<间的charset =UTF-8>
<标题>测试与LT; /标题>
< /头>
<身体GT;
<脚本SRC =脚本/ jquery.js和>< / SCRIPT>
<脚本SRC =脚本/ underscore.js>< / SCRIPT>
<脚本SRC =脚本/ Backbone.js的>< / SCRIPT>
<脚本>
VAR TodoView = Backbone.View.extend({
标签名:礼,
//缓存中的模板函数的单个项目。
todoTpl:_.template($('#项目模板)HTML()), 事件:{
DBLCLICK标签:编辑,
关键preSS .edit':'updateOnEnter',
模糊.edit':'关闭'
}, //重新呈现待办事项的标题。 渲染:功能(){
这$ el.html(this.todoTpl(this.model.toJSON()));
this.input =这个$('编辑')。
返回此;
}, 编辑:函数(){
// TODO时,标签被双击执行
}, 关闭:函数(){
// TODO时失去焦点执行
}, updateOnEnter:功能(E){
//在待办事项编辑模式下,每个键preSS执行,
//但我们将等待进入行动得到
} });
VAR todoView =新TodoView();
//记录引用,该cooresponds到视图实例的DOM元素
的console.log(todoView.el);
解决方案
如果模板脚本定义后,它不会工作。
包住切入点
$(函数(){
VAR todoView =新TodoView();
});
所以你不要让这样那样的错误。
I'm learning backbone.js and I'm pretty much in the beginning. I want to add a template through underscore template method but it's not working for me. I searched for this error but couldn't fix it myself. How can I move forward if it's not showing the template. Need some help guys.
Here is the code (this code is from addyosmani's book 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);
解决方案
If the template is defined after your script it will not work.
wrap your entry point into
$(function(){
var todoView = new TodoView();
});
so you dont get this kind of error.
这篇关于模板Backbone.js的不加载(类型错误:文字是不确定的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文