Simplified working demo code here.
Relevant JS code:
;(function(exports) {
//--------------------------------------------------------------------------
//
// Private properties:
//
//--------------------------------------------------------------------------
var _msg = '',
_ol = document.getElementById('debug');
//--------------------------------------------------------------------------
//
// Public methods:
//
//--------------------------------------------------------------------------
exports.init = function(arg) {
_msg = arg;
// Add a listener to the `window.resize` event, pass `exports`/`self` as the scope:
_addEvent(window, 'resize', _listenForChange, exports);
};
//--------------------------------------------------------------------------
//
// Private methods:
//
//--------------------------------------------------------------------------
function _addEvent(elem, type, handle, context) {
// Attache event:
if (elem) {
if (elem.addEventListener) {
// If the browser supports event listeners, use them:
elem.addEventListener(type, function() { handle.call(context); }, false);
} else if (elem.attachEvent) {
// IE & Opera:
elem.attachEvent('on' + type, function() { handle.call(context); });
} else {
// Otherwise, replace the current thing bound to `on[whatever]`!
elem['on' + type] = function() { handle.call(context); };
}
}
}
function _listenForChange() {
var li = document.createElement('li');
li.innerHTML = String(_msg);
_ol.appendChild(li);
if (typeof console !== 'undefined') console.log(_msg);
}
return exports; // Expose the API.
}(window.FOO = window.FOO || {}));
//--------------------------------------------------------------------
window.onload = function() {
FOO.init('world');
FOO.init('universe');
};
Problem with existing code:
When re-sizing the window, I only see "universe".
Goal:
When browser window re-sizes, I want to world "world" and "universe" output as a list item (and to the console).
I want to have the ability to instantiate FOO
multiple times and have it work independently from any other running instances.
My questions:
Is there a pattern (or patterns), similar to the one(s) that I'm currently using, that will allow me to run multiple instances of FOO
without having the last FOO
called trump all previously called FOO
s?
The code I posted is obviously not real world; my end goal is to allow/modify a plugin I wrote to be used without the end user having to worry if FOO
is already being used by another script.
Please let me know if I can clarify anything and/or provide more specific code.