2013-02-07 48 views
0

我從服務器獲取的數據中的每個10秒,在此,我得到3類型的數據的,無法刪除元素仍然陣列得到清除

超時呼叫後,我除去現有的數據,我可以見證控制檯顯示數組清除,但元素仍然附加。

如何清除DOM中的這兩個元素解除綁定以及..

我關閉函數不斷調用,但沒有元素從DOM中刪除。

我的單一視圖:

singleton.view = Backbone.View.extend({ 
    tagName  :'article', 
    template0 : function(value){ 
        var label = value === 0 ? "projectName" : value === 1 ? "assignedTo" :"projectName"; 
        return _.template("<a href='#'><%= "+label+" %></a>"); 
       }, 
    template1 : _.template($('#boardTemplate').html()), 
    initialize :function(prams){ 
     this.template = this['template'+0](prams.subTempNo); 
    }, 
    close:function(){ 
     console.log('clean up') // i am getting consoled 
     this.unbind();// i am unbinding 
     this.remove();// i am removing 
    }, 
    render:function(){ 
     var temp = this.template; 
     this.$el.html(temp(this.model.toJSON())); 
     return this; 
    } 
}); 
return singleton.view; 
的意見

listViewAppender:function(item,i){ 
      var listElement = new singleton.view({model:item,tempNo:0,subTempNo:i,tagName:'li'}); 
      listElement.close(); // whenever i call the new instance i am removing old stuff.. 
      this.$el.find('.'+this.classItems[i]).append(listElement.render().el); 
     }, 

我怎樣才能解決這個問題..任何正確的做法pelase ..

回答

0

我在我的功能走過了,我覺得在this.listCatch[i] = listElement;線的問題錯誤地宣佈。

後來我手動聲明數組,它的工作正常。在我初始化的時候,我介紹了3個數組,我現在需要它的工作正常。

this.listCatch = []; 


      for(var i=0;i<this.listItems.length; i+=1){ 
       this.listCatch[i] = []; 
      } 

所以在將模型推到數組之前,現在引入的數組解決了這個問題。

0

好只是一個快速返工。 ....你將不得不測試它。評論發生了什麼,我將更正下面的代碼。

你可以嘗試

listViewSet:function(key,i){ 
    var l = this.listCatch.length; 
    if(l > 0){ 
     for (var i = 0; i < l; i++) { 
      console.log(this.listCatch[i]); 
      this.listCatch[i].remove(); 
     } 
    } 

    this.listCatch = []; 

    _.each(this.listCollection.models, function(model){ 
     that.listViewAppender(model,i); //setting agian. 
    }); 
}, 

listViewAppender:function(item,i){ 
    var listElement = new singleton.view({model:item,tempNo:0,subTempNo:i,tagName:'li'}); 
    console.log(listElement); 
    this.$el.find('.'+this.classItems[i]).append(listElement.render().el); 
    this.listCatch[i] = listElement; 
}, 
+0

當然,讓我試試這種方式。 – 3gwebtrain

+0

我得到這個錯誤:TypeError:this.listCatch [i] .remove不是函數 – 3gwebtrain

+0

編輯了一下代碼。我假設列表視圖Appender之前調用listViewSet,否則將沒有什麼可以清除。如果首先調用listViewSet,則只需將for循環包裝在檢查'l'(數組長度)大於0的if語句中。同時我添加了兩個控制檯日誌來檢查this.listCatch [i]始終是一個主幹查看 – Xerri