Branch
Hash :
c3ed5b52
Author :
Date :
2018-03-11T11:11:05
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<h2>Keywords</h2>
<pre><code>
class C {..}
interface I {..}
foo(c: C, i: I) {
c instanceof C; // ok
c instanceof I; // ok
}
</code></pre>
<h2>Annotations</h2>
<pre><code>
// Final Methods
@Final
private tasks = new Map<string,Task>();
// Redefinition of Members
@Override
public async size(): int {
…
}
// Dependency Injection
@Binder
@Bind(Storage,StorageInMemory)
class InMemoryBinder {}
@GenerateInjector @UseBinder(InMemoryBinder)
export public class TaskManagerTest {
…
}
</code></pre>
<h2>Full example</h2>
<pre><code>
// A Web User Interface in HTML
// NOTE: requires full example project bundled with N4JS IDE to run.
import { TaskManager } from "TaskManager";
import {Application, Response } from "express";
import express from "express";
import { Todo } from "model";
export class WebUI {
private app: Application;
@Inject
private manager: TaskManager;
public start() {
this.app = express();
this.app.get('/', async (req, res) => {
let page = await this.renderHomePage();
res.send(page);
});
this.app.get("/clear", async (req, res) => {
await this.manager.clear();
redirect(res, '/');
});
this.app.get("/create", async (req, res) => {
let values = req.query as ~Object with {type: string, label: string};
if (values && values.type === 'Todo' && values.label && values.label.length > 0) {
await this.manager.createTodo(values.label);
}
redirect(res, '/');
});
this.app.listen(4000, '0.0.0.0', 511, function() {
console.log("HTML server listening on http://localhost:4000/");
});
}
protected async renderHomePage(): string {
let tasks = await this.manager.getTasks();
let todos = tasks.filter((task) => task instanceof Todo);
return `
<html>
<body>
Your to-do's:
<ul>
${
todos.length === 0 ? '<li><em>none</em></li>\n'
: todos.map((task) =>
'<li>' + task.label + ' <small>(id: ' + task.id + ')</small></li>'
).join('\n')
}
</ul>
<hr/>
<form action="/create" method="get">
<input type="hidden" name="type" value="Todo">
Label: <input type="text" name="label"><br>
<input type="submit" value="Create Todo">
</form>
<hr/>
<a href="/clear">[Clear All]</a>
</body>
</html>
`;
}
}
function redirect(res: Response, url: string) {
res.header('Cache-Control', 'no-cache');
res.redirect(301, url);
}
</code></pre>