forked from agsdot/mithril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.html
557 lines (520 loc) · 25.3 KB
/
components.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<!doctype html>
<html>
<head>
<title>Components - Mithril</title>
<meta name="description" value="Mithril.js - a Javascript Framework for Building Brilliant Applications" />
<link href="lib/prism/prism.css" rel="stylesheet" />
<link href="style.css" rel="stylesheet" />
</head>
<body>
<header>
<nav class="container">
<a href="index.html" class="logo"><span>○</span> Mithril</a>
<a href="getting-started.html">Guide</a>
<a href="mithril.html">API</a>
<a href="community.html">Community</a>
<a href="http://lhorie.github.io/mithril-blog">Learn</a>
<a href="mithril.min.zip">Download</a>
<a href="http://github.com/lhorie/mithril.js" target="_blank">Github</a>
</nav>
</header>
<main>
<section class="content">
<div class="container">
<div class="row">
<div class="col(3,3,12)">
<h2 id="core-topics">Core Topics</h2>
<ul>
<li><a href="installation.html">Installation</a></li>
<li><a href="getting-started.html">Getting Started</a></li>
<li><a href="routing.html">Routing</a></li>
<li><a href="web-services.html">Web Services</a></li>
<li><a href="components.html">Components</a></li>
</ul>
<h2 id="advanced-topics.html">Advanced Topics</h2>
<ul>
<li><a href="auto-redrawing.html">The Auto-Redrawing System</a></li>
<li><a href="integration.html">Integrating with Other Libraries</a></li>
<li><a href="optimizing-performance.html">Compiling Templates</a></li>
</ul>
<h2 id="misc">Misc</h2>
<ul>
<li><a href="comparison.html">Differences from Other MVC Frameworks</a></li>
<li><a href="benchmarks.html">Benchmarks</a></li>
<li><a href="practices.html">Good Practices</a></li>
<li><a href="tools.html">Useful Tools</a></li>
</ul>
</div>
<div class="col(9,9,12)">
<h2 id="components">Components</h2>
<hr>
<ul>
<li><a href="#application-architecture-with-components">Application architecture with components</a><ul>
<li><a href="#aggregation-of-responsibility">Aggregation of responsibility</a></li>
<li><a href="#distribution-of-concrete-responsibilities">Distribution of concrete responsibilities</a></li>
<li><a href="#cross-communication-in-single-purpose-components">Cross-communication in single-purpose components</a></li>
<li><a href="#the-observer-pattern">The observer pattern</a></li>
<li><a href="#hybrid-architecture">Hybrid architecture</a></li>
<li><a href="#classic-mvc">Classic MVC</a></li>
</ul>
</li>
<li><a href="#example-html5-drag-n-drop-file-uploader-component">Example: HTML5 drag-n-drop file uploader component</a></li>
</ul>
<hr>
<h2 id="application-architecture-with-components">Application architecture with components</h2>
<p>Components are versatile tools to organize code and can be used in a variety of ways.</p>
<p>Let's create a simple model entity which we'll use in a simple application, to illustrate different usage patterns for components:</p>
<pre><code class="lang-javascript">var Contact = function(data) {
data = data || {}
this.id = m.prop(data.id)
this.name = m.prop(data.name)
this.email = m.prop(data.email)
}
Contact.list = function(data) {
return m.request({method: "GET", url: "/api/contact", data: data})
}
Contact.save = function(data) {
return m.request({method: "POST", url: "/api/contact", data: data})
}
</code></pre>
<p>Here, we've defined a class called <code>Contact</code>. A contact has an id, a name and an email. There are two static methods: <code>list</code> for retrieving a list of contacts, and <code>save</code> to save a single contact. These methods assume that the AJAX responses return contacts in JSON format, containing the same fields as the class.</p>
<h3 id="aggregation-of-responsibility">Aggregation of responsibility</h3>
<p>One way of organizing components is to use component parameter lists to send data downstream, and to define events to bubble data back upstream to a centralized module who is responsible for interfacing with the model layer.</p>
<pre><code class="lang-javascript">var ContactsWidget = {
controller: function update() {
this.contacts = Contact.list()
this.save = function(contact) {
Contact.save(contact).then(update.bind(this))
}.bind(this)
},
view: function(ctrl) {
return [
m.component(ContactForm, {onsave: ctrl.save}),
m.component(ContactList, {contacts: ctrl.contacts})
]
}
}
var ContactForm = {
controller: function(args) {
this.contact = m.prop(args.contact || new Contact())
},
view: function(ctrl, args) {
var contact = ctrl.contact()
return m("form", [
m("label", "Name"),
m("input", {oninput: m.withAttr("value", contact.name), value: contact.name()}),
m("label", "Email"),
m("input", {oninput: m.withAttr("value", contact.email), value: contact.email()}),
m("button[type=button]", {onclick: args.onsave.bind(this, contact)}, "Save")
])
}
}
var ContactList = {
view: function(ctrl, args) {
return m("table", [
args.contacts().map(function(contact) {
return m("tr", [
m("td", contact.id()),
m("td", contact.name()),
m("td", contact.email())
])
})
])
}
}
m.mount(document.body, ContactsWidget)
</code></pre>
<p>In the example above, there are 3 components. <code>ContactsWidget</code> is the top level module being rendered to <code>document.body</code>, and it is the module that has the responsibility of talking to our Model entity <code>Contact</code>, which we defined earlier.</p>
<p>The <code>ContactForm</code> component is, as its name suggests, a form that allows us to edit the fields of a <code>Contact</code> entity. It exposes an event called <code>onsave</code> which is fired when the Save button is pressed on the form. In addition, it stores the unsaved contact entity internally within the component (<code>this.contact = m.prop(args.contact || new Contact())</code>).</p>
<p>The <code>ContactList</code> component displays a table showing all the contact entities that are passed to it via the <code>contacts</code> argument.</p>
<p>The most interesting component is <code>ContactsWidget</code>:</p>
<ol>
<li><p>on initialization, it fetches the list of contacts (<code>this.contacts = Contact.list</code>)</p>
</li>
<li><p>when <code>save</code> is called, it saves a contact (<code>Contact.save(contact)</code>)</p>
</li>
<li><p>after saving the contact, it reloads the list (<code>.then(update.bind(this))</code>)</p>
</li>
</ol>
<p><code>update</code> is the controller function itself, so defining it as a promise callback simply means that the controller is re-initialized after the previous asynchronous operation (<code>Contact.save()</code>)</p>
<p>Aggregating responsibility in a top-level component allows the developer to manage multiple model entities easily: any given AJAX request only needs to be performed once regardless of how many components need its data, and refreshing the data set is simple.</p>
<p>In addition, components can be reused in different contexts. Notice that the <code>ContactList</code> does not care about whether <code>args.contacts</code> refers to all the contacts in the database, or just contacts that match some criteria. Similarly, <code>ContactForm</code> can be used to both create new contacts as well as edit existing ones. The implications of saving are left to the parent component to handle.</p>
<p>This architecture can yield highly flexible and reusable code, but flexibility can also increase the cognitive load of the system (for example, you need to look at both the top-level module and <code>ContactList</code> in order to know what is the data being displayed (and how it's being filtered, etc). In addition, having a deeply nested tree of components can result in a lot of intermediate "pass-through" arguments and event handlers.</p>
<hr>
<h3 id="distribution-of-concrete-responsibilities">Distribution of concrete responsibilities</h3>
<p>Another way of organizing code is to distribute concrete responsibilities across multiple modules.</p>
<p>Here's a refactored version of the sample app above to illustrate:</p>
<pre><code class="lang-javascript">var ContactForm = {
controller: function() {
this.contact = m.prop(new Contact())
this.save = function(contact) {
Contact.save(contact)
}
},
view: function(ctrl) {
var contact = ctrl.contact()
return m("form", [
m("label", "Name"),
m("input", {oninput: m.withAttr("value", contact.name), value: contact.name()}),
m("label", "Email"),
m("input", {oninput: m.withAttr("value", contact.email), value: contact.email()}),
m("button[type=button]", {onclick: ctrl.save.bind(this, contact)}, "Save")
])
}
}
var ContactList = {
controller: function() {
this.contacts = Contact.list()
},
view: function(ctrl) {
return m("table", [
ctrl.contacts().map(function(contact) {
return m("tr", [
m("td", contact.id()),
m("td", contact.name()),
m("td", contact.email())
])
})
])
}
}
m.route(document.body, "/", {
"/list": ContactList,
"/create": ContactForm
})
</code></pre>
<p>Notice that now each component is self-contained: each has a separate route, and each component does exactly one thing. These components are designed to not interface with other components. On the one hand, it's extremely easy to reason about the behavior of the components since they only serve a single purpose, but on the other hand they don't have the flexibility that the previous example did (e.g. in this iteration, <code>ContactList</code> can only list all of the contacts in the database, not an arbitrary subset.</p>
<p>Also, notice that since these components are designed to encapsulate their behavior, they cannot easily affect other components. In practice, this means that if the two components were in a <code>ContactsWidget</code> component as before, saving a contact would not update the list without some extra code.</p>
<h4 id="cross-communication-in-single-purpose-components">Cross-communication in single-purpose components</h4>
<p>Here's one way to implement cross-communication between single purpose components:</p>
<pre><code class="lang-javascript">var Observable = function() {
var controllers = []
return {
register: function(controller) {
return function() {
var ctrl = new controller
ctrl.onunload = function() {
controllers.splice(controllers.indexOf(ctrl), 1)
}
controllers.push({instance: ctrl, controller: controller})
return ctrl
}
},
trigger: function() {
controllers.map(function(c) {
ctrl = new c.controller
for (var i in ctrl) c.instance[i] = ctrl[i]
})
}
}
}.call()
var ContactsWidget = {
view: function(ctrl) {
return [
ContactForm,
ContactList
]
}
}
var ContactForm = {
controller: function() {
this.contact = m.prop(new Contact())
this.save = function(contact) {
Contact.save(contact).then(Observable.trigger)
}
},
view: function(ctrl) {
var contact = ctrl.contact()
return m("form", [
m("label", "Name"),
m("input", {oninput: m.withAttr("value", contact.name), value: contact.name()}),
m("label", "Email"),
m("input", {oninput: m.withAttr("value", contact.email), value: contact.email()}),
m("button[type=button]", {onclick: ctrl.save.bind(this, contact)}, "Save")
])
}
}
var ContactList = {
controller: Observable.register(function() {
this.contacts = Contact.list()
}),
view: function(ctrl) {
return m("table", [
ctrl.contacts().map(function(contact) {
return m("tr", [
m("td", contact.id()),
m("td", contact.name()),
m("td", contact.email())
])
})
])
}
}
m.mount(document.body, ContactsWidget)
</code></pre>
<p>In this iteration, both the <code>ContactForm</code> and <code>ContactList</code> components are now children of the <code>ContactsWidget</code> component and they appear simultaneously on the same page.</p>
<p>The <code>Observable</code> object exposes two methods: <code>register</code> which marks a controller as a Observable entity, and <code>trigger</code> which reloads controllers marked by <code>register</code>. Controllers are deregistered when their <code>onunload</code> event is triggered.</p>
<p>The <code>ContactList</code> component's controller is marked as Observable, and the <code>save</code> event handler in <code>ContactForm</code> calls <code>Observable.trigger</code> after saving.</p>
<p>This mechanism allows multiple components to be reloaded in response to non-idempotent operations.</p>
<p>One extremely important aspect of this architecture is that since components encapsulate their internal state, then by definition it's harder to reason about AJAX request redundancy (i.e. how to prevent two identical AJAX requests originating from two different components).</p>
<h3 id="the-observer-pattern">The observer pattern</h3>
<p>The <code>Observable</code> object can be further refactored so that <code>trigger</code> broadcasts to "channels", which controllers can subscribe to. This is known, appropriately, as the <a href="http://en.wikipedia.org/wiki/Observer_pattern">observer pattern</a>.</p>
<pre><code class="lang-javascript">var Observable = function() {
var channels = {}
return {
register: function(subscriptions, controller) {
return function self() {
var ctrl = new controller
var reload = controller.bind(ctrl)
Observable.on(subscriptions, reload)
ctrl.onunload = function() {
Observable.off(reload)
}
return ctrl
}
},
on: function(subscriptions, callback) {
subscriptions.forEach(function(subscription) {
if (!channels[subscription]) channels[subscription] = []
channels[subscription].push(callback)
})
},
off: function(callback) {
for (var channel in channels) {
var index = channels[channel].indexOf(callback)
if (index > -1) channels[channel].splice(index, 1)
}
},
trigger: function(channel, args) {
console.log(channel)
channels[channel].map(function(callback) {
callback(args)
})
}
}
}.call()
</code></pre>
<p>This pattern is useful to decouple chains of dependencies (however care should be taken to avoid "come-from hell", i.e. difficulty in following a chains of events because they are too numerous and arbitrarily inter-dependent)</p>
<h3 id="hybrid-architecture">Hybrid architecture</h3>
<p>It's of course possible to use both aggregation of responsibility and the observer pattern at the same time.</p>
<p>The example below shows a variation of the contacts app where <code>ContactForm</code> is responsible for saving.</p>
<pre><code class="lang-javascript">var ContactsWidget = {
controller: Observable.register(["updateContact"], function() {
this.contacts = Contact.list()
}),
view: function(ctrl) {
return [
m.component(ContactForm),
m.component(ContactList, {contacts: ctrl.contacts})
]
}
}
var ContactForm = {
controller: function(args) {
this.contact = m.prop(new Contact())
this.save = function(contact) {
Contact.save(contact).then(Observable.trigger("updateContact"))
}
},
view: function(ctrl, args) {
var contact = ctrl.contact()
return m("form", [
m("label", "Name"),
m("input", {oninput: m.withAttr("value", contact.name), value: contact.name()}),
m("label", "Email"),
m("input", {oninput: m.withAttr("value", contact.email), value: contact.email()}),
m("button[type=button]", {onclick: ctrl.save.bind(this, contact)}, "Save")
])
}
}
var ContactList = {
view: function(ctrl, args) {
return m("table", [
args.contacts().map(function(contact) {
return m("tr", [
m("td", contact.id()),
m("td", contact.name()),
m("td", contact.email())
])
})
])
}
}
m.mount(document.body, ContactsWidget)
</code></pre>
<p>Here, the data fetching is still centralized in the top-level component, so that we can avoid duplicate AJAX requests when fetching data.</p>
<p>And moving the responsibility of saving to the <code>ContactForm</code> component alleviates the need to send data back up the component tree, making the handling of non-idempotent operations less prone to pass-through argument noise.</p>
<hr>
<h3 id="classic-mvc">Classic MVC</h3>
<p>Here's one last, but relevant variation of the pattern above.</p>
<pre><code class="lang-javascript">//model layer observer
Observable.on(["saveContact"], function(data) {
Contact.save(data.contact).then(Observable.trigger("updateContact"))
})
//ContactsWidget is the same as before
var ContactsWidget = {
controller: Observable.register(["updateContact"], function() {
this.contacts = Contact.list()
}),
view: function(ctrl) {
return [
m.component(ContactForm),
m.component(ContactList, {contacts: ctrl.contacts})
]
}
}
//ContactList no longer calls `Contact.save`
var ContactForm = {
controller: function(args) {
this.contact = m.prop(new Contact())
this.save = function(contact) {
Observable.trigger("saveContact", {contact: contact})
}
},
view: function(ctrl, args) {
var contact = ctrl.contact()
return m("form", [
m("label", "Name"),
m("input", {oninput: m.withAttr("value", contact.name), value: contact.name()}),
m("label", "Email"),
m("input", {oninput: m.withAttr("value", contact.email), value: contact.email()}),
m("button[type=button]", {onclick: ctrl.save.bind(this, contact)}, "Save")
])
}
}
//ContactList is the same as before
var ContactList = {
view: function(ctrl, args) {
return m("table", [
args.contacts().map(function(contact) {
return m("tr", [
m("td", contact.id()),
m("td", contact.name()),
m("td", contact.email())
])
})
])
}
}
m.mount(document.body, ContactsWidget)
</code></pre>
<p>Here we've moved <code>Contact.save(contact).then(Observable.broadcast("updateContact"))</code> out of the <code>ContactForm</code> component and into the model layer. In its place, <code>ContactForm</code> merely emits an action, which is then handled by this model layer observer.</p>
<p>This allows swapping the implementation of the <code>saveContact</code> handler without changing the <code>ContactForm</code> component.</p>
<hr>
<h3 id="example-html5-drag-n-drop-file-uploader-component">Example: HTML5 drag-n-drop file uploader component</h3>
<p>Here's an example of a not-so-trivial component: a drag-n-drop file uploader. In addition to the <code>controller</code> and <code>view</code> properties that make the <code>Uploader</code> object usable as a component, it also has an <code>upload</code> convenience function that provides a basic upload model method, and a <code>serialize</code> function that allows files to be serialized as JSON in regular requests encoded as <code>application/x-www-form-urlencoded</code>.</p>
<p>These two functions are here to illustrate the ability to expose APIs to component consumers that complement the component's user interface. By bundling model methods in the component, we avoid hard-coding how files are handled once they're dropped in, and instead, we provide a useful library of functions that can be consumed flexibly to meet the demands on an application.</p>
<pre><code class="lang-javascript">var Uploader = {
upload: function(options) {
var formData = new FormData
for (var key in options.data) {
for (var i = 0; i < options.data[key].length; i++) {
formData.append(key, options.data[key][i])
}
}
//simply pass the FormData object intact to the underlying XMLHttpRequest, instead of JSON.stringify'ing it
options.serialize = function(value) {return value}
options.data = formData
return m.request(options)
},
serialize: function(files) {
var promises = files.map(function(file) {
var deferred = m.deferred()
var reader = new FileReader
reader.readAsDataURL()
reader.onloadend = function(e) {
deferred.resolve(e.result)
}
reader.onerror = deferred.reject
return deferred.promise
})
return m.sync(promises)
},
controller: function(args) {
this.noop = function(e) {
e.preventDefault()
}
this.update = function(e) {
e.preventDefault()
if (typeof args.onchange == "function") {
args.onchange([].slice.call((e.dataTransfer || e.target).files))
}
}
},
view: function(ctrl, args) {
return m(".uploader", {ondragover: ctrl.noop, ondrop: ctrl.update})
}
}
</code></pre>
<p>Below are some examples of consuming the <code>Uploader</code> component:</p>
<pre><code class="lang-javascript">//usage demo 1: standalone multipart/form-data upload when files are dropped into the component
var Demo1 = {
controller: function() {
return {
upload: function(files) {
Uploader.upload({method: "POST", url: "/api/files", data: {files: files}}).then(function() {
alert("uploaded!")
})
}
}
},
view: function(ctrl) {
return [
m("h1", "Uploader demo"),
m.component(Uploader, {onchange: ctrl.upload})
]
}
}
</code></pre>
<p><a href="http://jsfiddle.net/vL22kjvs/5/">Demo</a></p>
<pre><code class="lang-javascript">//usage demo 2: upload as base-64 encoded data url from a parent form
var Demo2 = {
Asset: {
save: function(data) {
return m.request({method: "POST", url: "/api/assets", data: data})
}
},
controller: function() {
var files = m.prop([])
return {
files: files,
save: function() {
Uploader.serialize(files()).then(function(files) {
Demo2.Asset.save({files: files}).then(function() {
alert("Uploaded!")
})
})
}
}
},
view: function(ctrl) {
return [
m("h1", "Uploader demo"),
m("p", "Drag and drop a file below. An alert box will appear when the upload finishes"),
m("form", [
m.component(Uploader, {onchange: ctrl.files}),
ctrl.files().map(function(file) {
return file.name
}).join(),
m("button[type=button]", {onclick: ctrl.save}, "Upload")
])
]
}
}
</code></pre>
<p><a href="http://jsfiddle.net/vL22kjvs/6/">Demo</a></p>
</div>
</div>
</div>
</section>
</main>
<footer>
<div class="container">
Released under the <a href="http://opensource.org/licenses/MIT" target="_blank">MIT license</a>
<br />© 2014 Leo Horie
</div>
</footer>
<script src="lib/prism/prism.js"></script>
</body>
</html>