Permalink
Browse files

CommonJS Modules: scope-cjs

  • Loading branch information...
1 parent 9577b31 commit f4a6e0b39ab9365118b756931262115df63e841e @eric-brechemier committed May 11, 2012
Showing with 80 additions and 2 deletions.
  1. +4 −2 README.txt
  2. +76 −0 scope-cjs.js
View
6 README.txt
@@ -9,8 +9,8 @@ DESCRIPTION
Wrap your modules in scope() calls, and switch the implementation of scope to
run the code synchronously or asynchronously, optionally defining a module in
- global scope or in a shared context, using CommonJS Modules or Asynchronous
- Module Definition.
+ global scope or in a shared context, using CommonJS Modules [2] or
+ Asynchronous Module Definition [3].
This project defines a single function scope() with multiple implementations.
The goal is to make the definition of modules and private scopes slightly
@@ -130,6 +130,7 @@ INCLUDED IMPLEMENTATIONS
scope-ready.js - static asynchronous definition in parent context
scope-log.js - log when scope() is called and when code actually runs
scope-amd.js - Asynchronous Module Definition [3]
+ scope-cjs.js - CommonJS Modules [2]
scope-record.js - deprecated
PRIOR ART
@@ -174,4 +175,5 @@ HISTORY
2012-05-09, v0.0.8, scope-record is deprecated
2012-05-09, v0.0.9, Enhance scope-private: import inherited properties too
2012-05-10, v0.1.0, Bug fix in scope-log: add missing dependency "scope"
+ 2012-05-11, v0.2.0, CommonJS Modules: scope-cjs
View
76 scope-cjs.js
@@ -0,0 +1,76 @@
+// CommonJS Modules
+// Implementation of scopeornot API
+// https://github.com/eric-brechemier/scopeornot
+
+// This script must be loaded after scope-bootstrap.js.
+// It may be loaded after scope-bootstrap.js and scope-private.js
+// to define modules in private scope instead of global scope.
+
+/*global scope, require, exports */
+scope(function(parentContext){
+
+ var
+ // declare alias
+ parentScope = parentContext.scope;
+
+ /*
+ Function: requireMissingNeeds(needs,context)
+ Use require() to load missing dependencies in the context
+
+ Parameters:
+ needs - array of strings, names of the properties that the code expects
+ to find in context
+ context - object, set of properties in current context
+ */
+ function requireMissingNeeds(needs,context){
+ if (needs===null || typeof needs === "undefined"){
+ return;
+ }
+
+ var
+ i,
+ need,
+ needExports;
+
+ for (i=0; i<needs.length; i++){
+ need = needs[i];
+ if ( !context.hasOwnProperty(need) ){
+ needExports = require(need);
+ // scope() can be called within required module
+ // and set the needName property in context.
+ // The value defined by scope() call must be preserved.
+ if ( !context.hasOwnProperty(need) ){
+ // if no value was defined, assign the exported object instead
+ context[need] = needExports;
+ }
+ }
+ }
+ }
+
+ /*
+ Function: scope(code,needs,name)
+ Load missing needs with require(), run code and set the result to a
+ property with given name in both shared context and module exports
+
+ Parameters:
+ code - function(context), the code to run with the shared context
+ as parameter
+ needs - array of strings, the names of the properties that this code
+ would like to find in the context
+ name - string, optional, name of the property to set the value that the
+ code may return, both in shared context and in module exports
+ */
+ function scope(code,needs,name){
+ parentScope(function(context){
+ requireMissingNeeds(needs,context);
+ var result = code(context);
+ if (typeof name === "string"){
+ exports[name] = result;
+ }
+ return result;
+ },needs,name);
+ }
+
+ return scope;
+
+},["scope"],"scope");

0 comments on commit f4a6e0b

Please sign in to comment.