简体   繁体   English

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

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

I'm been trying to wrap javascript try/catch as seen on http://pastebin.com/f579d999d 我一直在尝试包装javascript try / catch,如http://pastebin.com/f579d999d所示

It works well, it basically wrap everything in a try/catch letting you catch errors like this: 它运作良好,基本上将所有内容包装在try / catch中,让您捕获如下错误:

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

(and then I'm going to post it to server) (然后我将其发布到服务器)

However, this does not work for draggables or resizables (but for everything else). 但是,这不适用于可拖动对象或可调整大小的对象(但适用于所有其他内容)。 If you start to drag/resize an element, it doesn't stop on mouse up (making the drag forever) 如果您开始拖动/调整元素的大小,则它不会在鼠标向上移动时停止(永远拖动)

It appears as if the ofn.apply() doesn't work on draggable/resizable. 似乎ofn.apply()在可拖动/可调整大小上不起作用。

Specifically (shortened): 具体来说(缩短):

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

But for all other events. 但对于所有其他事件。

Code block): 代码块):

  $.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); 

I'm pretty much lost here, and I can't find any resource saying why this shouldn't work (I can't even find anyone who has the same issues) 我在这里几乎迷失了,我找不到任何资源说明为什么这不起作用(我什至找不到任何遇到相同问题的人)

So my question is: 所以我的问题是:

  1. Does the above method seem like an OK way to catch errors with jQuery 上面的方法看起来像是一种使用jQuery捕获错误的好方法
  2. Has anyone experienced the same issue (and fixed it) 是否有人遇到过相同的问题(并已解决)
  3. Do I misunderstand something and I should simply not call this on draggable events 我是否误解了一些东西,我不应该在可拖动事件中称其为

Regards, Niklas 此致,尼古拉斯

Update 2011-08-28, the full code (working) is now: 更新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 );
};

If anyone has more tips on how to improve it (the original function is from http://blogs.cozi.com/tech/2008/04/javascript-error-tracking-why-windowonerror-is-not-enough.html ) please let me know in a comment. 如果有人有更多改进技巧(原始功能来自http://blogs.cozi.com/tech/2008/04/javascript-error-tracking-why-windowonerror-is-not-enough.html )请在评论中让我知道。

Re: 1 - We do the same thing, and it seems to work pretty well. 回复:1-我们做同样的事情,而且看起来效果很好。

Re: 2 - Yes. 回复:2-是的。 What's happening is jQuery UI isn't able to unbind "mousemove.draggable" once you wrap the original function. 发生的情况是,包装原始功能后,jQuery UI无法解除绑定“ mousemove.draggable”。 The fix is to add the line below (adapted from jQuery's proxy function): 解决方法是添加以下行(改编自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