K
K
Konstantin Ivanov2015-02-05 15:11:10
backbone.js
Konstantin Ivanov, 2015-02-05 15:11:10

The model is not deleted from the previous record?

Good afternoon! Need help implementing post editing (using Backbone). I will describe the essence of the problem.
There is an article section available at site.ru/admin#articles. There is a list of articles. There is an "Edit" button.
aab28040503f45c1ab6ba1196ce0152d.jpg
If it is clicked, the '*articles/edit/:id' : 'editArticle' route is triggered and the function is called

editArticle: function(articles, id){
    require(['models/articles'], function(ArticlesModel){
            var Articles = new ArticlesModel({id: id});
            Articles.fetch().then(function(){
                require(['views/articles/edit'], function(EditArticle){
                    var editArticle = new EditArticle({model: Articles});
                });
            });
    });
},

Next, we change something in the form and click save, everything is fine. But if you click on editing another record, edit it, then when you save, the previous record is overwritten. Those. it turns out that the model has not been deleted from memory or something else. I can not understand. Need help. Here is the edit form:
define([
    'jquery',
    'underscore',
    'backbone',
    'routes/routes',
    'text!templates/articles/edit.html'
    ], function($, _, Backbone, Router, EditArticleTpl){
        var EditArticle = Backbone.View.extend({
            el: "#element-page",
            template: _.template(EditArticleTpl),
            events: {
                'click #submit_edit_article': 'edit',
            },
            initialize: function(){
                this.render();
            },
            close: function(){
                this.stopListening();
            },
            render: function(){
                this.$el.html(this.template(this.model.toJSON()));
                return this;
            },
            edit: function(){
                this.model.save({
                    title       : this.$("#container_edit_article #title").val(),
                    text        : this.$("#container_edit_article #text").val(),
                    publish     : this.$("#container_edit_article #publish").val(),
                    title_seo   : this.$("#container_edit_article #title_seo").val(),
                    keywords    : this.$("#container_edit_article #keywords").val(),
                    description : this.$("#container_edit_article #description").val(),
                }, {validate : true});
                
                // Перенаправляет на другую страницу
                var router = new Router();
                router.navigate("articles", true);

            },
        });
    
        return EditArticle;
});

3befa4460eee444786c630c5a7fcff19.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aen, 2015-02-06
@aen

You have zombie views. When you redirect router.navigate("articles", true);the view itself is not destroyed and it continues to wait for the element to be clicked. In fact, it just hangs in memory, and when you render a new view in the same place, the previous ones catch a click on #submit_edit_article.
In short: destroy views if you are not using them.
For a better understanding, put a breakpoint in the method editand look at the callstack from where the call comes from and on which object it is caught. cidthe views will be different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question