Created
May 24, 2011 06:43
-
-
Save blankyao/988222 to your computer and use it in GitHub Desktop.
from catchen/jsHelper(http://catchen.github.com/jsHelpers/zh/#Central)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var Central = window.Central = {}; | |
var centralService = function(target) { | |
var listeners = {}; | |
target.listen = function(command, handler) { | |
listeners[command] = listeners[command] || []; | |
var i = 0; | |
while (i < listeners[command].length && listeners[command][i] != handler) { | |
i++; | |
} | |
if (i == listeners[command].length) { | |
listeners[command].push(handler); | |
} | |
return target; | |
}; | |
target.call = function(command, argument) { | |
if (listeners[command]) { | |
for (var i = 0; i < listeners[command].length; i++) { | |
try { | |
listeners[command][i](argument); | |
} catch (error) {} | |
} | |
} | |
return target; | |
}; | |
}; | |
Central.extend = function(target) { | |
new centralService(target); | |
return target; | |
}; | |
Central.extend(Central); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment