Skip to content
Browse files

squashed, fragment refactor fixes, error handler, min demos

  • Loading branch information...
1 parent 63d2883 commit d085d5381084c5d6a47376e5ccd8b25cf279c87e @markhibberd committed Sep 17, 2011
View
1 .gitignore
@@ -1,2 +1,3 @@
/gen
*~
+\#*
View
21 Makefile
@@ -1,22 +1,33 @@
MODULE = disorder
VERSION = 0.1
+
ETC = etc
WWW = ${ETC}/www
-PUBLISH_WWW = web@mth.io:${MODULE}.mth.io/data
-PUBLISH_RELEASE = web@mth.io:${MODULE}.mth.io/data/release/.
+
GEN = gen
DIST = ${GEN}/dist
-DIRS = ${GEN} ${DIST}
-SRC = src/disorder.js src/disorder._.js src/disorder.fragment.js src/disorder.resolve.js src/disorder.loader.js
+DEMO = ${GEN}/demo
+
+DIRS = ${GEN} ${DIST} ${DEMO}
+
+SRC = src/disorder.js src/disorder._.js src/disorder.fragment.js src/disorder.resolve.js src/disorder.loader.js src/disorder.noconflict.js
+
+PUBLISH_WWW = web@mth.io:${MODULE}.mth.io/data
+PUBLISH_RELEASE = web@mth.io:${MODULE}.mth.io/data/release/.
+
MODULE_DIST = ${DIST}/${MODULE}.all.js
+DEMO_DIST = ${DEMO}/demo.js
.PHONY: clean size default
-default: clean ${MODULE_DIST}
+default: clean ${MODULE_DIST} ${DEMO_DIST}
${MODULE_DIST}: ${SRC} ${DIST}
cat ${SRC} > $@
+${DEMO_DIST}: ${DEMO}
+ cat demo/modules/blocks/*.js > $@
+
${DIRS}:
mkdir -p $@
View
6 test/fixed.loader/index.html → demo/fixed.loader/index.html
@@ -4,9 +4,7 @@
<script type="text/javascript" src="../../gen/dist/disorder.all.js"></script>
<!-- Note arbitrary order, dependencies loaded after referer without issue. -->
- <script type="text/javascript" src="../modules/blocks/multi.js"></script>
- <script type="text/javascript" src="../modules/blocks/red.js"></script>
- <script type="text/javascript" src="../modules/blocks/blue.js"></script>
+ <script type="text/javascript" src="../../gen/demo/demo.js"></script>
<script type="text/javascript">
var demo = disorder.loader.fixed();
@@ -16,6 +14,8 @@
var el = document.getElementById("x");
multi.blat(el);
};
+ }, function () {
+ alert("oops");
});
</script>
</head>
View
0 test/modules/blocks/blue.js → demo/modules/blocks/blue.js
File renamed without changes.
View
0 test/modules/blocks/multi.js → demo/modules/blocks/multi.js
File renamed without changes.
View
0 test/modules/blocks/red.js → demo/modules/blocks/red.js
File renamed without changes.
View
2 test/url.loader/index.html → demo/url.loader/index.html
@@ -13,6 +13,8 @@
var el = document.getElementById("x");
multi.blat(el);
};
+ }, function () {
+ alert("oops");
});
</script>
</head>
View
8 src/disorder._.js
@@ -1,18 +1,10 @@
disorder._ = (function (_) {
- _.map = function (xs, f) {
- var r = [];
- for (var i = 0; i < xs.length; ++i)
- r.push(f(xs[i]));
- return r;
- };
-
_.curry = function (f, a) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(a);
return f.apply(null, args);
};
};
-
return _;
}({}))
View
26 src/disorder.fragment.js
@@ -1,23 +1,24 @@
-disorder.fragment = (function (fragment, map, curry) {
+disorder.fragment = (function (fragment) {
var definitions = {};
var fragments = {};
var define = function (definition, args, ondefine) {
+ var name = definition.name;
fragments[name] = definition.fn.apply(null, args);
ondefine && ondefine(fragments[name]);
- }
+ };
- var fragmenter = function (resolver, name, use) {
+ var fragmenter = function (resolver, name, use, error) {
var definition = definitions[name];
var dependencies = definition.dependencies;
var args = [];
var loop = function () {
if (args.length == dependencies.length)
- define(definition, args, use)
+ define(definition, args, use);
else
fragment.using(resolver, dependencies[args.length], function (d) {
args.push(d); loop();
- });
+ }, error);
};
loop();
};
@@ -35,18 +36,19 @@ disorder.fragment = (function (fragment, map, curry) {
};
/** use a fragment */
- fragment.using = function (resolver, name, use) {
+ fragment.using = function (resolver, name, use, error) {
if (!definitions[name])
resolver(name, function () {
- fragment.using(resolver, name, use);
- });
- else if (!definitions[name])
- throw "can not load module [" + name + "]";
+ if (definitions[name])
+ fragment.using(resolver, name, use, error);
+ else
+ error();
+ }, error);
else if (!fragments[name])
- fragmenter(resolver, name, use);
+ fragmenter(resolver, name, use, error);
else
use(fragments[name]);
};
return fragment;
-}({}, disorder._.map, disorder._.curry))
+}({}))
View
10 src/disorder.js
@@ -1,3 +1,7 @@
-// XXX no conflict mode.
-this.disorder = {};
-this.disorder.global = this;
+(function (global) {
+ var conflict = this.disorder;
+ global.disorder = {};
+ global.disorder.global = global;
+ global.disorder.conflict = conflict;
+}(this))
+
View
7 src/disorder.noconflict.js
@@ -0,0 +1,7 @@
+disorder.noconflict = (function (current) {
+ return function () {
+ var global = current.global;
+ global.disorder = current.conflict;
+ return current;
+ };
+}(disorder));
View
31 src/disorder.resolve.js
@@ -1,5 +1,5 @@
disorder.resolve = (function (resolve) {
- resolve.fixed = function (scope, name, onload) {
+ resolve.fixed = function (scope, name, onload, onerror) {
var parts = name.split(".");
var check = function (obj, ps) {
@@ -8,24 +8,32 @@ disorder.resolve = (function (resolve) {
var next = ps.shift();
var r = obj[next];
if (!r)
- throw "Could not resolve [" + name + "]";
- find(r, ps);
+ onerror();
+ else
+ find(r, ps);
};
check(scope, parts);
onload();
};
- resolve.url = function (scope, prefix, munger, name, use) {
+ var ie = function (onload, onerror) {
+ return function () {
+ if (this.readyState == 'loaded' || this.readyState == 'complete') {
+ if (this.status >= 200 && this.status < 300)
+ onload();
+ if (this.status >= 400)
+ onerror();
+ }
+ };
+ };
+
+ resolve.url = function (scope, prefix, munger, name, onload, onerror) {
var script = document.createElement('script');
script.type= 'text/javascript';
- script.onreadystatechange = function () {
- if (this.readyState == 'complete' || this.readyState == 'loaded') {
- use && use();
- use = null;
- }
- }
- script.onload = use;
+ script.onreadystatechange = ie(onload, onerror);
+ script.onload = onload;
+ script.onerror = onerror;
script.src = prefix + "/" + munger(name);
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
@@ -37,3 +45,4 @@ disorder.resolve = (function (resolve) {
return resolve;
}({}))
+

0 comments on commit d085d53

Please sign in to comment.
Something went wrong with that request. Please try again.