保存模型后无法重新渲染主干视图

unable to re-render the backbone view after saving the model

本文关键字:新渲染 视图 模型 保存      更新时间:2023-09-26

我保存了模型,并且从服务器端得到了更新DB的ok响应。我要做的是重新绘制集合,并使用保存的模型信息更新视图。我遗漏了一些小东西,但我就是想不通。

   $(document).ready(function () {
window.App = {
    Models: {},
    Collections: {},
    Views: {}
};
window.template = function(id){
    return _.template( $('#' + id).html() );
};

// Person Model
App.Models.Person = Backbone.Model.extend({
    defaults: {
        id: null,
        question: null,
        quizid: null,
        answerid: null
    },
     url: function() {
            return 'myurl/';
        }
});

App.Collections.People = Backbone.Collection.extend({
    model: App.Models.Person,
    url: 'myurl/'
});

App.Views.People = Backbone.View.extend({
    tagName: 'ul',
    render: function(){
        this.collection.each(function(person){
            var personView = new App.Views.Person({ model: person });
            this.$el.append(personView.render().el);
        }, this);
        return this;
    }
});

App.Views.Person = Backbone.View.extend({
    tagName: 'li',
    template: template('personTemplate'),
    events: {
     "click #saveButton" : "savedata",
    },

    savedata: function(event){
        //alert(this.model.get("id") + " " + $('#title').val());
                var newModel = new App.Models.Person();
                newModel.save({id: this.model.get("id"), question: $('#title').val()}, {
                    wait : true,
                    success: function (response) {
                        alert(response.responseText);
                        },
                        error: function (model, response) {
                            alert(response.responseText);
                        }
                    }
                );
    },
    render: function(){
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});

var peopleCollection = new App.Collections.People;
peopleCollection.fetch({
    success: function(){
    var peopleView = new App.Views.People({collection: peopleCollection});
  $('body').append(peopleView.render().el);
}});

var pw = new peopleView({collection: personCollection});
pw.render();

如果要在编辑模型数据后刷新视图,则必须侦听syncchange事件并重新呈现视图。

App.Views.Person = Backbone.View.extend({
    initialize: function(params) {
        this.listenTo(this.model, 'sync', this.render);
    }
});

如果你正在添加一个全新的模型,那么你需要在创建后将其添加到集合中。

newModel.save({id: this.model.get("id"), question: $('#title').val()} //...
this.collection.add(newModel)

并在人员视图中侦听add事件

this.lisenTo(this.collection, 'add', this.render
App.Views.People = Backbone.View.extend({
   initialize: function(params) {
       this.listenTo(this.collection, 'add remove sync', this.render);
   },
   render: function(){
       this.$el.empty();
       this.collection.each(function(person){
            var personView = new App.Views.Person({ model: person });
            this.$el.append(personView.render().el);
        }, this);
       return this;
    }
});

在这种情况下,重新获取整个集合是多余的。