lib/Plugin.js
function(sails, done) {
		sails.log.silly('Initializing Bulkhead packages...');
		// Detach models from the Sails instance
		var bulkheads = Object.keys(Bulkhead),
			i = 0,
			detachedLoadModels = sails.modules.loadModels;

		/**
		 * Reload event that loads each Bulkhead package's models
		 */
		var reloadedEvent = function() {
			_.each(Bulkhead, function(bulkhead, bulkheadName) {
				// A registered Bulkhead package needs to be initiated, bound to the Sails instance, detached, and merged back into the Bulkhead
				_.each(bulkhead.models, function(model, index) {
					bulkhead.models[model.originalIdentity] = sails.models[model.identity];
					bulkhead[model.originalGlobalId] = bulkhead.models[model.originalIdentity];
					delete model.originalIdentity;
					delete model.originalGlobalId;
				});
	
				_.each(bulkhead.services, function(service, index) {
					service.plugin = bulkhead;
					bulkhead[service.originalIdentity] = bulkhead.services[index];
					delete service.originalIdentity;
				});
				
				sails.log.silly(Object.keys(bulkhead.models).length + ' loaded from the ' + path.basename(bulkheadName) + ' package...');
			});
			
			sails.removeListener('hook:orm:reloaded', reloadedEvent);
			sails.modules.loadModels = detachedLoadModels;
			loaded = true;
			sails.log.silly('Bulkhead packages are active.');
			done();
		};

		// Attach a custom reload event
		sails.on('hook:orm:reloaded', reloadedEvent);

		// Attaches and reloads the ORM
		if(!loaded && Bulkhead[bulkheads[i]]) {

			// We detach the model path to make sure that the loadModels module never refreshes and overrides
			sails.modules.loadModels = function(cb) {
				// Load all models into the global scope with namespacing
				sails.log.silly('Loading Bulkhead models...');
				async.series([
				    detachedLoadModels,
				    function(next) {
				    	var models = {};
				    	_.each(Bulkhead, function(bulkhead) {
							_.each(bulkhead.models, function(model) {
								models[model.identity] = model;
							});
						});
				    	next(null, models);
				    }
				], function(err, results) {
					cb(null, _.extend(results[0], results[1]));
				});
			};
			sails.hooks.orm.reload();
		} else {
			// No Bulkheads were detected
			if(loaded) {
				sails.log.silly('Bulkhead already loaded.');
			} else {
				sails.log.silly('No Bulkhead packages found!');	
			}

			done();
		}
	}
function(sails, done) {
		sails.log.silly('Initializing Bulkhead packages...');
		// Detach models from the Sails instance
		var bulkheads = Object.keys(Bulkhead),
			i = 0,
			detachedLoadModels = sails.modules.loadModels;

		/**
		 * Reload event that loads each Bulkhead package's models
		 */
		var reloadedEvent = function() {
			_.each(Bulkhead, function(bulkhead, bulkheadName) {
				// A registered Bulkhead package needs to be initiated, bound to the Sails instance, detached, and merged back into the Bulkhead
				_.each(bulkhead.models, function(model, index) {
					bulkhead.models[model.originalIdentity] = sails.models[model.identity];
					bulkhead[model.originalGlobalId] = bulkhead.models[model.originalIdentity];
					delete model.originalIdentity;
					delete model.originalGlobalId;
				});
	
				_.each(bulkhead.services, function(service, index) {
					service.plugin = bulkhead;
					bulkhead[service.originalIdentity] = bulkhead.services[index];
					delete service.originalIdentity;
				});
				
				sails.log.silly(Object.keys(bulkhead.models).length + ' loaded from the ' + path.basename(bulkheadName) + ' package...');
			});
			
			sails.removeListener('hook:orm:reloaded', reloadedEvent);
			sails.modules.loadModels = detachedLoadModels;
			loaded = true;
			sails.log.silly('Bulkhead packages are active.');
			done();
		};

		// Attach a custom reload event
		sails.on('hook:orm:reloaded', reloadedEvent);

		// Attaches and reloads the ORM
		if(!loaded && Bulkhead[bulkheads[i]]) {

			// We detach the model path to make sure that the loadModels module never refreshes and overrides
			sails.modules.loadModels = function(cb) {
				// Load all models into the global scope with namespacing
				sails.log.silly('Loading Bulkhead models...');
				async.series([
				    detachedLoadModels,
				    function(next) {
				    	var models = {};
				    	_.each(Bulkhead, function(bulkhead) {
							_.each(bulkhead.models, function(model) {
								models[model.identity] = model;
							});
						});
				    	next(null, models);
				    }
				], function(err, results) {
					cb(null, _.extend(results[0], results[1]));
				});
			};
			sails.hooks.orm.reload();
		} else {
			// No Bulkheads were detected
			if(loaded) {
				sails.log.silly('Bulkhead already loaded.');
			} else {
				sails.log.silly('No Bulkhead packages found!');	
			}

			done();
		}
	}
function ( sails , done ) { sails . log . silly ( 'Initializing Bulkhead packages...' ) ; // Detach models from the Sails instance var bulkheads = Object . keys ( Bulkhead ) , i = 0 , detachedLoadModels = sails . modules . loadModels ; /**
		 * Reload event that loads each Bulkhead package's models
		 */ var reloadedEvent = function ( ) { _ . each ( Bulkhead , function ( bulkhead , bulkheadName ) { // A registered Bulkhead package needs to be initiated, bound to the Sails instance, detached, and merged back into the Bulkhead _ . each ( bulkhead . models , function ( model , index ) { bulkhead . models [ model . originalIdentity ] = sails . models [ model . identity ] ; bulkhead [ model . originalGlobalId ] = bulkhead . models [ model . originalIdentity ] ; delete model . originalIdentity ; delete model . originalGlobalId ; } ) ; _ . each ( bulkhead . services , function ( service , index ) { service . plugin = bulkhead ; bulkhead [ service . originalIdentity ] = bulkhead . services [ index ] ; delete service . originalIdentity ; } ) ; sails . log . silly ( Object . keys ( bulkhead . models ) . length + ' loaded from the ' + path . basename ( bulkheadName ) + ' package...' ) ; } ) ; sails . removeListener ( 'hook:orm:reloaded' , reloadedEvent ) ; sails . modules . loadModels = detachedLoadModels ; loaded = true ; sails . log . silly ( 'Bulkhead packages are active.' ) ; done ( ) ; } ; // Attach a custom reload event sails . on ( 'hook:orm:reloaded' , reloadedEvent ) ; // Attaches and reloads the ORM if ( ! loaded && Bulkhead [ bulkheads [ i ] ] ) { // We detach the model path to make sure that the loadModels module never refreshes and overrides sails . modules . loadModels = function ( cb ) { // Load all models into the global scope with namespacing sails . log . silly ( 'Loading Bulkhead models...' ) ; async . series ( [ detachedLoadModels , function ( next ) { var models = { } ; _ . each ( Bulkhead , function ( bulkhead ) { _ . each ( bulkhead . models , function ( model ) { models [ model . identity ] = model ; } ) ; } ) ; next ( null , models ) ; } ] , function ( err , results ) { cb ( null , _ . extend ( results [ 0 ] , results [ 1 ] ) ) ; } ) ; } ; sails . hooks . orm . reload ( ) ; } else { // No Bulkheads were detected if ( loaded ) { sails . log . silly ( 'Bulkhead already loaded.' ) ; } else { sails . log . silly ( 'No Bulkhead packages found!' ) ; } done ( ) ; } }
Initializes all registered Bulkhead packages, grafts them to the Sails object, then moves them to their individual packages.
@param	Object		Sails instance
@param	Function	Callback to fire when initialization is finished
Initializes all registered Bulkhead packages grafts them to the Sails object then moves them to their individual packages .