2012-03-21 21 views
0

在单个分页应用程序中,这是绑定和触发事件的最佳方式吗?一个网页套接字连接到服务器并初始化,然后调用页面加载等功能绑定/触发事件的最佳方式

var app = {}; 
websocket.init(function() 
{ 
    //websocket is loaded, call page load function 
    $(app).trigger('load'); 
}); 
function logout() 
{ 
    websocket.logout(function() 
    { 
    //now logged out 
    $(app).trigger('loggedout'); 
    } 
} 

//page.js 
$(app).bind('load', function() 
{ 

}); 
$(app).bind('loggedout', function() 
{ 

}); 
+0

的方法看起来不错,但你应该使用更近jQuery.on()和jQuery.off()方法从jQuery API – 2012-03-21 15:31:00

+0

我想你会只使用$ .bind('customevent')和$ .trigger('customevent'),我不知道为什么你会把你的应用程序对象变成一个jQuery对象... – Aknosis 2012-03-21 15:31:11

+0

如果我没有把它变成一个jQuery对象,我将如何将事件绑定到它? – firebird 2012-03-21 18:07:56

回答

0
var app = {}; 
websocket.init(function() 
{ 
    //websocket is loaded, call page load function 
    $(app).load(); 
}); 
function logout() 
{ 
    websocket.logout(function() 
    { 
    //now logged out 
    $(app).loggedout(); 
    } 
} 

//page.js 
$(app).load(function() 
{ 

}); 
$(app).loggedout(function() 
{ 

});