jQuery 内部问题,fn.bind/fn.apply 在draggables上(试图做更好的异常处理)
我一直在尝试包装 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);
我在这里几乎迷失了方向,我找不到任何资源说明为什么这不起作用(我什至找不到任何有相同问题的人)
所以我的问题是:
- 上述方法看起来是否可行使用 jQuery 捕获错误的方法
- 有没有人遇到过同样的问题(并修复了它)
- 我是否误解了什么,我不应该在可拖动事件上调用它
问候, 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:
- Does the above method seem like an OK way to catch errors with jQuery
- Has anyone experienced the same issue (and fixed it)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回复:1 - 我们做了同样的事情,而且看起来效果很好。
回复:2 - 是的。发生的情况是,一旦包装原始函数,jQuery UI 就无法解除“mousemove.draggable”的绑定。修复方法是添加以下行(改编自 jQuery 的代理函数):
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):