I have been working on some jQuery widgets for work / fun lately ( github ), and there is something else that bothers me.
When I use the widget in my application, I want to be able to pass a debugging parameter that will enable or disable logging (using console.log) completely only in this plugin.
The best decision:
from:
(function( $, undefined ) {
})(jQuery);
to:
(function( $, console, undefined ) {
if (!o.debug) console = { log: function(){} };
})(jQuery, console);
One problem left
If I load two widgets like:
$myDiv.myWidget({debug: true});
$myDiv2.myWidget({debug: false});
I will not have later debugging from the 1st instance. Any ideas?
[original] So far, I have used a piece of code that I found on the Internet:
var logger = function() {
var oldConsoleLog = null;
var pub = {};
pub.enableLogger = function enableLogger() {
if(oldConsoleLog == null) return;
window['console']['log'] = oldConsoleLog;
};
pub.disableLogger = function disableLogger() {
oldConsoleLog = console.log;
window['console']['log'] = function() {};
};
return pub;
}();
Then on _create()I can do this:
if (!o.debug) {
logger.disableLogger();
}
It looked good, but it is not a plugin, as it will disable console.log for my entire application.
, .log( console.log, , /, ).
!