日期:2014-05-16 浏览次数:20353 次
var Y = YUI(); var EventProxy = function(){ this.event_list = {}; this.data_list = []; this.callback = null; this.trigger_flag = false; } EventProxy.prototype.assign = function(){ this.callback = Array.prototype.splice.call(arguments, arguments.length - 1, 1)[0]; if(typeof this.callback !== 'function'){ return false; } for(var i = 0,len = arguments.length; i < len; i++){ this.event_list[arguments[i]] = false; } } EventProxy.prototype.trigger = function(event_name, data){ if(!this.callback || this.event_list[event_name]){ return false; } this.event_list[event_name] = true; this.data_list.push(data); this.trigger_flag = true; for(var key in this.event_list){ if(this.event_list.hasOwnProperty(key) && !this.event_list[key]){ this.trigger_flag = false; } } if(this.trigger_flag){ this.callback.apply(null, this.data_list); } } var proxy = new EventProxy(); proxy.assign('event1', 'event2', 'event3', function(v1, v2, v3){ Y.log(v1 + ' ' + v2 + ' ' + v3); }) proxy.trigger('event1', 'data1'); proxy.trigger('event2', 'data2'); proxy.trigger('event3');?