Giter Site home page Giter Site logo

redcatjs / js Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 114 KB

$js - asynchronous dependencies manager

Home Page: http://redcatphp.com/js-amd-framework

License: Other

JavaScript 100.00%
dependencies-manager lazy-loading async asynchronous-module-definition js javascript require-js

js's Introduction

No longer actively maintained. I recommand you to take a look on Webpack and Babel

$js - Dependencies manager

From lightweight javascript dependencies manager to asynchronous module definition framework.
$js is greatly lighter than the famous AMD framework requirejs or, of course, the wonderful jQuery framework. By using it as the only script required by your page to load you can improve significantly the performance of your page loading and make your javascript realy non obstrusive. Because the javascript work start after page loaded, if you use jquery, no more need of "$(document).ready()" function, the document will always be ready when your code will work on it.
If you allready loaded a script via $js, and you need it again in other part of your code, it will not reload it unecessary.

Simple Usage

Put this code at the top bottom of your page just before the ending body tag.

<script src="js/js.pack.js" type="text/javascript"></script>
<script type="text/javascript">
    $js.dev = true;
    $js('script');
</script>

Dependencies manager

Config

Here is the default config.

$js.async = true;
$js.path = 'js/';
$js.pathSuffix = '.js';
$js.pathDetection = true;
$js.dev = false;

With pathDetection enabled if the path is allready present at start of script url, it will not be repeated, same at end for the pathSuffix.
If a script url start with trailing slash "/", no path or pathSuffix will be added to it, allowing you to use pseudo absolute path in your domain. In the same way if a script url contain the "://" sign of absolute url, it will not be changed.
If dev set to true, a time GET parameter will be added to script calling, like in jquery, to avoid the cache mechanism.
If you put async to true, all scripts called from main flow will be loaded in order. It's not recommanded to do that.

Simple call

$js('js/script.js');
$js('js/script');
$js('script.js');
$js('script');

Async Callbacks

In async mode it will not wait for dependency1 loaded to start loading dependency2. In this case, dependency2 does not depend on dependency1. The order of arguments doesn't matter.

$js(['dependency1','dependency2'],function(){
	//when dependency1 and dependency2 are loaded
});
$js({
	dependency1:function(){
		//when dependency1 loaded
	},
	dependency2:function(){
		//when dependency2 loaded (after dependency loaded)
	},
},function(){
	//when dependency1 and dependency2 are loaded
});

Sync Callbacks

The boolean true enable sync mode. In sync mode it will wait for dependency1 loaded to start loading dependency2. In this case, dependency2 does depend on dependency1. The order of arguments doesn't matter.

$js(true,['dependency1','dependency2'],function(){
	//when dependency1 and dependency2 are loaded
});
$js(true,{
	'dependency1':function(){
		//when dependency1 loaded
	},
	'dependency2':function(){
		//when dependency2 loaded (after dependency loaded)
	},
},function(){
		//when dependency1 and dependency2 are loaded
});

Alias

Alias are recursively resolved and can be a reference to several scripts.

$js.alias('j','jquery');
$js.alias('jui',['j','jquery-ui/core','jquery-ui/widget']);

Dependencies Map

A call to dependencies method will add the given map to allready defined. This map will be used on script calls to resolve dependencies and load them when needed but will not load the keys of map and all the map.

$js.dependencies({
	'jquery-ui/mouse':['jui'],
	'jquery-ui/sortable':['jui','jquery-ui/mouse'],
	'jquery-ui/resizable':['jui','jquery-ui/mouse'],
	'jquery.sortElements':['j'],
});

$js([
	'jquery.sortElements',
	'jquery-ui/sortable',
	'jquery-ui/resizable',
],function(){
	//...
});

Chainable

$js('dependency1')('dependency2')(function(){
	//when dependency1 and dependency2 are loaded
});

//is equivalent of
$js(true,['dependency1','dependency2'],function(){
	//when dependency1 and dependency2 are loaded
});

//and equivalent of
$js('dependency1',function(){
	$js('dependency2',function(){
		//when dependency1 and dependency2 are loaded
	});
});

//but the difference it that you can assign the resolved function to variable
var whenReady = $js('dependency1');
whenReady = whenReady('dependency2');
whenReady(function(){
	//when dependency1 and dependency2 are loaded
});

Asynchronous Module Definition

A module can be a function or an object and can be defined in several ways. Order of arguments doesn't matter.

Simple module definition and usage

// definition
$js.module('random',function(){
	return Math.random();
});

// usage
var number = $js.module('random')();

Module definition and usage with automatic name

// file script.js
$js('random',function(){	
	//usage
	var number = $js.module('random')();
});

// file random.js
// here the name will be automaticaly set to 'random'
$js.module(function(){
	return Math.random();
});

Complete module definition with dependencies

// file script.js
$js('moduleA',function(){
	// will output 'test of moduleA'
	console.log( $js.module('moduleA').val );
	
	$js.module('moduleA').test();
});

// file moduleA.js
// with dependencies in sync mode
$js.module(true,['moduleB','otherDependency'],{
	val:'test of moduleA',
	test:function(){
		// will output 'result of test moduleB'
		console.log( $js.module('moduleB')() );
	}
});

// file moduleB.js
$js.module(function(){
	return 'result of moduleB';
});

js's People

Contributors

devthejo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.