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