假设我正在构建一个简单的应用程序,它允许我添加电影并将它们显示在列表中。这是我的代码(正文):
<div id="container-addmovie"></div>
<p>Movies:</p>
<ul id="container-movie"></ul>
<!-- templates -->
<!-- Movie template -->
<script type="text/template" id="template-movie">
<li><strong>Title: </strong><%=title%>; <strong>Rating: </strong><%=mpaaRating%></li>
</script>
<!-- add movie template -->
<script type="text/template" id="template-addmovie">
<input type="text" name="" id="input-addmovie">
<input type="button" name="" id="button-addmovie" value="Add new movie">
</script>
<!-- models -->
<!-- movie model -->
<script type="text/javascript">
var Movie = Backbone.Model.extend({});
</script>
<!-- views -->
<!-- movie view -->
<script type="text/javascript">
var MovieView = Backbone.View.extend({
el: "#container-movie",
template: _.template($("#template-movie").html()),
initialize: function() {
// model reder
this.render();
// change event
this.listenTo(this.model, "change", this.render);
},
render: function() {
console.log("MovieView render; Movie Title: " + this.model.get("title"));
var htmlOutput = this.template(this.model.toJSON());
this.$el.html(htmlOutput);
return this;
}
})
</script>
<!-- add movie view -->
<script type="text/javascript">
var AddMovieView = Backbone.View.extend({
initialize: function() {
// model reder
this.render();
},
render: function() {
var template = _.template($("#template-addmovie").html());
this.$el.html ( template );
},
events: {
"click input[type=button]": "addNewMovie"
},
addNewMovie: function (event) {
var movieModel = new Movie({ title: $("#input-addmovie").val(), mpaaRating: "R" });
var movieView = new MovieView({ model: movieModel });
}
})
</script>
<script type="text/javascript">
var addMovie = new AddMovieView( {el: $("#container-addmovie")} )
</script>
问题是当我启动addNewMovie
事件时......
var movieModel = new Movie({ title: $("#input-addmovie").val(), mpaaRating: "R" });
var movieView = new MovieView({ model: movieModel });
...我不是在创建新模型,而是覆盖退出。因此,不会在 ul 中创建新的 li,但现有的会发生变化。问题是:每次我启动 addNewMovie 事件并渲染它的视图时如何创建一个新模型(将新的 li 推到它的容器中)? 我想,我需要一些收藏品。
更新(添加集合):
<div id="container-addmovie"></div>
<p>Movies:</p>
<ul id="container-movie"></ul>
<!-- templates -->
<!-- Movie template -->
<script type="text/template" id="template-movie">
<li><strong>Title: </strong><%=title%>; <strong>Rating: </strong><%=mpaaRating%></li>
</script>
<!-- add movie template -->
<script type="text/template" id="template-addmovie">
<input type="text" name="" id="input-addmovie">
<input type="button" name="" id="button-addmovie" value="Add new movie">
</script>
<!-- models -->
<!-- movie model -->
<script type="text/javascript">
var Movie = Backbone.Model.extend({});
</script>
<!-- collections -->
<script type="text/javascript">
var MovieCollection = Backbone.Collection.extend({
model: Movie,
addMovie: function(element) {
return this.add(element);
}
});
</script>
<!-- views -->
<!-- movie view -->
<script type="text/javascript">
var MovieView = Backbone.View.extend({
el: "#container-movie",
template: _.template($("#template-movie").html()),
initialize: function() {
// model reder
this.render();
// change event
this.listenTo(this.model, "change", this.render);
},
render: function() {
console.log("MovieView render; Movie Title: " + this.model.get("title"));
var htmlOutput = this.template(this.model.toJSON());
this.$el.html(htmlOutput);
return this;
}
})
</script>
<!-- add movie view -->
<script type="text/javascript">
var AddMovieView = Backbone.View.extend({
initialize: function() {
// model reder
this.render();
this.collection = new MovieCollection();
},
render: function() {
var template = _.template($("#template-addmovie").html());
this.$el.html ( template );
},
events: {
"click input[type=button]": "addNewMovie"
},
addNewMovie: function (event) {
var elementMovie = { title: $("#input-addmovie").val(),mpaaRating:"R"};
this.movieModel = new Movie(elementMovie);
console.log (elementMovie);
this.collection.addMovie(this.movieModel);
}
})
</script>
<script type="text/javascript">
var addMovie = new AddMovieView( {el: $("#container-addmovie")} )
</script>
我仍然得到TypeError: 'undefined' is not an object (evaluating 'this.collection.addMovie')
谢谢,对不起菜鸟问题。