This repository was archived by the owner on Jul 7, 2021. It is now read-only.
forked from rcarmo/pink
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-started.html
More file actions
468 lines (394 loc) · 20.4 KB
/
get-started.html
File metadata and controls
468 lines (394 loc) · 20.4 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Pink - Single Page App framework</title>
<meta name="description" content="">
<meta name="author" content="ink, cookbook, recipes">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<!-- load inks css from the cdn -->
<link rel="stylesheet" type="text/css" href="http://cdn.ink.sapo.pt/3.0.2/css/ink.css">
<link rel="stylesheet" type="text/css" href="http://cdn.ink.sapo.pt/3.0.2/css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="content/css/docs.css">
<link rel="stylesheet" type="text/css" href="libs/prettify/prettify.css">
<link rel="stylesheet" type="text/css" href="libs/prettify/themes/ghink2.css">
<!-- load inks javascript files from the cdn -->
<script type="text/javascript" src="http://cdn.ink.sapo.pt/3.0.2/js/ink-all.js"></script>
<script type="text/javascript" src="libs/prettify/prettify.js"></script>
</head>
<body>
<div class="ink-grid">
<!--[if lte IE 9 ]>
<div class="ink-alert basic">
<button class="ink-dismiss">×</button>
<p>
<strong>You are using an outdated Internet Explorer version.</strong>
Please <a href="http://browsehappy.com/">upgrade to a browser</a> to improve your web experience.
</p>
</div>
-->
<header id="mainMenu">
<h1 class="hide-tiny hide-small"><a href="index.html"><img src="content/img/logo-small.png" height="50" alt=""></a><small>Single page app framework</small></h1>
<nav class="ink-navigation quarter-top-space">
<ul class="menu horizontal black hide-tiny hide-small">
<li class="active"><a href="get-started.html">Get started</a></li>
<li><a href="docs.html">Docs</a></li>
<li><a href="samples.html">Sample apps</a></li>
<li><a href="https://github.com/sapo/pink" target="_blank">GitHub</a></li>
</ul>
<ul class="menu horizontal black hide-medium hide-large hide-xlarge">
<li id="verticalMenuTrigger" class="menu-bars" data-target="#verticalMenuMenu">
<a href="#"><i class="fa fa-bars"></i></a>
</li>
<li class="small-logo-right">
<a href="index.html"><img src="content/img/logo-small.png" height="30" alt=""></a>
</li>
</ul>
</nav>
<nav class="ink-navigation hide-all" id="verticalMenuMenu">
<ul class="menu vertical black">
<li class="active"><a href="get-started.html">Get started</a></li>
<li><a href="docs.html">Docs</a></li>
<li><a href="samples.html">Sample apps</a></li>
<li><a href="https://github.com/sapo/pink" target="_blank">GitHub</a></li>
</ul>
</nav>
</header>
<div id="headerPadding" class="hide-small hide-tiny"> </div>
<div id="smallHeaderPadding" class="hide-medium hide-large hide-xlarge"> </div>
<section class="column-group article quarter-space">
<div class="large-100">
<article>
<header>
<h1>How to build a simple app</h1>
</header>
<summary>
Pink is a Javascript framework to build single page apps, we'll show you
how to get started with a simple tutorial based on the "Image search" sample app.
</summary>
<br>
<h2>Application structure</h2>
<pre>
+App
+--ImageSearch
+----Search
|------lib.js
|------tpl.html
+----View
|------lib.js
|------tpl.html
+----Shell
|------lib.js
|------tpl.html
|----lib.js
+content
+--css
|----pink.css
|----tasks.css
+--img
+libs
|index.html
</pre>
<p>
Pink was heavly influenced from Ink's module structure and Durandal.js framework.
With that foundation we built a modular structure based on conventions for the names of files and the
directory tree that is used to arrange components. An app is comprised of modules, a module is a piece
of UI logic that serves a single well defined purpose. Pink modules can be purely logic expressed in
javascript (lib.js) or can also have an UI template (tpl.html).
</p>
<h2>index.html</h2>
<p>
At the root of the application is the main html file, in this case "index.html".
This file is the only page the browser loads throught the aplication lifecycle.
</p>
<blockquote><pre class="prettyprint lang-html">
...
<body>
<div id="applicationHost" data-bind="module: 'App.Images.Shell'"></div>
<script src="libs/ink-all.js"></script>
<script type="text/javascript">
Ink.setPath('App', 'App/');
Ink.setPath('Pink', 'libs/Pink/');
Ink.requireModules(['App.Images'], function(app) {
app.run();
});
</script>
</body>
</html>
</pre></blockquote>
<p>
In the body of the page we find the div with the initial data binding directive, that tells the framework
what is the first UI module to load. Usually this module has the application layout, with menus and the active route's
module.
</p>
<p>
The "Ink.setPath" command defines where Ink can find the modules needed by the app, this should include the application
folder (usually "App") and Pink's libraries folder (usually "libs/Pink").
</p>
<p>
After that, we find the "Ink.requireModules" command, that instructs Ink to load the needed application modules, and
when the module is ready, call the application run method.
</p>
<h2>App/Images/lib.js</h2>
<blockquote><pre class="prettyprint lang-js">
Ink.createModule('App.Images', '1', ['Pink.App_1', 'Pink.Plugin.Signals_1', 'Pink.Data.Binding_1'], function(App, Signal, ko) {
var Module = function() {
App.call(this, 'search', 'search');
this.appTitle = 'Image search sample';
};
Module.prototype = new App();
Module.constructor = Module;
Module.prototype.listInvisibleRoutes = function() {
return [
{hash: 'search', module: 'App.Images.Search'},
{hash: 'view', module: 'App.Images.View'}
];
};
Module.prototype.addCustomSignals = function() {
this.signals.viewPhoto = new Signal();
};
Module.prototype.ready = function() {
var self=this;
Ink.requireModules(['App.Images.View'], function() {
self.start();
});
};
return new Module();
});
</pre></blockquote>
<p>
The application module must inherit it's prototype from the "Pink.App" module.
As this simple application as no visible menus, we only need to redefine the app's
"listInvisibleRoutes" method that must return an array of route definitions.
<br>
A route definition matches an url fragment (hash) with a module name, this match is made through
a regular expression that can have parameter placeholders.
</p>
<p>
We also override the "addCustomSignals" method from "Pink.App", this allows us to add new client signals
to the app. Although not required, is's a good pratice to decouple the app's modules, so that they have
very limited knowledge about the other parts of the application, allowing those parts to be easly replaced.
For this decoupling we usually use custom events to signal the ocurrence of some actions, like the success
of an entity save, or the change in a search widget's terms.
</p>
<p>
In this example we create a new signal called "viewPhoto" that will notify the "App.Images.View" module to show
the photo's details. The photo details will be passed along with event.
</p>
<p>
The "ready" method from "Pink.App" is also overriden to add custom startup logic. In our example we require the
preload of the "App.Images.View" module, so that it can subscribe to "viewPhoto" events. After that we call "Pink.App"
"start" method, that begins the data binding process, in our case, loading the "App.Images.Shell" method referenced in
"index.html".
</p>
<h2>App/Images/Shell/lib.js</h2>
<blockquote><pre class="prettyprint lang-js">
Ink.createModule('App.Images.Shell', '1', ['App.Images'], function(app) {
var Module = function() {
var self=this;
this.appTitle = app.appTitle;
this.mainModule = app.mainModule;
this.modalModule = app.modalModule;
this.alertModule = app.alertModule;
this.infoModule = app.infoModule;
};
Module.prototype.afterRender = function() {
app.signals.shellRendered.dispatch();
};
return new Module();
});
</pre></blockquote>
<p>
The "App.Images.Shell" module, is the main app layout were we define the app's title, the main module
that is loaded from the app's current active route, and optionally modal window modules.
The "afterRender" method is called by the framework after the module's template is rendered, so we use that
to notify other parts of the app that the app's layout is ready.
</p>
<h2>App/Images/Shell/tpl.html</h2>
<blockquote><pre class="prettyprint lang-html">
<div id="appContainer" class="ink-grid toggle no-padding">
<div id="toastPanel"></div>
<div id="standbyLightBox">
<div class="lightbox-container">
<i class="icon-spinner icon-spin icon-4x"></i>
</div>
</div>
<div class="column-group">
<nav id="mainMenu" class="ink-navigation">
<ul class="menu horizontal white">
<li><a href="#search"><i class="icon-home"></i></a></li>
</ul>
<h4 class="menu-title" data-bind="text: appTitle"></h4>
</nav>
<section data-bind="module: mainModule">
</section>
<div data-bind="module: modalModule"></div>
<div data-bind="module: alertModule"></div>
<div data-bind="module: infoModule"></div>
</div>
</div>
</pre></blockquote>
<p>
The "App.Images.Shell" template (App/Images/Shell/tpl.html), has the app's main layout markup,
responsible for UI composition through the use
of the "module" binding directive. Here we instruct the framework to load the active route's module (mainModule).
If we want to show modal dialogs, we should also include, the "Pink.App"'s "modalModule", "alertModule"
and "infoModule".
</p>
<h2>App/Images/Search/lib.js</h2>
<blockquote><pre class="prettyprint lang-js">
Ink.createModule('App.Images.Search', '1', ['App.Images', 'Pink.Data.Binding', 'Ink.Net.JsonP_1'], function(app, ko, JsonP) {
var Module = function() {
var self=this;
this.photos = ko.observableArray();
this.noPhotos = ko.computed(function() {
return self.photos().length==0;
});
this.search = ko.observable('');
this.searching = ko.observable(false);
this.search.subscribe(this.searchChangeHandler.bind(this));
};
Module.prototype.searchChangeHandler = function(search) {
if (this.timer) {
window.clearTimeout(this.timer);
}
if (search.length > 2) {
this.timer = window.setTimeout(this.doSearch.bind(this, search), 1000);
}
};
Module.prototype.doSearch = function(search) {
var self=this;
var uri = 'http://services.sapo.pt/Photos/JSON2?u='+search;
this.searching(true);
this.photos([]);
new JsonP(uri, {
params: {limit: '40'},
onSuccess: function(data) {
var aItems = data.rss.channel.item;
var photos = [];
var i;
if (aItems) {
for(i=0, total=aItems.length; i < total; i++) {
photos.push({
tbUrl: aItems[i]['media:thumbnail'][2].url,
url: aItems[i]['media:content'].url,
title: aItems[i].title,
clickHandler: self.viewPhoto.bind(self)
});
}
}
self.searching(false);
self.photos(photos);
},
onFailure: function() {
self.searching(false);
app.showErrorToast('Failure in photos service');
}
});
};
Module.prototype.viewPhoto = function(photo) {
app.signals.viewPhoto.dispatch(photo);
};
return new Module();
});
</pre></blockquote>
<p>
The 'App.Images.Search' module is the application home module, where the user can input a simple
keyword query to make a search in SAPO's photo service.
</p>
<p>
When the user clicks on a photo, the module emits a "viewPhoto" signal with the selected photo.
</p>
<h2>App/Images/Search/tpl.html</h2>
<blockquote><pre class="prettyprint lang-html">
<div class="half-space">
<div class="ink-form">
<div class="control-group column-group half-right-space">
<div class="control">
<input type="search" placeholder="Search terms..." data-bind="value: search, valueUpdate: 'afterkeydown'"></input>
</div>
</div>
</div>
<!-- ko if: searching -->
<i class="icon-spinner icon-spin icon-4x"></i>
<!-- /ko -->
<!-- ko ifnot: searching -->
<div class="top-space" data-bind="foreach: photos">
<div class="thumbnail">
<a href="javascript:void(0)" data-bind="click: clickHandler">
<img data-bind="attr: {src: tbUrl}" height="120">
</a>
</div>
</div>
<div class="top-space" data-bind="visible: noPhotos">
No results
</div>
<!-- /ko -->
</div>
</pre></blockquote>
<p>
The search template binds the view model's (App/Images/Search/lib.js), "search", "searching", "photos",
and "noPhotos" properties to build the home UI.
</p>
<h2>App/Images/View/lib.js</h2>
<blockquote><pre class="prettyprint lang-js">
Ink.createModule('App.Images.View', '1', ['App.Images', 'Pink.Data.Binding_1'], function(app, ko) {
var Module = function() {
app.signals.viewPhoto.add(this.viewPhoto.bind(this));
this.photoSrc = ko.observable('');
this.photoTitle = ko.observable('');
};
Module.prototype.viewPhoto = function(photo) {
this.photoSrc(photo.url);
this.photoTitle(photo.title);
app.navigateTo('#view');
};
return new Module();
});
</pre></blockquote>
<p>
The "App.Images.View" module subscribes to "viewPhoto" signals, and when it'receives them, updates the
"photoSrc" and "photoTitle" properties and tells the app to change the active route to the "view" route.
</p>
<h2>App/Images/View/tpl.html</h2>
<blockquote><pre class="prettyprint lang-html">
<div class="space">
<h3 data-bind="text: photoTitle"></h3>
</div>
<div class="space">
<img data-bind="attr: {src: photoSrc}">
</div>
</pre></blockquote>
</article>
</div>
</section>
</div>
<footer class="clearfix">
<div class="ink-grid">
<div class="column-group">
<div class="large-50 vertical-space">
<h3>Pink is built with:</h3>
<ul class="unstyled">
<li><a href="http://ink.sapo.pt">Ink</a></li>
<li><a href="http://knockoutjs.com">Knockout</a></li>
<li><a href="http://projects.jga.me/routie/">Routie</a></li>
<li><a href="http://millermedeiros.github.io/js-signals/">Signals</a></li>
</ul>
</div>
<div class="large-50 vertical-space content-right">
<p class="note">Pink is licensed under the MIT Licence.</p>
</div>
</div>
</div>
</footer>
<script type="text/javascript">
prettyPrint();
Ink.requireModules(['Ink.UI.Toggle_1'], function (Toggle) {
new Toggle('#verticalMenuTrigger');
});
</script>
</body>
</html>