MCP.state = (function(MCP,$){
var disp = $({})
var data = {}
var refs = {}
var handlers = {}
var _id=0
function getId(){return ++_id}
var that = {
set:function(key,val){
data[key] = val
disp.trigger({type:key})
},
get:function(key){
return data[key]
},
bind:function(key,handler){
var id = getId()
var r = refs[key]
var h = handlers[key]
if (!r) r = refs[key] = {}
if (!h) h = handlers[key] = {}
r[id] = handler
h[id] = function(event){
handler(data[event.type])
}
disp.bind(key,h[id])
},
unbind:function(key,handler){
var r = refs[key]
var h = handlers[key]
for (id in r) {
if (!r.hasOwnProperty(id)) continue
if (r[id]===handler){
disp.unbind(key,h[id])
delete r[id]
delete h[id]
break
}
}
}
}
return that
}(MCP,this.jQuery));Still working on it.
refs and handlers doesn't feel right. Invaders Must Die is reminding me of being in a Capitol Hill laundromat with a Tetris machine in the back.