<!DOCTYPE html>
<html>
<head>
<link href="//cdn.jsdelivr.net/picnicss/4.1.1/picnic.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title>Mag.JS</title>
</head>
<body>
<nav>
<h1>Hello Mag.JS!</h1>
<a target="_tab" href="https://github.com/magnumjs/mag.js">GitHub</a>
</nav>
<article class="card">
<header>
<h2>Lists of Things!</h2>
</header>
<footer>
<div id="app">
<input>
<button class="mainButton">Add List
</button>
<div>
<ul class="lists">
<li>
<a></a>
</li>
</ul>
<ol class="items">
<li>
<a></a>
</li>
</ol>
</div>
</div>
</footer>
</article>
<script src="//rawgit.com/magnumjs/mag.js/master/mag-latest.min.js"></script>
<script src="//rawgit.com/magnumjs/mag.js/master/dist/mag.addons.0.22.min.js"></script>
<script src="todos.js"></script>
<script src="app.js"></script>
</body>
</html>
var app = {}
app.controller = function(props) {
this.selectedListId = null
this.button = {
_onClick: () => {
if (this.input) {
if (this.selectedListId === null) {
props.todos.addList(this.input);
} else {
props.todos.addItemToList(this.selectedListId, this.input);
}
this.input = '';
}
}
}
}
app.view = function(state, props) {
state.lists = {
li: props.todos.lists.map(function(list, id) {
return {
a: {
_onClick: function() {
if (state.selectedListId != id) {
//toggle
state.selectedListId = id;
state.button._text = 'Add Item to ' + list.name
} else {
state.selectedListId = null
state.button._text = 'Add List'
}
},
_text: list.name
}
}
})
}
state.items = {
li: state.selectedListId !== null ? props.todos.getListById(state.selectedListId).items.map(function(item) {
return {
a: {
_onClick: function() {
alert(item.id)
},
_text: item.name
}
}
}) : []
}
}
var props = {
todos: todos
}
mag.module("app", app, props)
li:empty, ol:empty,
.hide {
display: none;
}
a {
display: block;
}
a:after {
content: " \bb";
}
.mainButton {
font-size: 1.5em;
}
nav a {
float: right;
margin-top: -50px;
}
body {
background: #fff;
text-align: left;
width: 90%;
max-width: 960px;
margin: 0 auto;
padding: 20px 0 0;
}
nav {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 3em;
padding: 0 .6em;
background: #fff;
box-shadow: 0 0 0.2em rgba(17, 17, 17, 0.2);
z-index: 10000;
transition: all .3s;
transform-style: preserve-3d;
}
header {
font-weight: bold;
position: relative;
border-bottom: 1px solid #eee;
padding: .6em .8em;
}
footer {
padding: .8em;
}
article {
top: 100px;
}
.card {
max-width: 100%;
display: block;
position: relative;
box-shadow: 0;
border-radius: .2em;
border: 1px solid #ccc;
overflow: hidden;
text-align: left;
background: #fff;
margin-bottom: .6em;
padding: 6px;
transition: all .3s ease;
}
ul, ol {
width:50%;
float:left;
}
#Mag.JS
## plunk boilerplate of "Things"
Basic Mag.JS boilerplate to count and create a list of things dynamically.
Example of external state and updates
var todos = {
lists: [],
addList: function(listName) {
return todos.lists.push({
name: listName,
items: []
})
},
getListById: function(listId) {
return todos.lists[listId];
},
addItemToList: function(listId, item) {
var List = todos.getListById(listId);
List.items.push({
name: item
});
}
};