Reputation: 5424
I have just started my backbone tutorials, and on first assignment i have to display a lit of ul on the page. i have write the following code, the browser didn't show any errors but also didn't display the list on the screen. Can somebody inspect my code , where i am doing wrong.
<!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>
Upvotes: 1
Views: 98
Reputation: 193261
The view should be created like this (note #
in front of CSS selector for songs
):
var Object_SongsView = new SongsView({
el: "#songs",
model: songs
});
Upvotes: 2