javascriptjqueryajaxbackbone.jsbackbone-views

Click function doesn't work after ajax call in dynamic element (Backbone)


I've create dynamic popup in my Backbone.view by clicking button:

var Section = Backbone.View.extend({
className: 'sqs-frontend-overlay-editor-widget-section',
events:{
    'click .sqs--section--control__edit':           'Section_control'
},

initialize: function(){

},

render: function(){
    this.$el.append(_.template(_section).apply(this.options));
    return this.$el;
},

Section_control: function(){
    var me = this;   
    require(['View/Popup/Section_control'], function(_Section_control){
        var sec     = new _Section_control({popup: popup, sec: me.options.section});
        var popup   = new Popup({content: sec.render()});
    });
}
});
return Section;

in the created dynamic popup i have button with trigger:

    events:{
        'click .module-invert-mode':    'invert'
    },

    invert: function(e){
            console.log('hello');

            if(this.options.sec.hasClass('.module-invert')) {
                console.log('yse');
            }

            this.options.sec.toggleClass('module-invert');
            this.options.sec.trigger('invertChange');

    },

and button invertChange trigger:

    el.on("invertChange", function(e){
        var section = el.parents('section');
        var index   = section.index();
        var model   = collection.at(index);
        model.set(Helper.sectionToObj(section),{doReload: true})
    });

take a look at the {doReload: true} function that i call in invertChange:

    change: function(model, options){
        me = this;


        if( model._changing && options.doReload ) {
            $.ajax({
                url: 'wp-admin/admin-ajax.php',
                type: 'post',
                data: {
                    action: 'getShortcode',
                    shortcode: model.attributes.shortcode
                },
                success: function (data) {
                    //var section = $(data);
                    me.$el.find('section:eq(' + model.collection.indexOf(model) + ')').replaceWith(data);
                    me.add( model, model.collection );
                    //me.collection.add({shortcode: model.attributes.shortcode}, {at: section.index()});
                }
            });
        }
    },

the problem is when I create dynamic popup and click on the button with invertChange trigger, ajax works only once, when I click on button in popup again, ajax doesn't works ( next ajax request works only if close and create dynamic popup again). How I can call ajax without constantly closing and opening my dynamic popup?


Solution

  • The problem that you have code which overrides child views

    me.$el.find('section:eq(' + model.collection.indexOf(model) + ')').replaceWith(data);
    

    And this listener is not able to handle event

    el.on("invertChange", function(e){
    

    because your code

    this.options.sec.trigger('invertChange');
    

    doesn't trigger event on correct view, it has lost the reference to this view after replaceWith()

    As a solution you need parse your data object and apply each changes locally to elements

    something like this

    $(data).find("* [attr]").each(function(i, el) {
        var $el = $(el),
            attr = $el.attr("attr"),
            $parent = me.$el.find('section:eq(' + model.collection.indexOf(model) + ')');
        if ($el.is("div, span")) {
            $parent.find('[attr=' + attr + ']').html($el.html());
        } else if ($el.is("img")) {
            $parent.find('[attr=' + attr + ']').attr("src", $el.attr("src"));
        } else if ($el.is("a")) {
            $parent.find('[attr=' + attr + ']').attr("href", $el.attr("href"));
        } else if (attr == "image_back_one") {
            $parent.find('[attr=' + attr + ']').attr("style", $el.attr("style"));
        } else {
            console.log($el);
        }
    });