jQuery 内部问题,fn.bind/fn.apply 在draggables上(试图做更好的异常处理)

发布于 2024-10-03 22:18:31 字数 2216 浏览 0 评论 0原文

我一直在尝试包装 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 = 函数() { ofn.apply(this, 参数); }; fn = wfn;

但对于所有其他事件。

代码块):

 $.fn.bind = 函数(类型, 数据, fn) {
       var ofn,wfn;
       if (!fn && data && $.isFunction(data)) {
          fn = 数据;
           数据=未定义;
      }
       if (fn && type.indexOf("error") === -1) {
          ofn = fn;
          wfn = 函数() {
             尝试 {
                ofn.apply(this, 参数);
             } 抓住(e){
                处理程序(e);
                返回假;
             }
          };
          fn = wfn;
       }
       返回 jbind.call(this, 类型, 数据, fn);

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

所以我的问题是:

  1. 上述方法看起来是否可行使用 jQuery 捕获错误的方法
  2. 有没有人遇到过同样的问题(并修复了它)
  3. 我是否误解了什么,我不应该在可拖动事件上调用它

问候, Niklas

更新 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)请在评论中告诉我。

I'm been trying to wrap javascript try/catch as seen on http://pastebin.com/f579d999d

It works well, it basically wrap everything in a try/catch letting you catch errors like this:

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

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
  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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2024-10-10 22:18:31

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

回复:2 - 是的。发生的情况是,一旦包装原始函数,jQuery UI 就无法解除“mousemove.draggable”的绑定。修复方法是添加以下行(改编自 jQuery 的代理函数):

// 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++;

Re: 1 - We do the same thing, and it seems to work pretty well.

Re: 2 - Yes. What's happening is jQuery UI isn't able to unbind "mousemove.draggable" once you wrap the original function. The fix is to add the line below (adapted from jQuery's proxy function):

// 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++;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文