Skip to content

Tiny (~2.5kb) js script / module management / loading system

Notifications You must be signed in to change notification settings

gillescochez/req.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

req.js is tiny script / module management system which can:

  • Lazy load javascript files and execute callback when the requested files are loaded
  • Execute callbacks in the order they were declared for multiple identical requests
  • Store objects declared using req API and make them available as arguments in the callback function when requested
  • Return its settings object and its module storage objects
  • Set and get configuration parameter
  • Can return a loaded/declared object from its storage
  • Module declaration dependencies loading

Usage

// load modules
req(['user','panel'], function(user, panel) {

});

// load modules in the callback scope and an external file in the global scope
req(['user','http://domain.com/data.js','panel'], function(user, panel) {

});

// set a configuration parameter
req('path','http://resources.domain.com/js/');

// set multiple configuration parameter
req({
	path: './',
	order: false
});

// get a configuration parameter
var p = req('get','path');

// get the settings object
var config = req().settings;

// declare a module
req('dummy', {
	foo: function() {
		console.log('foo');
	}
});

// request a module (the module must be loaded)
var mod = req('dummy');

// get an object containing all the modules
var list = req().objects;

// get an object of both settings and objects setting in one go (as you might have guess by now...)
var obj = req();

Module dependencies

req('mod', ['dummyObject','dummyClass'], function(dumObj, dumCla) {
	return this;
});

Native

req.js also extends the Array, Object and Function objects so you can do funky things.

(function(){ return this; }).req('fooClass');

({foo:true}).req('fooObject');

['fooObject','fooClass'].req(function(obj, clas) {
	console.log(obj, clas);
});

Minimalistic chainability

// change path
req('path','./')

// declare a module
('core', {
	init:function(){
	
	}
)

// load the declared module just for irony sake
(['core','module'], function(core, mod) {
	core.init();
	mod.load();
});

About

Tiny (~2.5kb) js script / module management / loading system

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published