Backbone / Dropzone:如何正确配置Dropzone实例

时间:2016-01-06 15:27:44

标签: javascript backbone.js dropzone.js

我的应用程序实现了大量的dropzone元素。通过导航,我实现了Dropzone,但是当用户离开其他页面时,我需要正确处理Dropzones以保持我的文档事件/ DOM清洁。

我在骨干/ requirejs应用程序中使用了Dropd的amd版本。

文档说调用disable()方法删除所有dropzone事件侦听器。我调用disable方法,然后从DOM中删除元素。这是正确清洁Dropzone的安全方法吗? Dropzone模块是否仍然保留对此删除的Dropzone元素的引用?

这是我用来渲染dropzone元素的代码"作为视图"在我的骨干应用程序中我需要制作"删除"功能正确清理Dropzone实例:

define(['backbone','dropzone-amd-module'], function(Backbone, Dropzone){
    // Prevent Dropzone from auto discovering this element:
    Dropzone.options.myAwesomeDropzone = false;
    // This is useful when you want to create the
    // Dropzone programmatically later
    // Disable auto discover for all elements:
    Dropzone.autoDiscover = false;

    return Backbone.View.extend({
        tagName: 'div',
        className: 'dropzone',
        dropzone_reference: null,
        render: function(){
            this.initDropzone();
            return this;
        },
        initDropzone: function(){
            // init dropzone (avoid to init more than once!)
            if( !this.dropzone_reference)this.dropzone_reference = new Dropzone(this.el, this.options);
        },
        remove: function(){
            // remove dropzone instance
            this.dropzone_reference.disable();
            Backbone.View.prototype.remove.apply(this, arguments);
        }

    });
}

1 个答案:

答案 0 :(得分:1)

Backbones view.remove()

remove: function() {
  this._removeElement();
  this.stopListening();
  return this;
},

从DOM中删除视图的元素。

正如你所说,drop方法确实删除了事件处理程序:

  

如果您不再需要dropzone,只需在对象上调用.disable()即可。这将删除元素上的所有事件侦听器

由于view.remove()从DOM中删除了元素本身,所以即使.disable()也没有必要。

引用dropzone模块的唯一内容是视图的dropzone_reference属性。当没有对视图的引用时,它将被垃圾收集,使dropzone模块没有引用,这将反过来收集垃圾。

请确保不要对已销毁的视图进行任何引用。

相关问题