我一直在尝试将javascript try / catch包装在http://pastebin.com/f579d999d
上它运行良好,它基本上将所有内容包装在try / catch中,让您捕获这样的错误:
$.handleErrors(function(e){
console.log("an error occurred");
console.log(e);
});
(然后我要将它发布到服务器上)
但是,这不适用于draggables或resizables(但对于其他所有内容)。如果您开始拖动/调整元素大小,它不会在鼠标上停止(永久拖动)
看起来好像ofn.apply()不适用于draggable / resizable。
具体(缩短):
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);
我几乎迷失在这里,我找不到任何资源说明为什么这不起作用(我甚至找不到任何有同样问题的人)
所以我的问题是:
此致 尼古拉斯
更新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),请在评论中告诉我。
答案 0 :(得分:0)
Re: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++;