我是新的骨干我目前正在学习模板但是我有这个输出错误 未捕获的TypeError:无法读取属性'替换' of undefined underscore.js:1305 _.template underscore.js:1305 (匿名函数)
这是我的index.js
var Person = Backbone.Model.extend({
defaults: {
name: 'ozan onder',
age: 20,
occupation: 'worker'
}
});
var PersonView = Backbone.View.extend({
//by default the tagname is a div element
//but we will override it
tagName: 'li',
//% is how can access to a property name
// create templer
template: _.template($('#personTemplate').html()),
initialize: function() {
this.render();
},
render: function() {
//set the html
this.$el.html(this.template(this.model.toJSON()));
}
});
var person = new Person;
var personView = new PersonView({ model: person });
console.log(personView.el);
And here is my index.html
<!DOCTYPE html>
<html>
<head>
<!--this import order is important-->
<script src="../../library/jquery-1.8.2.min.js"></script>
<script src="../../library/underscore.js"></script>
<script src="../../library/backbone.js"></script>
<script src="index.js"></script>
</head>
<body>
<script id="personTemplate" type="text/template">
<strong><%= name %></strong> (<%= age %>) - <%= occupation %>
</script>
</body>
</html>
答案 0 :(得分:1)
确保将代码包装在jQuery ready函数中,以便在加载页面之前不会触发它。也许您正在对代码初始化时不存在的DOM元素进行操作。
$( document ).ready(function() {
// put your code here
});
OR - They both accomplish the same thing.
$(function() {
// put your code here
});