Created
November 1, 2012 04:52
-
-
Save eduardolundgren/3991905 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://yui.yahooapis.com/3.1.0/build/yui/yui-min.js"></script> | |
</head> | |
<body> | |
<h1>Modules</h1> | |
<script type="text/javascript"> | |
var modules = { | |
a: { | |
requires: [ 'b', 'c' ] | |
}, | |
b: { | |
requires: [ 'c', 'd' ] | |
}, | |
c: { | |
requires: [ 'd', 'e' ] | |
}, | |
d:{ | |
// cyclic reference | |
requires: [ 'c' ] | |
}, | |
e: { | |
} | |
}; | |
YUI().use('base', 'collection', function(Y) { | |
window.Modules = { | |
getRequires: function(name) { | |
return Y.Array.unique( | |
this._getRequires.apply(this, [name]) | |
); | |
}, | |
_getRequires: function(name) { | |
var instance = this, module = modules[name], o = []; | |
if (module) { | |
var req1 = module.requires; | |
for (var i=0; (req1 && (i < req1.length)); i++) { | |
var m = req1[i]; | |
if (modules[m]) { | |
var req2 = modules[m].requires; | |
if (req2 && (req2.indexOf(name) === -1)) { | |
o = o.concat( | |
instance._getRequires(m) | |
); | |
} | |
o.push(m); | |
} | |
}; | |
o.push(name); | |
} | |
return o; | |
} | |
}; | |
// Testing | |
console.log( | |
Modules.getRequires('a') // ["d", "e", "c", "b", "a"] | |
); | |
console.log( | |
Modules.getRequires('c') // ["d", "e", "c"] | |
); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment