2015-04-05 44 views
1

我通過點擊按鈕,創建我的Backbone.view動態彈出後工作:在創建動態彈出Сlick功能不會動態元素Ajax調用(骨幹)

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; 

我有按鈕,觸發:

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

    }, 

和按鈕invertChange觸發:

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}) 
    }); 

看看在{doReload: true}功能,我在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()}); 
       } 
      }); 
     } 
    }, 

的問題是,當我創建動態彈出,然後點擊按鈕,invertChange觸發,Ajax的工作原理只有一次,當我在按鈕彈出再次點擊,AJAX不作品(下一個ajax請求只有在關閉時才起作用,並再次創建動態彈出)。如何在不經常關閉並打開我的動態彈出窗口的情況下調用ajax?謝謝!

+2

嘗試添加'this.delegateEvents()''裏面渲染(解決方案)'方法或'replaceWith()'後內部成功' – 2015-04-05 21:04:41

+0

@EugeneGlova試圖添加內部渲染()或replaceWith()後,但它不適合我( – 20yco 2015-04-06 06:35:45

回答

1

你有代碼覆蓋孩子的問題的看法

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

而這個監聽器無法處理事件

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

因爲你的代碼

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

不在正確的視圖上觸發事件,它在之後失去了對該視圖的引用210

,因爲你需要分析你的data對象和本地應用的每個變化元素

像這樣

$(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); 
    } 
}); 
+0

作品完美的我,thx男人! – 20yco 2015-04-06 09:36:00