Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
137 lines (113 sloc) 3.35 KB
<html>
<body>
<div id="root"></div>
<script type="text/javascript">
//Model
var model = {
inbox: false,
archive: false,
all_todos: [
{
text: "Read about SAM",
archived: false
},
{
text: 'Write code for an example',
archived: true
},
{
text: 'Create real world app using SAM',
archived: false
},
{
text: 'Celebrate',
archived: false
}
]
};
model.isArchived = function (todo) {
return todo.archived == true;
};
model.isInbox = function (todo) {
return todo.archived == false;
};
model.present = function (data) {
model.archive = data.archive || false;
model.inbox = data.inbox || false;
state.render(model);
};
//View
var view = {};
//noinspection JSUnusedLocalSymbols
view.init = function (model) {
return ("<h2>Todo</h2><form onSubmit=\"return actions.inbox({});\">\n\
<input type=\"submit\" value=\"View Inbox\">\n\
</form>\n\
<form onSubmit=\"return actions.archived({});\">\n\
<input type=\"submit\" value=\"View Archives\">\n\
</form>");
};
view.archived = function (model) {
model.todos = model.all_todos.filter(model.isArchived);
return ("<h2>Todo</h2><ul>" + model.todos.map(function (obj) {
return ('<li>' + obj.text + '</li>');
}).join('') +
"</ul><form onSubmit=\"return actions.inbox({});\">\n\
<input type=\"submit\" value=\"View Inbox\">\n\
</form>");
};
view.inbox = function (model) {
model.todos = model.all_todos.filter(model.isInbox);
return ("<h2>Todo</h2><ul>" + model.todos.map(function (obj) {
return ('<li>' + obj.text + '</li>');
}).join('') +
"</ul><form onSubmit=\"return actions.archived({});\">\n\
<input type=\"submit\" value=\"View Archives\">\n\
</form>");
};
view.display = function (root) {
var stateroot = document.getElementById("root");
stateroot.innerHTML = root;
};
view.display(view.init(model));
//State
var state = {
view: view
};
model.state = state;
state.root = function (model) {
var root = "The system is in an invalid state.";
if (model.inbox) {
root = state.view.inbox(model);
}
if (model.archive) {
root = state.view.archived(model);
}
state.view.display(root);
};
state.render = function (model) {
state.root(model);
};
//Actions
var actions = {};
//user clicked View Inbox
actions.inbox = function (data, present) {
present = present || model.present;
data = data || {};
data.inbox = true;
data.archive = false;
present(data);
return false;
};
//User clicked View Archives
actions.archived = function (data, present) {
present = present || model.present;
data = data || {};
data.inbox = false;
data.archive = true;
present(data);
return false;
};
</script>
</body>
</html>
You can’t perform that action at this time.