繁体   English   中英

如何在获取新数据之前清除主干集合?

[英]How to clear backbone collection before fetching new data?

大家好,我一直在开发主干应用程序,在该应用程序中添加图像,并删除或编辑现有图像。 现在我在路由器的各个部分(如图库,表单)使用路由器,但是当我在图库中编辑某些内容时,又回到表单,然后又回来,我没有得到之前所做的更改。 而是显示旧数据。 我需要刷新,我的意思是CTRL + F5才能看到更改不好。

我尝试了.clear和.reset来重置集合和模型,但不会影响集合中的数据。 谁能帮我解决我的问题。

以下是Collection和model的代码:

var GalleryImage = Backbone.Model.extend({});

 var GalleryImages = Backbone.Collection.extend({
model : GalleryImage,
url: '######',
initialize : function(){


    this.reset();
    if(this.reset())
    {
        console.log("model reset", GalleryImage.cid)
    }else{
        console.log("model not set")
    }

    this.on("reset", this.loadImagesView,this);
    this.on("error", this.errorMessage,this);
        //console.log(GalleryImages.get(0))

},
loadImagesView : function(){
    //console.log(this);
    //console.log(this.CollView.$el);
    if(!this.CollView){
        this.CollView = new GalleryImagesView({collection:this})
    }
        this.CollView.render(); 

},
errorMessage : function(){
    jQuery('#imageBlocksDiv').html('<span id="noMsg"><h4>No images to display.</h4></span>');
}

});

对于路由器是:

initGallery : function(){

            jQuery('#invset').hide();
            jQuery('#imagelist').children().eq(0).append(jQuery('.floor_img').find('#box').css({'z-index':'1','position':'relative','margin-top':'0px','margin-left':'0px'}));
            pinterest(undefined);
    jQuery("#box").show();
            jQuery('#invset').hide();
            jQuery('#inventorycontent').hide();
    jQuery("#boxnews").hide();
    jQuery("#boxzip").hide();
    jQuery(".select").attr('style','display:block');
    jQuery(".select").attr('id','ugallerygal');
    jQuery(".pagetitle").html('Gallery');
    var that = this,projectId = jQuery('#ugallerygal').val();
    jQuery('#imageBlocksDiv').html('');
    var imgObj = new GalleryImages();

    //this.reset(imgObj);

    imgObj.fetch({data: jQuery.param({projectId: projectId, add:true, reset:true}), success:function(){
                      console.log(imgObj)
                      imgObj.trigger("reset");
          },
          error : function(){
          imgObj.collection.trigger("error");
          }
    });

},

不仅我有一个部分,当用户单击图像时,图像会打开,并且通过单击图像上的任何位置,都可以放置一个点并在该点中添加细节,我将它存储在解析中,但是更改的值会保存,但是当我回到图像上,显示的是较旧的位置和数字图像,而不是解析中存储的较新的位置和更新点。 我猜这两个州的问题都一样,所以一种解决方案将对所有人有效。

任何帮助深表感谢。 预先感谢

谢谢Santosh Upadhayay

问题可能与重置的顺序和添加重置侦听器有关。 尝试改变

this.reset();
if(this.reset())
{
    console.log("model reset", GalleryImage.cid)
}else{
    console.log("model not set")
}

this.on("reset", this.loadImagesView,this);
this.on("error", this.errorMessage,this);

this.on("reset", this.loadImagesView,this);
this.on("error", this.errorMessage,this);
this.reset();
if(this.reset())
{
    console.log("model reset", GalleryImage.cid)
}else{
    console.log("model not set")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM