Last active
November 27, 2015 20:15
-
-
Save rubeniskov/cc5cc7768cbe6d022e50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(global, modules, factory){ | |
global.module = function(){ | |
return modules[name] || (modules[name]={define:factory()}); | |
}; | |
})(this, {}, function(){ | |
return (function(prefixe, normalize, err){ | |
return (function moduleBind(modules, context, prefix) { | |
return (function(get, set, identity){ | |
return function module(name, requires, invoke) { | |
return arguments.length === 1 ? get(prefixe(prefix,name, true)) : (function(instance, context){ | |
set(prefixe(prefix,name, true), invoke.apply(context, requires.map(function(module) { | |
return module === 'define' ? instance.define : get(prefixe(name,module, true)); | |
}))); | |
return instance | |
})({define:moduleBind(modules, context, name)}, context||this, (invoke === undefined) && (invoke = identity(requires)) && (requires = [])); | |
} | |
})(function get(module){ | |
return modules[normalize(module)] || err("Missing module <{0}>, ensure you have the declaration initialized before call it", module); | |
}, function(module, instance){ | |
modules[normalize(module)] = instance; | |
}, function(identity) { | |
return function() { | |
return identity | |
} | |
}); | |
})({}, global || this, ''); | |
})(function(prefix, name, check){ | |
return (check && (/\./).test(name)) || !prefix ? name : prefix + '$' + name; | |
}, function(str){ | |
return str.replace(/[\.\/\-\_]/g,'$'); | |
}, function(msg){ | |
throw new Error((msg||'Unknow error').replace(/(\{([0-9]+)\})/g, (function(parts){ | |
return function(m){ | |
return parts[m[1]]; | |
} | |
}(Array.prototype.slice.call(arguments, 1))))); | |
}) | |
}); | |
var app = module('app'); | |
var fruits = app.define('fruits', function(){ | |
/*do nothing*/ | |
}); | |
fruits.define('oranges', 2); | |
fruits.define('bananas', 4); | |
// undefined | |
console.log(app.define('fruits')) | |
// 2 | |
console.log(fruits.define('oranges')); | |
// 4 | |
console.log(fruits.define('bananas')); | |
// Error | |
try{ | |
console.log(app.define('bananas')) | |
} catch(e){ | |
console.warn(e.message); | |
} | |
// Error | |
try{ | |
console.log(app.define('foo', ['bananas'], function(bananas){ | |
// Causes error because doesn't exist module bananas in foo | |
// So if you want include fruit.bananas you must put full path | |
})); | |
} catch(e){ | |
console.warn(e.message); | |
} | |
// true | |
console.log(fruits.define('oranges') === fruits.define('fruits.oranges') && fruits.define('oranges') === define('fruits.oranges')); | |
app.define('fruits.bananas', 70) | |
// 70 | |
console.log(fruits.define('bananas')); | |
app.define('fruits', ['define'], function(define){ | |
define('oranges', 20); | |
define('lemons', 100); | |
}); | |
// 20 | |
console.log(fruits.define('oranges')); | |
app.define('fruits', ['define', 'oranges', 'fruits.lemons'], function(define, oranges, lemons){ | |
define('fruits.bananas', 50); | |
return { | |
oranges: oranges, | |
lemons: lemons, | |
bananas: define('bananas'), | |
apples: 5 | |
} | |
}); | |
// {oranges: 20, lemons: 100,bananas: 50, apples: 5} | |
console.log(app.define('fruits')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment