Skip to content

Instantly share code, notes, and snippets.

@zhang-ning
Last active August 29, 2015 14:09
Show Gist options
  • Save zhang-ning/42979800a930d07c061f to your computer and use it in GitHub Desktop.
Save zhang-ning/42979800a930d07c061f to your computer and use it in GitHub Desktop.
cmd.js
/*
* all rights resorved by [email protected] @猎人豆豆 @hunter.dding
* please notice that the define,main are occupied as gloable variable
* but most of time you only need to use define with CMD stand.
* river.js by Jonathan version 13.11
*/
var _$river = {
// module define and run api
sandbox: function() {
var boxes = {};
return {
create: function(key, fn) {
key = key.toLowerCase();
boxes[key] = fn;
},
run: function(fn) {
var context = {
need: function(key) {
key = key.toLowerCase();
var mod = Object.create(context);
mod.exports = {}; //reset,
var api = boxes[key] && boxes[key].call(mod,mod.exports,mod.need,mod) || undefined;
// api = typeof mod.exports === 'function' ? mod.exports : Object.keys(mod.exports).length ? mod.exports : api;
api = api || mod.exports;
return api;
},
exports: {}
};
fn.call(context,context.exports,context.need,context);
}
};
}
};
_$river.module = _$river.sandbox();
Object.create = Object.create || function(source){
var F = function () {}
F.prototype = source;
return new F();
}
/*jshint unused:false */
/**
*@name define
*@param {string} key - module name and NameSpace
*@param {function} fn - the module implementation
*/
var define = _$river.module.create;
var main = _$river.module.run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment