繁体   English   中英

jQuery内部问题,fn.bind / fn.apply应用于可拖动对象(试图做更好的异常处理)

[英]Problems with jQuery internals, fn.bind/fn.apply on draggables (trying to do better exception handling)

我一直在尝试包装javascript try / catch,如http://pastebin.com/f579d999d所示

它运作良好,基本上将所有内容包装在try / catch中,让您捕获如下错误:

$.handleErrors(function(e){
    console.log("an error occurred");
    console.log(e);
});

(然后我将其发布到服务器)

但是,这不适用于可拖动对象或可调整大小的对象(但适用于所有其他内容)。 如果您开始拖动/调整元素的大小,则它不会在鼠标向上移动时停止(永远拖动)

似乎ofn.apply()在可拖动/可调整大小上不起作用。

具体来说(缩短):

  ofn = fn; wfn = function() { ofn.apply(this, arguments); }; fn = wfn; 

但对于所有其他事件。

代码块):

  $.fn.bind = function(type, data, fn) { var ofn, wfn; if (!fn && data && $.isFunction(data)) { fn = data; data = undefined; } if (fn && type.indexOf("error") === -1) { ofn = fn; wfn = function() { try { ofn.apply(this, arguments); } catch(e) { handler(e); return false; } }; fn = wfn; } return jbind.call(this, type, data, fn); 

我在这里几乎迷失了,我找不到任何资源说明为什么这不起作用(我什至找不到任何遇到相同问题的人)

所以我的问题是:

  1. 上面的方法看起来像是一种使用jQuery捕获错误的好方法
  2. 是否有人遇到过相同的问题(并已解决)
  3. 我是否误解了一些东西,我不应该在可拖动事件中称其为

此致,尼古拉斯

更新2011-08-28,完整代码(有效)现在为:

jQuery.fn.bind = function( type, data, fn ) { 
    if ( !fn && data && typeof data == 'function' ) {
        fn = data;
        data = null;
    }

    if ( fn )
    {
        var origFn = fn;
        var wrappedFn = jQuery.proxy(origFn, function () { 
            try {
                origFn.apply( this, arguments );
            }catch ( ex ) {
                return trackError( ex );
           }
        });
        fn = wrappedFn;
    }
    return jQueryBind.call( this, type, data, fn );
};

如果有人有更多改进技巧(原始功能来自http://blogs.cozi.com/tech/2008/04/javascript-error-tracking-why-windowonerror-is-not-enough.html )请在评论中让我知道。

回复:1-我们做同样的事情,而且看起来效果很好。

回复:2-是的。 发生的情况是,包装原始功能后,jQuery UI无法解除绑定“ mousemove.draggable”。 解决方法是添加以下行(改编自jQuery的proxy函数):

// Set the guid of unique handler to the same of original handler, so it can be removed
wfn.guid = ofn.guid = ofn.guid || wfn.guid || jQuery.guid++;

暂无
暂无

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

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