在View.event Backbone.js中创建一个新模型

时间:2013-11-10 21:49:07

标签: javascript backbone.js view model frontend

假设我正在构建一个简单的应用程序,它允许我添加电影并将其显示在列表中。这是我的代码(正文):

<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')

谢谢,抱歉没有问题。

1 个答案:

答案 0 :(得分:0)

是的,你需要收集元素。 你可以这样做:

这是集合:

var MovieCollection = Backbone.Collection.extend({
        model: Movie,
        addMovie: function(elements, options) {
             return this.add(elements, options);
        }
    });

在您的视图中,例如在初始化中,您可以执行此操作:

this.collection = new MovieCollection();

并添加新模型电影试试这个:

var elementMovie = { title: $("#input-addmovie").val(),mpaaRating:"R"};
this.movieModel = new Movie(element);
this.collection.addElement(this.movieModel);

<强>更新

您不必创建新视图,尝试将插入事件和添加的方法添加到Movie视图中,如下所示:

var MovieView = Backbone.View.extend({
        el: "#container-movie",
        template: _.template($("#template-movie").html()),
        initialize: function() {
            // model reder
            this.collection = new MovieCollection();
            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;
        },
        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);
        }
    })