Backbone Navigation Error

对着背影说爱祢 提交于 2019-12-13 04:31:28

问题


Whenever I press the back button in my browser the view is empty, and I get an error like this:

I don't really know what part of my application I should put on here to help the problem but none of the errors refer to a part of my code..

Interestingly if I press the front button and press the back button again everything renders perfectly and the error is not there anymore.

My router and views look like this:

var AppRouter = Backbone.Router.extend({

    initialize: function() {
        this.model = new model();
        this.collection = new collection();
    },

    routes: {
        "": "showForm",
        "post/:id": "showPost"
    },

    showPost: function(id) {
        var that = this;
        this.collection.fetch().complete(function() {
            var curmodel = that.collection.get(id);
            var post = new postView({
                model: curmodel
            });
            post.render();
            $(".maincontainer").html(post.el);
        });
    },

    showForm: function() {
        var that = this;
        var qcView = new qcV({
            collection: this.collection
        });
        qcView.render();
        $(".maincontainer").html(qcView.el);
    }
});


var PostView = Backbone.View.extend({
    template: _.template(postTemplate),

    events: {
        'click .reply': 'addcomment',
        'keypress textarea': 'addcommentOnEnter'
    },

    initialize: function() {
        _.bindAll(this, "render", "addcomment", "addcommentOnEnter");
        this.model.bind('change', this.render);
    },

    addcomment: function() {
        this.model.comment($(".post-area").val());
    },

    addcommentOnEnter: function(e) {
        if (e.keyCode != 13) return;
        this.model.comment($(".post-area").val());
    },

    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
    }
});

var qcView = Backbone.View.extend({
    template: _.template(quickComposeTemplate),

    events: {
        'click .next-after': 'save',
    },

    render: function() {
        console.log("qcV render");
        this.$el.html(this.template({
            user: window.user
        }));
        var postlist = new postOverViewList({
            collection: this.collection
        });
        postlist.render();
        $(".postoverview").html(postlist.el);
    },

    save: function() {
        var that = this;

        var newmodel = this.collection.create({
            name: $('#big-input').val(),
            firstRemark: $('#small-input').val(),
            postedBy: window.user.displayName,
            twitterHandle: window.user.twittername,
            pictureUrl: window.user.profilePic
        }, { wait: true });

        var that = this;

        this.collection.fetch().complete(function() {
            window.location.href = "/#post/" + that.collection.last().get("_id")
        });
    },
});

var postOverViewList = Backbone.View.extend({

    initialize: function() {
        _.bindAll(this, 'render', 'addOne');
        // this.listenTo(this.collection, 'add', this.addOne);
    },

    render: function() {
        var that = this;
        this.collection.fetch().complete(function() {
            that.collection.forEach(that.addOne, that);
        });
    },

    addOne: function(post) {
        var posta = new postOverView({
            model: post
        });
        posta.render();
        this.$el.append(posta.el);
    },
});


var postOverView = Backbone.View.extend({
    template: _.template(postOverViewTemplate),

    render: function() {
        $('.quick-compose-posts').append(this.template(this.model.toJSON()));
    },
});

来源:https://stackoverflow.com/questions/18137699/backbone-navigation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!