Add finalizer to chat module in Chat JavaScript application

This commit is contained in:
Dmitriy Simushev 2013-02-15 11:56:42 +00:00
parent 5d0ac36fa4
commit 23812038d9
7 changed files with 161 additions and 60 deletions

View File

@ -5,5 +5,6 @@
Copyright (c) 2005-2011 Mibew Messenger Community Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php License: http://mibew.org/license.php
*/ */
(function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this))},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread, (function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)b.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});
c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_); for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread,c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,
a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_);

View File

@ -5,4 +5,4 @@
Copyright (c) 2005-2011 Mibew Messenger Community Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php License: http://mibew.org/license.php
*/ */
(function(a,b){a.Models.Avatar=a.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){a.Objects.server.registerFunction("setupAvatar",b.bind(this.apiSetupAvatar,this))},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_); (function(b,c){b.Models.Avatar=b.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){this.registeredFunctions=[];this.registeredFunctions.push(b.Objects.server.registerFunction("setupAvatar",c.bind(this.apiSetupAvatar,this)))},finalize:function(){for(var a=0;a<this.registeredFunctions.length;a++)b.Objects.server.unregisterFunction(this.registeredFunctions[a])},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_);

View File

@ -5,8 +5,10 @@
Copyright (c) 2005-2011 Mibew Messenger Community Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php License: http://mibew.org/license.php
*/ */
(function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=a.Application;j.module("Chat",{startWithParent:!1}).addInitializer(function(d){var g=a.Objects,c=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(d.layoutsData.chat||{})});a.Objects.chatLayout=f;j.mainRegion.show(f);c.thread=new a.Models.Thread(d.thread);c.user=new a.Models.ChatUser(d.user);c.page=new a.Models.Page(d.page);var e=new a.Collections.Controls; (function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=[],k=a.Application,l=k.module("Chat",{startWithParent:!1});l.addInitializer(function(c){var g=a.Objects,d=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(c.layoutsData.chat||{})});a.Objects.chatLayout=f;k.mainRegion.show(f);d.thread=new a.Models.Thread(c.thread);d.user=new a.Models.ChatUser(c.user);d.page=new a.Models.Page(c.page);var e=new a.Collections.Controls;
c.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:d.links.mailLink}),e.add(b.sendMail));c.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:d.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:d.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140}); d.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:c.links.mailLink}),e.add(b.sendMail));d.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:c.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:c.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140});
e.add(b.refresh);d.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:d.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status})); e.add(b.refresh);c.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:c.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status}));
c.user.get("isAgent")||(c.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:c.avatar})));g.Collections.messages=new a.Collections.Messages;c.messageForm=new a.Models.MessageForm(d.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:c.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));c.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:c.sound}));g.server.callFunctionsPeriodically(function(){var b= d.user.get("isAgent")||(d.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:d.avatar})));g.Collections.messages=new a.Collections.Messages;d.messageForm=new a.Models.MessageForm(c.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:d.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));d.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:d.sound}));j.push(g.server.callFunctionsPeriodically(function(){var b=
a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))})})})(Mibew); a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))}))});l.addFinalizer(function(){a.Objects.chatLayout.close();
for(var c=0;c<j.length;c++)a.Objects.server.stopCallFunctionsPeriodically(j[c]);"undefined"!=typeof a.Objects.Models.avatar&&a.Objects.Models.avatar.finalize();a.Objects.Collections.messages.finalize();delete a.Objects.chatLayout;delete a.Objects.Models.thread;delete a.Objects.Models.user;delete a.Objects.Models.page;delete a.Objects.Models.avatar;delete a.Objects.Models.messageForm;delete a.Objects.Models.sound;delete a.Objects.Models.Controls;delete a.Objects.Models.Status;delete a.Objects.Collections.messages;
delete a.Objects.Collections.controls;delete a.Objects.Collections.status})})(Mibew);

View File

@ -25,7 +25,7 @@ MibewAPIChatInteraction=function(){this.obligatoryArguments={"*":{threadId:null,
Copyright (c) 2005-2011 Mibew Messenger Community Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php License: http://mibew.org/license.php
*/ */
(function(a,b){a.Models.Avatar=a.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){a.Objects.server.registerFunction("setupAvatar",b.bind(this.apiSetupAvatar,this))},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_); (function(b,c){b.Models.Avatar=b.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){this.registeredFunctions=[];this.registeredFunctions.push(b.Objects.server.registerFunction("setupAvatar",c.bind(this.apiSetupAvatar,this)))},finalize:function(){for(var a=0;a<this.registeredFunctions.length;a++)b.Objects.server.unregisterFunction(this.registeredFunctions[a])},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_);
/* /*
This file is part of Mibew Messenger project. This file is part of Mibew Messenger project.
http://mibew.org http://mibew.org
@ -116,8 +116,9 @@ MibewAPIChatInteraction=function(){this.obligatoryArguments={"*":{threadId:null,
Copyright (c) 2005-2011 Mibew Messenger Community Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php License: http://mibew.org/license.php
*/ */
(function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this))},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread, (function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)b.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});
c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_); for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread,c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,
a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_);
/* /*
This file is part of Mibew Messenger project. This file is part of Mibew Messenger project.
http://mibew.org http://mibew.org
@ -256,11 +257,13 @@ c=""!=this.ui.message.val();c!=a.get("typing")&&a.set({typing:c})},setFocus:func
Copyright (c) 2005-2011 Mibew Messenger Community Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php License: http://mibew.org/license.php
*/ */
(function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=a.Application;j.module("Chat",{startWithParent:!1}).addInitializer(function(d){var g=a.Objects,c=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(d.layoutsData.chat||{})});a.Objects.chatLayout=f;j.mainRegion.show(f);c.thread=new a.Models.Thread(d.thread);c.user=new a.Models.ChatUser(d.user);c.page=new a.Models.Page(d.page);var e=new a.Collections.Controls; (function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=[],k=a.Application,l=k.module("Chat",{startWithParent:!1});l.addInitializer(function(c){var g=a.Objects,d=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(c.layoutsData.chat||{})});a.Objects.chatLayout=f;k.mainRegion.show(f);d.thread=new a.Models.Thread(c.thread);d.user=new a.Models.ChatUser(c.user);d.page=new a.Models.Page(c.page);var e=new a.Collections.Controls;
c.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:d.links.mailLink}),e.add(b.sendMail));c.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:d.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:d.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140}); d.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:c.links.mailLink}),e.add(b.sendMail));d.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:c.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:c.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140});
e.add(b.refresh);d.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:d.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status})); e.add(b.refresh);c.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:c.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status}));
c.user.get("isAgent")||(c.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:c.avatar})));g.Collections.messages=new a.Collections.Messages;c.messageForm=new a.Models.MessageForm(d.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:c.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));c.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:c.sound}));g.server.callFunctionsPeriodically(function(){var b= d.user.get("isAgent")||(d.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:d.avatar})));g.Collections.messages=new a.Collections.Messages;d.messageForm=new a.Models.MessageForm(c.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:d.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));d.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:d.sound}));j.push(g.server.callFunctionsPeriodically(function(){var b=
a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))})})})(Mibew); a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))}))});l.addFinalizer(function(){a.Objects.chatLayout.close();
for(var c=0;c<j.length;c++)a.Objects.server.stopCallFunctionsPeriodically(j[c]);"undefined"!=typeof a.Objects.Models.avatar&&a.Objects.Models.avatar.finalize();a.Objects.Collections.messages.finalize();delete a.Objects.chatLayout;delete a.Objects.Models.thread;delete a.Objects.Models.user;delete a.Objects.Models.page;delete a.Objects.Models.avatar;delete a.Objects.Models.messageForm;delete a.Objects.Models.sound;delete a.Objects.Models.Controls;delete a.Objects.Models.Status;delete a.Objects.Collections.messages;
delete a.Objects.Collections.controls;delete a.Objects.Collections.status})})(Mibew);
/* /*
This file is part of Mibew Messenger project. This file is part of Mibew Messenger project.
http://mibew.org http://mibew.org

View File

@ -24,13 +24,34 @@
* Collection initializer. * Collection initializer.
*/ */
initialize: function() { initialize: function() {
/**
* Contains ids of periodically called functions
* @type Array
*/
this.periodicallyCalled = [];
// Periodically try to get new messages // Periodically try to get new messages
this.periodicallyCalled.push(
Mibew.Objects.server.callFunctionsPeriodically( Mibew.Objects.server.callFunctionsPeriodically(
_.bind(this.updateMessagesFunctionBuilder, this), _.bind(this.updateMessagesFunctionBuilder, this),
_.bind(this.updateMessages, this) _.bind(this.updateMessages, this)
)
); );
}, },
/**
* Collection finalizer
*/
finalize: function() {
// Stop call functions periodically
for (var i = 0; i < this.periodicallyCalled.length; i++) {
Mibew.Objects.server.stopCallFunctionsPeriodically(
this.periodicallyCalled[i]
);
}
},
/** /**
* Update messages if they are exist. * Update messages if they are exist.
* @param args {Object} An object of passed arguments * @param args {Object} An object of passed arguments

View File

@ -30,13 +30,32 @@
* Model initializer. * Model initializer.
*/ */
initialize: function() { initialize: function() {
/**
* Contain ids of registered by the model api functions
* @type Array
*/
this.registeredFunctions = [];
// Register API function // Register API function
this.registeredFunctions.push(
Mibew.Objects.server.registerFunction( Mibew.Objects.server.registerFunction(
'setupAvatar', 'setupAvatar',
_.bind(this.apiSetupAvatar, this) _.bind(this.apiSetupAvatar, this)
)
); );
}, },
// Model finalizer
finalize: function() {
// Unregister api functions
for(var i = 0; i < this.registeredFunctions.length; i++) {
Mibew.Objects.server.unregisterFunction(
this.registeredFunctions[i]
);
}
},
/** /**
* Set avatar * Set avatar
* This is an API function. * This is an API function.

View File

@ -18,6 +18,12 @@
*/ */
Mibew.Objects.Models.Status = {}; Mibew.Objects.Models.Status = {};
/**
* Contains ids of periodically called functions
* @type Array
*/
var periodicallyCalled = [];
// Create shortcut for Application object // Create shortcut for Application object
var app = Mibew.Application; var app = Mibew.Application;
@ -43,6 +49,7 @@
// Show layout at page // Show layout at page
app.mainRegion.show(layout); app.mainRegion.show(layout);
// Initialize Thread and User // Initialize Thread and User
models.thread = new Mibew.Models.Thread(options.thread); models.thread = new Mibew.Models.Thread(options.thread);
models.user = new Mibew.Models.ChatUser(options.user); models.user = new Mibew.Models.ChatUser(options.user);
@ -177,6 +184,7 @@
// TODO: May be move it somewhere else // TODO: May be move it somewhere else
// Periodically call update function at the server side // Periodically call update function at the server side
periodicallyCalled.push(
objs.server.callFunctionsPeriodically( objs.server.callFunctionsPeriodically(
function() { function() {
// Get thread and user objects // Get thread and user objects
@ -219,7 +227,54 @@
canPost: args.canPost || false canPost: args.canPost || false
}); });
} }
)
); );
});
// Add module finalizer
chat.addFinalizer(function() {
// Close layout
Mibew.Objects.chatLayout.close();
// Stop call functions periodically
for (var i = 0; i < periodicallyCalled.length; i++) {
Mibew.Objects.server.stopCallFunctionsPeriodically(
periodicallyCalled[i]
);
}
// Run models finalizers
// TODO: may be automate this process
// Finalize avatar model
if (typeof Mibew.Objects.Models.avatar != 'undefined') {
Mibew.Objects.Models.avatar.finalize();
}
// Run collections finalizers
// Finalize messages collection
Mibew.Objects.Collections.messages.finalize();
// Clean up memory
// Delete layout
delete Mibew.Objects.chatLayout;
// Delete models
delete Mibew.Objects.Models.thread;
delete Mibew.Objects.Models.user;
delete Mibew.Objects.Models.page;
delete Mibew.Objects.Models.avatar;
delete Mibew.Objects.Models.messageForm;
delete Mibew.Objects.Models.sound;
delete Mibew.Objects.Models.Controls;
delete Mibew.Objects.Models.Status;
// Delete collections
delete Mibew.Objects.Collections.messages;
delete Mibew.Objects.Collections.controls;
delete Mibew.Objects.Collections.status;
}); });
})(Mibew); })(Mibew);