Created
May 13, 2018 18:45
-
-
Save mfeineis/2a00c28a4dba099c5a6b042e03112b6b to your computer and use it in GitHub Desktop.
Fiddling on how to decouple global routing by having the supported modules register themselves via the router - inverting the dependency (i.e. https://medium.com/@cramforce/designing-very-large-javascript-applications-6e013a3291a3)
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
// wheather-local.hybrid.js | |
import { http } from "http"; | |
function registration(require) { | |
// The router registration for the "wheather-local" widget | |
// will be moved to the main application | |
require(["env", "router"], ({ api }, { register }) => ( | |
register({ | |
fragment: `${api}/fragments/wheather-local`, | |
module: "wheather-local", | |
route: "wheather/:country/:city", | |
}) | |
)); | |
} | |
console.log(registration.toString()); | |
// The "wheather-local" widget | |
define("wheather-local", ["document"], (document) => { | |
return (root, { id }) => { | |
const widget = document.createElement("div"); | |
widget.id = id; | |
widget.classList.add("wheather-widget"); | |
widget.innerHTML += "Some wheather"; | |
root.parentNode.replaceChild(widget, root); | |
}; | |
}); | |
// wheather-local.es6.json.js | |
import { http } from "http"; | |
import document from "document"; | |
const meta = { | |
fragment: "${api}/fragments/wheather-local", | |
module: "wheather-local", | |
route: "wheather/:country/:city", | |
}; | |
const factory = (root, { id }) => { | |
const widget = document.createElement("div"); | |
widget.id = id; | |
widget.classList.add("wheather-widget"); | |
widget.innerHTML += "Some wheather"; | |
root.parentNode.replaceChild(widget, root); | |
}; | |
export { | |
meta, | |
factory, | |
} | |
// wheather-local.frontmatter.js | |
// --- | |
// fragment: "${API}/fragments/wheather-local" | |
// module: "wheather-local" | |
// route: "wheather/:country/:city" | |
// --- | |
import { http } from "http"; | |
import document from "document"; | |
export const widget = (root, { id }) => { | |
const widget = document.createElement("div"); | |
widget.id = id; | |
widget.classList.add("wheather-widget"); | |
widget.innerHTML += "Some wheather"; | |
root.parentNode.replaceChild(widget, root); | |
}; | |
// wheather-local.es6.js | |
import { http } from "http"; | |
import document from "document"; | |
function meta(require) { | |
// The router registration for the "wheather-local" widget | |
// will be moved to the main application | |
require(["env", "router"], ({ api }, { register }) => ( | |
register({ | |
fragment: `${api}/fragments/wheather-local`, | |
module: "wheather-local", | |
route: "wheather/:country/:city", | |
}) | |
)); | |
}; | |
const factory = (root, { id }) => { | |
const widget = document.createElement("div"); | |
widget.id = id; | |
widget.classList.add("wheather-widget"); | |
widget.innerHTML += "Some wheather"; | |
root.parentNode.replaceChild(widget, root); | |
}; | |
export { | |
meta, | |
factory, | |
} | |
// wheather-local.amd.js | |
function registration(require) { | |
// The router registration for the "wheather-local" widget | |
// will be moved to the main application | |
require(["env", "router"], ({ api }, { register }) => ( | |
register({ | |
fragment: `${api}/fragments/wheather-local`, | |
module: "wheather-local", | |
route: "wheather/:country/:city", | |
}) | |
)); | |
} | |
console.log(registration.toString()); | |
// The "wheather-local" widget | |
define("wheather-local", ["http", "document"], (http, document) => { | |
return (root, { id }) => { | |
const widget = document.createElement("div"); | |
widget.id = id; | |
widget.classList.add("wheather-widget"); | |
widget.innerHTML += "Some wheather"; | |
root.parentNode.replaceChild(widget, root); | |
}; | |
}); | |
function Lyb() { | |
} | |
Lyb.define = (id, deps, factory) => { }; | |
Lyb.define("Acme.Person", ["Acme.Util"], () => ({ | |
extends: [Acme.Object], | |
private: { | |
secret: "it's so secreat", | |
}, | |
protected: { | |
whyJustWhy: function () { | |
}, | |
}, | |
public: { | |
publicMethod() { | |
}, | |
}, | |
static: { | |
staticMethod: function () { | |
}, | |
}, | |
})); | |
/* | |
function Lyb() { | |
} | |
Lyb.define = (id, deps, factory) => { | |
}; | |
Lyb.define("Global.Person", ["Global.Bla"], (private, protected) => { | |
private.secret = "it's so secret"; | |
protected.whyJustWhy = function () { | |
alert("Why, just why?"); | |
}; | |
}); | |
function Router() { | |
} | |
Router.register = (route, deps, factory) => { | |
}; | |
Router.register("wheather-local", { | |
route: "wheather/:country/:city", | |
fragment: "{{api}}/fragments/wheather", | |
}); | |
Router.provide("wheather-local", ["http"], (route) => { | |
}); | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment