2010-09-18 100 views

回答

0

事件不是廣播,而是傳遞給目標組件的味精。

argument_object = { method:'post', onComplete: function() { self.end() } } 
dest_component.emit("event", argument_object); 
dest_component.on('event', function(argument_object){ 
    // 
} 

事件架構層

首先,一切從網絡襪子

var sock = io.listen() 

sock.on("connection", function(client) { 
    client.on("message", function(msg) { (msg='join') new Player(client) } 
    client.on("disconnect", function() { } 
} 

每個Web襪子客戶裹成一個玩家開始,處理與上層玩家而發出事襪子客戶味精。

function Player(client) { 
    client.on("message", this.handleMsg.bind(this); 
} 

Player.prototype.handleMsg = function(event) { 
    this.emit('event', event, this); 
} 

遊戲包含一組玩家。玩家的事件處理程序使用上層遊戲的功能。

Game = module.exports.Game = function() { } 

Game.prototype.add = function(player) { 
    player.on("event", this.notify.bind(this); 
} 

Game.prototype.notify = function(msg, eventsrc) { 
    for(var player in players) 
     player.send(encode(msg)); 
    } 
} 
0

好的,我猜事件沒有播出,它是對某個對象的回調。