jQuery及jQueryUI内存泄漏问题修复

1. jQuery的remove带来的内存泄漏修复:

将remove方法内的代码修改为下面代码即可:

if ( !selector || jQuery.filter( selector, [ this ] ).length ) { // Prevent memory leaks var item = $(this); var clearItem = $('#clear-use-memory'); if(clearItem.length == 0){ jQuery('<div/>').hide().attr('id','clear-use-memory').appendTo('body'); clearItem = jQuery('#clear-use-memory'); } item.appendTo(clearItem); jQuery('*',clearItem).each(function(i, e) { (events = jQuery.data(this, 'events')) && jQuery.each(events, function(i, e1) { jQuery(e).unbind(i + '.*'); }); jQuery.event.remove(this); jQuery.removeData(this); }); clearItem[0].innerHTML = ''; item = null; }

2. jQueryUI内存泄漏:

这个bug是存在于所有的widget扩展而来的插件,下面的修复方式是官方关于此bug给出的修复方案:将200行左右的widget方法进行如下修改

$.widget = function(name, prototype) { var namespace = name.split(".")[0]; name = name.split(".")[1]; // create plugin method $.fn[name] = function(options) { var isMethodCall = (typeof options == 'string'), args = Array.prototype.slice.call(arguments, 1); // prevent calls to internal methods if (isMethodCall && options.substring(0, 1) == '_') { return this; } // handle getter methods if (isMethodCall && getter(namespace, name, options, args)) { var instance = $.data(this[0], name); return (instance ? instance[options].apply(instance, args) : undefined); } // handle initialization and non-getter methods return this.each(function() { var instance = $.data(this, name); // constructor (!instance && !isMethodCall && $.data(this, name, new $[namespace][name](this, options))._init()); // method call (instance && isMethodCall && $.isFunction(instance[options]) && instance[options].apply(instance, args)); }); }; // create widget constructor $[namespace] = $[namespace] || {}; $[namespace][name] = function(element, options) { var self = this; this.namespace = namespace; this.widgetName = name; this.widgetEventPrefix = $[namespace][name].eventPrefix || name; this.widgetBaseClass = namespace + '-' + name; this.options = $.extend({}, $.widget.defaults, $[namespace][name].defaults, $.metadata && $.metadata.get(element)[name], options); this.element = $(element) .bind('setData.' + name, this._handleData) .bind('getData.' + name, this._handleData) .bind('remove', this._handleRemove); }; // add widget prototype $[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype, { _handleData : function(e, k, v){ var instance = $.data(e.target, name); if (instance) return instance[ v ? '_setData' : '_getData' ](k,v); }, _handleRemove : function(e) { var instance = $.data(e.target, name); if (!instance) return null; var result = instance.destroy(); // default instance.element .removeData(instance.widgetName) .unbind('.' + instance.widgetName) ; return result; } }); // TODO: merge getter and getterSetter properties from widget prototype // and plugin prototype $[namespace][name].getterSetter = 'option'; };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值