<!DOCTYPE html>
<html>
<head>
<link href="//cdn.jsdelivr.net/picnicss/4.1.1/picnic.min.css" rel="stylesheet">
<link href="//rawgit.com/magnumjs/mag.js/master/src/mag.comps.css" rel="stylesheet">
<script src="//rawgit.com/magnumjs/mag.js/master/mag.min.js"></script>
</head>
<body>
<h1>Hello Mag.JS!</h1>
<a target="_tab" href="https://github.com/magnumjs/mag.js">GitHub</a>
<hr/>
<div id="app" class="hide">
<h2>Boilerplate: <count></count></h2>
<ul>
<li></li>
</ul>
</div>
<script src="//rawgit.com/magnumjs/mag.js/master/src/mag.addons.js"></script>
<script src="//rawgit.com/magnumjs/mag.js/master/src/mag.comps.js"></script>
<script src="parent-mod.js"></script>
<script src="child-mod.js"></script>
<script src="sib-mod.js"></script>
<script src="sub-mod.js"></script>
<script src="app-init.js"></script>
</body>
</html>
// initialize mag module reference
mag.mods.demo.parent({ things: []});
#Mag.JS Modular namespacing
Best practice in creating objects
1. always have one global top level namespace
e.g. var MyCoolApp = MyCoolApp || {};
2. That is included on every page
3.All sub modules are based on that
But they use local vars and return in functions
4. Always have a top parent moduel container
5. components are reusable within modules
(function(namespace) {
var mod = {}
// default props to be over ridden
var props = {
things: []
}
mod.controller = function(props) {
mag.addons.show(this)
this.li = props.things
this.h2 = {
_text: 'My MagJS mod!'
}
}
mod.view = function(state, props) {
mag.mods.demo.child({
key: "things",
things: props.things,
onThingClick: function(things) {
state.li = things
}
});
}
namespace.parent = mag.comp('app', mod, props);
})(mag.namespace('mods.demo'));
(function(namespace) {
var props = {
onThingClick: function(things) {}
}
var mod = {
view: function(state, props) {
state.h2 = {
_onclick: function() {
state.count = state.count + 1 || 1
props.things.push(state.count)
props.onThingClick(props.things)
}
}
}
}
namespace.child = mag.comp('app', mod, props);
})(mag.namespace('mods.demo'));
(function(namespace) {
var mod = {
view: function() {
}
}
namespace.sub = mod;
})(mag.namespace('mods.demo'));
(function(namespace) {
var mod = {
view: function() {
}
}
namespace.sib = mod;
})(mag.namespace('mods.demo'));