how to correctly use .each here?
This is basically for a library I'm building. In this module I'll attempt to build a debug helper library to help me during debugging.
My problems here are that when FDL.D.ListEvents()
is called a couple of awkward things happen:
1) If I include enough
_logEvent()
calls, the last few stop closing their groups at the console, which makes it completely unreadable.2) It "decides" that it listed less events than it actually did.
When I use _logEvent()
on, for instance, just the mouseevents, it lists them kind of "correctly", or at least in the way I'd expect the function to work, revealing and counting around 20 events on the DOM (though the timespan is determined to be zero, which is kind of weird).
Apparently I'm too danger开发者_如何学Cous to post images :( Edit: ! Now I can!
Output image:
When I call the function with the full listing of events, I get this weird output format instead:
The following would be the standalone part of the lib (you can run it) that is supposed to do the magic
var FDL = FDL || {};
// FIREBUG DEBUG LIBRARY
FDL.D = (function()
{
function _logEvents(eventName)
{
var _arr = $(":visible");
var _count = 0;
console.groupCollapsed(eventName);
_arr.each(function(idx)
{
var _el = this;
var _id = _el.id;
if(!_id){
_id = _el.tagName;
}
var _ev = eval("_el." + eventName);
if(_ev){
console.log("%o\n" + _ev + "\n\n",$(this));
_count++;
}
});
console.log(_count + " " + eventName + " events");
console.groupEnd();
return _count;
}
return {
ListEvents : function ()
{
var _start = new Date().getTime();
var _count = 0;
console.group("Events");
_count += _logEvents("onblur");
_count += _logEvents("onchange");
_count += _logEvents("onclick");
_count += _logEvents("ondblclick");
_count += _logEvents("onerror");
_count += _logEvents("onfocus");
_count += _logEvents("onkeydown");
_count += _logEvents("onkeypress");
_count += _logEvents("onkeyup");
_count += _logEvents("onmousedown");
_count += _logEvents("onmousemove");
_count += _logEvents("onmouseout");
_count += _logEvents("onmouseover");
_count += _logEvents("onmouseup");
_count += _logEvents("onresize");
_count += _logEvents("onselect");
_count += _logEvents("onunload");
var _diff = new Date(new Date().getTime() - _start).getSeconds();
console.groupCollapsed("details");
console.log(_count + " events bound");
console.log(_diff + "s runtime");
console.groupEnd();
console.groupEnd();
}
};
})();
I tried a few ideas but I couldn't come up with the right solution to make these _logEvent()
calls chain in the expected order. The problem basically being that .each()
is called async and I suppose this is what breaks my code's execution order.
Thanks for your input! <3 stackoverflow
EDIT: I changed the groupCollapsed() calls with group() calls and it has worked indeed. Now I face a different problem, how should I make it so it only displays once for each event name the bindings as long as the bound function is the same?
For instance if somewhere we had $(":input").click(function(){alert("Clicked!");});
I'd like _logEvents("onclick")
to display the function and selector just once, but display how many times it was bound. For instance if we had 4 inputs, the output would be:
jQuery(:input) Len:1 function () {alert("Clicked!");}
The problem is not in your code, it's in the groupCollapsed routine. Use the old 'group' routine for now and you'll be fine. This bugreport looks a lot like your problem.
精彩评论