无法让 backbone 渲染任何内容
Can't get backbone to render anything
我正在尝试学习一些 backbone.js,但在第一个障碍上失败了。它只是不会渲染任何东西。我已经尝试了许多不同的 'getting started' 文章,但仍然一无所获。没有错误,什么都没有。我已经将它简化为我认为应该根据我所阅读的内容工作的东西,但现在我对任何事情都完全不确定。请帮忙...
编辑:使用 JSfiddle 更新:https://jsfiddle.net/zsLjawy0/
html:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
</head>
<body>
<script type="text/template" id="test">
test
</script>
<script src="app.js"></script>
</body>
</html>
app.js
$(document).ready(function(){
TestView = Backbone.View.extend({
initialize: function() {
this.render();
console.log('this does nothing');
},
render: function() {
var template = _.template( $("#test").html(), {} );
this.$el.html( template );
return this; // Also does nothing
}
});
});
正如我所见,您的应用只是定义了 TestView 但没有创建它的实例,也没有呈现它。您应该创建一个 TestView 实例,呈现它并将呈现的内容添加到文档中。
您的代码有几个问题。
首先,如 Voice 所示,在某些时候您需要创建视图的实例,否则无法初始化渲染函数。 ==> MyView = new TestView();
第二点,你需要定义一个模板和一个#el,这样你才能正确渲染。
TestView = Backbone.View.extend({
el: #test,
template: _.template(LoadAtemplateWithREQUIREJS_it_would_be_wise),
initialize: function() {
this.render();
console.log('this does nothing');
},
render: function() {
var template =
this.$el.html( this.template(someParams_amodelforinstance);
return this; // Also does nothing
}
});
最后一点,不要使用脚本作为您的 div 目标,使用 div。
这 :
<div id="test"> </div>
我让你知道为什么。
希望对您有所帮助!
我正在尝试学习一些 backbone.js,但在第一个障碍上失败了。它只是不会渲染任何东西。我已经尝试了许多不同的 'getting started' 文章,但仍然一无所获。没有错误,什么都没有。我已经将它简化为我认为应该根据我所阅读的内容工作的东西,但现在我对任何事情都完全不确定。请帮忙...
编辑:使用 JSfiddle 更新:https://jsfiddle.net/zsLjawy0/
html:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
</head>
<body>
<script type="text/template" id="test">
test
</script>
<script src="app.js"></script>
</body>
</html>
app.js
$(document).ready(function(){
TestView = Backbone.View.extend({
initialize: function() {
this.render();
console.log('this does nothing');
},
render: function() {
var template = _.template( $("#test").html(), {} );
this.$el.html( template );
return this; // Also does nothing
}
});
});
正如我所见,您的应用只是定义了 TestView 但没有创建它的实例,也没有呈现它。您应该创建一个 TestView 实例,呈现它并将呈现的内容添加到文档中。
您的代码有几个问题。
首先,如 Voice 所示,在某些时候您需要创建视图的实例,否则无法初始化渲染函数。 ==> MyView = new TestView();
第二点,你需要定义一个模板和一个#el,这样你才能正确渲染。
TestView = Backbone.View.extend({
el: #test,
template: _.template(LoadAtemplateWithREQUIREJS_it_would_be_wise),
initialize: function() {
this.render();
console.log('this does nothing');
},
render: function() {
var template =
this.$el.html( this.template(someParams_amodelforinstance);
return this; // Also does nothing
}
});
最后一点,不要使用脚本作为您的 div 目标,使用 div。 这 :
<div id="test"> </div>
我让你知道为什么。
希望对您有所帮助!