gpt4 book ai didi

javascript - 将数据传递给 Backbone Js 中的 View 在浏览器中看不到

转载 作者:行者123 更新时间:2023-11-30 17:07:40 24 4
gpt4 key购买 nike

我刚刚开始我的 Backbone 教程,在第一次作业时我必须在页面上显示一点 ul。我写了下面的代码,浏览器没有显示任何错误,但也没有在屏幕上显示列表。有人可以检查我的代码,我哪里做错了。

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Pass Data to Views</title>
</head>
<body>
<!-- ========= -->
<!-- Your HTML -->
<!-- ========= -->

<ul id="songs"></ul>

<!-- ========= -->
<!-- Libraries -->
<!-- =========
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js" type="text/javascript"></script> -->
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/underscore-min.js" type="text/javascript"></script>
<script src="js/backbone-min.js" type="text/javascript"></script>
<script src="js/backbone.localStorage-min.js" type="text/javascript"></script>
<!-- =============== //
<!-- Javascript code -->
<!-- =============== -->
<script type="text/javascript">

// Model
var Song = Backbone.Model.extend();

// Collection : just a group of Models
var Songs = Backbone.Collection.extend({
model: Song
});

// View
var SongView = Backbone.View.extend({
tagName: "li",
render: function(){
this.$el.html(this.model.get("title"));
return this;
}
});

var SongsView = Backbone.View.extend({
render: function(){
var self = this;

this.model.each(function(song){
var songView = new SongView({ model: song });
self.$el.append(songView.render().$el);
});
}
});


// Adding Data to Models.
var songs = new Songs([
new Song({title: "This is title One"}),
new Song({title: "This is Title Two"}),
new Song({title: "This is title third"})
]);

var Object_SongsView = new SongsView({el: "songs", model: songs});
Object_SongsView.render();

</script>

</body>
</html>

最佳答案

应该像这样创建 View (注意 songs 的 CSS 选择器前面的 #):

var Object_SongsView = new SongsView({
el: "#songs",
model: songs
});

关于javascript - 将数据传递给 Backbone Js 中的 View 在浏览器中看不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com