Skip to content

Instantly share code, notes, and snippets.

@mikaelkaron
Created July 5, 2013 02:24
Show Gist options
  • Save mikaelkaron/5931274 to your computer and use it in GitHub Desktop.
Save mikaelkaron/5931274 to your computer and use it in GitHub Desktop.
Multi context setup for troop
<!doctype html>
<html lang="en">
<body>
<script type="text/javascript">
"use strict";
var require = (function () {
/**
* Troopifies requirejs configuration
* @param {String} troopjs_location_default Default location of the trooplib (if not configured in packages)
*/
function troopify(troopjs_location_default) {
var me = this;
// Get packages
var config_packages = me.packages;
// Get pkg config for troopjs
var troopjs_pkg = config_packages.filter(function (pkg) {
return pkg.name === "troopjs";
});
// Get location of troopjs
var troopjs_location = troopjs_pkg.length > 0
? troopjs_pkg[0].location
: troopjs_location_default;
// Add troopjs submodules to packages
Array.prototype.push.apply(config_packages, [
"troopjs-core",
"troopjs-browser",
"troopjs-data",
"troopjs-jquery",
"troopjs-requirejs",
"troopjs-utils"
].map(function (pkg) {
return {
"name" : pkg,
"location" : [ troopjs_location, "lib", pkg ].join("/")
};
}));
return me;
}
var config = {
"context" : "v2",
"packages" : [{
"name" : "jquery",
"location" : "components/jquery",
"main" : "jquery"
}, {
"name" : "poly",
"location" : "components/poly",
"main" : "poly"
}, {
"name" : "when",
"location" : "components/when",
"main" : "debug"
}, {
"name" : "troopjs",
"location" : "components/troopjs",
"main" : "maxi"
}, {
"name" : "troopjs-playground",
"location" : "."
}],
"map" : {
"*" : {
"template" : "troopjs-requirejs/template",
"mv" : "troopjs-requirejs/multiversion"
}
},
"deps" : [ "require", "jquery", "troopjs" ],
"callback" : function (localRequire, jQuery) {
require.config({
"context" : "v1",
"packages" : [{
"name" : "jquery",
"location" : "components/jquery",
"main" : "jquery"
}, {
"name" : "troopjs-bundle",
"location" : "https://raw.github.com/troopjs/troopjs/build/1.x",
"main" : "troopjs-bundle"
}],
"deps" : [ "require", "jquery", "troopjs-bundle" ],
"callback" : function (_localRequire, _jQuery) {
define("config", {});
_localRequire([ "troopjs-core/pubsub/hub" ], function (hub) {
hub.subscribe("test2", window, function (topic, a, b, c, deferred) {
console.warn(a, b, c);
// deferred.resolve(123);
});
setTimeout(function () {
_jQuery.Deferred(function (dfd) {
hub.publish("test", 1, 2, 3, dfd);
})
.done(console.warn);
}, 500);
});
}
});
localRequire([ "mv!troopjs-bundle#v1"], function () {
localRequire([
"troopjs-browser/application/widget",
"troopjs-browser/route/widget",
"troopjs-core/pubsub/proxy/to1x",
"mv!troopjs-core/pubsub/hub#v1"
], function (Application, RouteWidget, Proxy, hub) {
jQuery(function ($) {
Application.extend({
"hub:memory/test2" : function (a, b, c) {
console.info(a, b, c);
this.publish("test", a, b, c).spread(console.info);
return [321];
}
})($("html"), "bootstrap", RouteWidget($(window), "route"), Proxy({
"hub" : hub,
"subscribe" : {
"test" : {
"topic" : "test2",
"memory" : true
}
},
"publish" : {
"test" : {
"topic" : "test2",
"defer" : false
}
}
})).start();
});
});
});
}
};
return troopify.call(config);
})();
</script>
<script src="components/requirejs/require.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment