There are several approach to achieve this kind of thing. Based on your architecture, requirements, preference you can do either of the following:
Approach 1 :
You can accept a callback function as a init function and there you can handle it.
var library = function(initCallback) {
ev.subscribe('init', initCallback);
//library code here
//Publish some sort of initialisation event
ev.publish('init');
//return an object that includes the ev pubsub functionality
};
Then you can call like
var test = new library(function() {});
Approach 2 :
Separate the init function from instantiate. This architecture used most. You can expose selected method only as public.
var library = function() {
//library code here
//Publish some sort of initialisation event
function init() {
ev.publish('init');
}
//return an object that includes the ev pubsub functionality
return {
init : init,
ev : ev,
// .... expose other function/properties
};
};
Then you can use this like:
var test = new library();
//the ev object is returned from the instantiation of the library
test.ev.subscribe('init',function() {});
//Call the actual initialization
test.init();
Approach 3:
As you stated you do not want to follow neither of above approaches, then there may be only one way to achieve what you want, by moving the event handler from your object. As you haven't gave your event handler code, I am giving an example implementation with jquery event handler.
var library = function() {
//library code here
//Publish some sort of initialisation event
var self = this;
$(window).trigger( "init.library", [ self ] );
//return an object that includes the ev pubsub functionality
};
hen you can use this like:
$(window).on( "init.library", function( event, obj ) {
//Do whatever you want with "obj"
});
var test = new library();
If you do not want to use the last approach, then best of luck :). And if you find any other interesting solution, please post it for others. Actually another thing you can do, if your purpose is to just call the init function, after instantiate of the object(not while instantiating) then you can change ev.publish('init');
line as
setTimeout(function() {
ev.publish('init');
},1);
Then your code will also work!!
Happy coding!!