Skip to content

Instantly share code, notes, and snippets.

@orbitbot
Forked from ilsenem/app.js
Created October 31, 2015 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orbitbot/6bf1b9b74a0a7b251d6f to your computer and use it in GitHub Desktop.
Save orbitbot/6bf1b9b74a0a7b251d6f to your computer and use it in GitHub Desktop.
Mithril Dependency Injector #.editorconfig
// Example
// app.js
document.addEventListener("DOMContentLoaded", function (e) {
m.route.mode = "hash";
m.route(document.getElementById("application"), "/", {
"/": m.resolve("pages.home"),
"/findus": m.resolve("pages.findus")
});
});
// pages/home.js
m.factory("pages.home", ["components.layout", function (layout) {
return {
"controller": function () {
this.title = "Home";
return this;
},
"view": function (c) {
return layout("This is my page text at: " + c.title);
}
};
}]);
// pages/findus.js
m.factory("pages.findus", ["components.layout", function (layout) {
return {
"controller": function () {
this.title = "Find us";
return this;
},
"view": function (c) {
return layout("This is page text at: " + c.title);
}
};
}]);
// components/layout.js
m.factory("conponents.layout", ["components.nav", function (nav) {
return function (text) {
return [
nav(),
text
];
};
}]);
// components/nav.js
m.factory("components.nav", ["application.config", "helpers.icon", function (cfg, icon) {
return function () {
return m("ul", cfg.nav.map(function (link) {
return m("li", [
m("a[href='"+link.href+"']", [
icon(link.icon), link.title
])
]);
}));
};
}]);
// helpers/icon.js
m.factory("helpers.icon", ["application.config", function (cfg) {
var icon = "";
switch (cfg.iconset) {
case "bootstrap":
icon = "span.glyphicon.glyphicon-";
break;
case "fontawesome":
icon = "i.fa.fa-";
break;
}
return function (type) {
return m(icon + type);
};
}]);
// config.js
m.factory("application.config", function () {
return {
"debug": true,
"iconset": "fontawesome",
"nav": [
{"title": "Home", "href": "#/", "icon": "home"},
{"title": "Find us", "href": "#/findus", "icon": "map-marker"},
]
};
});
(function (m) {
var repository = {};
m.factory = function (name, dependencies) {
if (typeof repository[name] !== "undefined") {
throw new Error("Duplicate dependency entry");
} else if (typeof dependencies === "undefined") {
throw new Error("Empty dependency list");
}
repository[name] = {
fn : typeof dependencies === "function" ? dependencies : dependencies.pop() || null,
dependencies : Array.isArray(dependencies) ? dependencies || null : null,
instance : null
};
if (repository[name].fn === null) {
throw new Error("Empty repository entry");
} else if (typeof repository[name].fn !== "function") {
throw new Error("Dependency not a function");
}
};
m.resolve = function (name, params, scope) {
var resolved;
if (typeof repository[name] === "undefined") {
throw new Error("Undefined dependency resolve: " + name);
}
if (repository[name].instance === null) {
resolved = [];
if (repository[name].dependencies !== null) {
repository[name].dependencies.forEach(function(dependency) {
return resolved.push(m.resolve(dependency));
});
}
repository[name].instance = repository[name].fn.apply(scope || {}, resolved.concat(Array.prototype.slice.resolve(params || [], 0)));
}
return repository[name].instance;
};
})(Mithril);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment