File: public/assets/local/js/require.min.js

Recommend this page to a friend!
  Classes of Aby Dahana  >  Aksara  >  public/assets/local/js/require.min.js  >  Download  
File: public/assets/local/js/require.min.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Aksara
A CodeIgniter based API and CRUD generator
Author: By
Last change: Initial commit
Initial commit
Date: 2 months ago
Size: 2,007 bytes
 

Contents

Class file image Download
/**
 * require
 *
 * A script library to import the javascript / css file
 *
 * @author		Aby Dahana
 * @profile		abydahana.github.io
 *
 * Property of Aksara Laboratory
 * www.aksaracms.com
 */

"use strict";

var $loaded_script									= new Array(),
	$loaded_style									= new Array();

var require											= (function()
{
	return {
		js: function(scripts, callback)
		{
			if(!$.isArray(scripts))
			{
				scripts								= [scripts];
			}
			
			var length								= scripts.length,
				deferred							= new $.Deferred(),
				pipe								= deferred;
			
			$.each(scripts, function(i, script)
			{
				if($.inArray(script, $loaded_script) === -1)
				{
					pipe							= pipe.pipe(function()
					{
						if((i + 1) === length && callback)
						{
							/* is final import, run callback (if any) after finish */
							$.getScript(script, function()
							{
								/* push previous rendered file to history */
								$loaded_script.push(script);
								
								if(callback)
								{
									return callback();
								}
							})
						}
						else
						{
							/* continue looping to import script */
							return $.getScript(script, function()
							{
								/* push previous rendered file to history */
								$loaded_script.push(script)
							})
						}
					})
				}
				else
				{
					/* all required file is already loaded, return the callback instead */
					if(callback)
					{
						return callback()
					}
				}
			}),
			
			/* resolve deferred */
			deferred.resolve()
		},
		
		css: function(styles, callback)
		{
			$.each(styles, function(i, style)
			{
				if(!$.isArray(styles))
				{
					styles								= [styles];
				}
				
				if($.inArray(style, $loaded_style) === -1)
				{
					$('<link rel="stylesheet" type="text/css" href="' + style + '" />').appendTo('head')
				}
				
				/* push previous rendered file to history */
				$loaded_style.push(style);
			});
			if(callback)
			{
				/* return the callback if any */
				return callback();
			}
		}
	}
})();

For more information send a message to info at phpclasses dot org.