Skip to content

Commit

Permalink
Added the new tasks app.
Browse files Browse the repository at this point in the history
  • Loading branch information
thatismatt committed Jun 12, 2010
1 parent 6aa5dc5 commit edb8f71
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/tasks/README.md
@@ -0,0 +1,4 @@
Tasks
=====

This example app has a corresponding tutorial that steps through its creation. The tutorial can be found [here](http://thatismatt.github.com/josi/tutorial.html).
7 changes: 7 additions & 0 deletions examples/tasks/app.js
@@ -0,0 +1,7 @@
this.init = function() {
this.router.add(
// this route matches: /<controller>/<action>/<id>
/^\/(?:(\w+)\/?)?(?:(\w+)\/?)?(?:([0-9]+)\/?)?$/,
{ controller: 'home', action: 'index' }
);
};
10 changes: 10 additions & 0 deletions examples/tasks/controllers/home.js
@@ -0,0 +1,10 @@
var view = require('josi/actionresults').view;

this.index = function() {
return view({
title: 'tasks - a josi app',
controller: 'home',
action: 'index',
description: 'tasks is a <a href="http://thatismatt.github.com/josi/">josi</a> app'
});
};
31 changes: 31 additions & 0 deletions examples/tasks/controllers/task.js
@@ -0,0 +1,31 @@
var actionresults = require('josi/actionresults');
var view = actionresults.view;
var redirect = actionresults.redirect;
var tasks = require('../models/tasks');

this.index = function() {
return view({
title: 'All Tasks',
tasks: tasks.list()
});
};

this.details = function() {
var task = tasks.get(this.route[0]);
return view({
title: 'Task Details',
task: task
});
};

this.create = function() {
if (this.form.title) {
tasks.save({
title: this.form.title,
details: this.form.details || 'No details.'
});
return redirect('/task/');
} else {
return view({ title: 'Create Task' });
}
};
24 changes: 24 additions & 0 deletions examples/tasks/models/tasks.js
@@ -0,0 +1,24 @@
var tasks = [];

this.list = function() {
return tasks;
};

this.get = function(id) {
return tasks[id];
};

this.save = function(task) {
if (!task.id) {
tasks.push(task);
} else {
tasks[task.id] = task;
}
};

// test data
tasks.push(
{ title: 'Go for a jog', details: 'Run miles, and miles, and miles.' },
{ title: 'Sleep', details: 'I love sleep.' },
{ title: 'Read a book', details: 'Read a book by someone famous.' }
);
5 changes: 5 additions & 0 deletions examples/tasks/views/home/index.html
@@ -0,0 +1,5 @@
<p>Controller: <b><%= controller %></b></p>
<p>Action: <b><%= action %></b></p>
<p><%= description %></p>
<p><a href="/task/">Tasks list</a></p>

9 changes: 9 additions & 0 deletions examples/tasks/views/master.html
@@ -0,0 +1,9 @@
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1><%= title %></h1>
<%= main %>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/tasks/views/task/create.html
@@ -0,0 +1,11 @@
<form method="post" action="/task/create/">
<div>
<label for="title">Title:</label><input id="title" name="title">
</div>
<div>
<label for="details">Details:</label><input id="details" name="details">
</div>
<div>
<input type="submit" value="Create">
</div>
</form>
2 changes: 2 additions & 0 deletions examples/tasks/views/task/details.html
@@ -0,0 +1,2 @@
<h2><%= task.title %></h2>
<p><%= task.details %></p>
6 changes: 6 additions & 0 deletions examples/tasks/views/task/index.html
@@ -0,0 +1,6 @@
<ul>
<% tasks.forEach(function(t, id) { %>
<li><a href="/task/details/<%= id %>"><%= t.title %></a></li>
<% }); %>
</ul>
<p><a href="/task/create/">Create a new task</a></p>

0 comments on commit edb8f71

Please sign in to comment.