Debian Codegraph @

examples/prism-n4js.html

raw symbols SCIP
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&lt;string,Task&gt;();
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 &lt;html&gt;
86 &lt;body&gt;
87 Your to-do's:
88 &lt;ul&gt;
89 ${
90 todos.length === 0 ? '&lt;li&gt;&lt;em&gt;none&lt;/em&gt;&lt;/li&gt;\n'
91 : todos.map((task) =>
92 '&lt;li&gt;' + task.label + ' &lt;small&gt;(id: ' + task.id + ')&lt;/small&gt;&lt;/li&gt;'
93 ).join('\n')
94 }
95 &lt;/ul&gt;
96 &lt;hr/&gt;
97 &lt;form action="/create" method="get"&gt;
98 &lt;input type="hidden" name="type" value="Todo"&gt;
99 Label: &lt;input type="text" name="label"&gt;&lt;br&gt;
100 &lt;input type="submit" value="Create Todo"&gt;
101 &lt;/form&gt;
102 &lt;hr/&gt;
103 &lt;a href="/clear"&gt;[Clear All]&lt;/a&gt;
104 &lt;/body&gt;
105 &lt;/html&gt;
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>