forked from gitana/alpaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorials.html
736 lines (696 loc) · 25.8 KB
/
tutorials.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Alpaca - Easy HTML5 Forms for jQuery - Tutorials</title>
<meta name="keywords"
content="jquery forms,JSON forms"/>
<meta name="description"
content="Alpaca is a client-side, cross-platform, JSON based forms engine for rendering elegant forms. It comes with comprehensive control library and support for validation, wizard, layout, templating, preview, i18n etc. Built upon jQuery library, jQuery UI and jQuery Mobile, it can be easily extended for additional form controls."/>
<meta name="author" content="Gitana Software Inc."/>
<link rel="icon" type="image/x-icon" href="favicon.ico"/>
<script type="text/javascript" src="lib/jquery-latest.min.js">
</script>
<!--Alpaca JavaScript-->
<script type="text/javascript" src="lib/base.js"></script>
<script type="text/javascript" src="lib/validator.js"></script>
<script type="text/javascript" src="lib/jquery.tmpl.js"></script>
<script type="text/javascript" src="lib/equiv_and_hoozit.js"></script>
<script type="text/javascript" src="lib/jquery.maskedinput-1.3.js"></script>
<script type="text/javascript" src="js/Alpaca.js"></script>
<script type="text/javascript" src="js/views/DefaultView.js"></script>
<script type="text/javascript" src="js/views/ListView.js"></script>
<script type="text/javascript" src="js/views/MobileView.js"></script>
<script type="text/javascript" src="js/views/LayoutView.js"></script>
<script type="text/javascript" src="js/View.js"></script>
<script type="text/javascript" src="js/Field.js"></script>
<script type="text/javascript" src="js/ControlField.js"></script>
<script type="text/javascript" src="js/ContainerField.js"></script>
<script type="text/javascript" src="js/Connector.js"></script>
<script type="text/javascript" src="js/Form.js"></script>
<!--Alpaca Base Fields-->
<script type="text/javascript" src="js/fields/basic/TextField.js"></script>
<script type="text/javascript" src="js/fields/basic/TextAreaField.js"></script>
<script type="text/javascript" src="js/fields/basic/CheckBoxField.js"></script>
<script type="text/javascript" src="js/fields/basic/FileField.js"></script>
<script type="text/javascript" src="js/fields/basic/ListField.js"></script>
<script type="text/javascript" src="js/fields/basic/RadioField.js"></script>
<script type="text/javascript" src="js/fields/basic/SelectField.js"></script>
<script type="text/javascript" src="js/fields/basic/NumberField.js"></script>
<script type="text/javascript" src="js/fields/basic/ArrayField.js"></script>
<script type="text/javascript" src="js/fields/basic/ObjectField.js"></script>
<script type="text/javascript" src="js/fields/basic/AnyField.js"></script>
<!--End of Alpaca Base Fields-->
<script type="text/javascript" src="js/views/DefaultViewLanguagePacks.js"></script>
<!--End of Alpaca JavaScript-->
<script type="text/javascript" src="examples/lib/google-code-prettify/prettify.min.js"></script>
<script type="text/javascript" src="examples/lib/tweet/jquery.tweet.js"></script>
<script type="text/javascript" src="examples/js/footer.js"></script>
<!--Alpaca CSS-->
<link type="text/css" href="css/960.fluid.css" rel="stylesheet"/>
<link type="text/css" href="css/alpaca.css" rel="stylesheet"/>
<link type="text/css" href="css/jquery-ui-latest.custom.css" rel="stylesheet"/>
<!--End of Alpaca CSS-->
<link type="text/css" href="examples/css/site.css" rel="stylesheet"/>
<link type="text/css" href="examples/lib/google-code-prettify/prettify.css" rel="stylesheet"/>
<link type="text/css" href="examples/lib/tweet/jquery.tweet.css" rel="stylesheet"/>
<script type="text/javascript" src="https://use.typekit.com/fnm3kpm.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
</head>
<body>
<header>
<div class='container_12'>
<div class='actual-body'>
<div class='grid_12'>
<h1><img src="examples/img/alpaca.png"><span>Easy HTML5 Forms for jQuery</span></h1>
<div class="resource-bar">
<a href="index.html">Home</a>
<a href="resources.html#downloads">Downloads</a>
<a href="tutorials.html" class="current">Tutorials</a>
<a href="examples/components/fields/text-field.html">Components</a>
<a href="resources.html#documentation">Documentation</a>
<a href="resources.html#unittests">Tests</a>
<a href="examples/forms/customer-profile/edit-form.html">Forms</a>
<a href="resources.html#mobile">Mobile</a>
</div>
</div>
</div>
</div>
</header>
<div class='container_12'>
<div class="actual-body">
<div class='grid_12'>
<div class="widget">
<div class="widget-title">
<h2>Alpaca Form Walkthrough Example</h2>
</div>
<div class="widget-body">
<div class="step">
<img src="examples/img/step-1.png"/>
<p>
Use your favorite text editor to create an empty html file and then include latest jQuery library,
Alpaca JavaScript library and Alpaca stylesheet in its head section.
</p>
<pre class="prettyprint linenums">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://www.alpacajs.org/js/alpaca.min.js"></script>
<link type="text/css" href="http://www.alpacajs.org/css/alpaca.min.css" rel="stylesheet"/>
</pre>
</div>
<div class="step">
<img src="examples/img/step-2.png"/>
<p>
Add a div to the body section of your html file and we are now ready to use Alpaca to render a feedback
form within the div. Let us first try to use only a schema parameter which is a JSON schema document
that describes the data structure of a "User Feedback" object with three properties, name, feedback and ranking.
</p>
<p>
As illustrated in
the following example, Alpaca is able to render a basic feedback form based on the provided schema.
It maps object property to form field based on it data type and format. For example, a string type
object property
will be mapped to a text field. However if we specify a list of allowed values for the
property with an enum attribute, Alpaca will instead render a
radio button group (or a drop-down select if the number of enum options is more than three) with all
values listed in the enum attribute. Since it is not a required field, Alpaca will automatically append
a None option to the radio button group which maps to empty value.
</p>
<p>
The feedback form we have at this point is very basic because the schema parameter only describes the data
structure and format. As for the options of the rendered form fields, Alpaca makes a lot of assumptions,
e.g. the size of text field is set as 40 as default.
</p>
<div class="container_12">
<div class='grid_6'>
<div id="form1" class="tutorial-example"></div>
</div>
<div class='grid_6'>
<pre class="prettyprint linenums">
<div id="form1">
</div>
<script type="text/javascript">
$(function() {
$("#form1").alpaca({
"schema": {
"title":"User Feedback",
"description":"What do you think about Alpaca?",
"type":"object",
"properties": {
"name": {
"type":"string",
"title":"Name"
},
"feedback": {
"type":"string",
"title":"Feedback"
},
"ranking": {
"type":"string",
"title":"Ranking",
"enum":['excellent','ok','so so']
}
}
}
});
});
</script>
</pre>
</div>
</div>
</div>
<div class="step">
<img src="examples/img/step-3.png"/>
<p>
To render a better feedback form, let us add an options parameter in addition to the schema parameter. The
options parameter is another JSON document that specifies type and options for the rendered form fields. It now
sets the size of name field as 20, maps the feedback object property to a textarea field and uses drop-down
select for the ranking object property with custom option labels.
</p>
<p>
The options parameter can also be used to provide additional field labels and help messages or overwrite the
ones that came from the schema parameter. In general, we can couple the same schema parameter with
different options parameters to render the form in various formats.
</p>
<div class="container_12">
<div class='grid_6'>
<div id="form2" class="tutorial-example"></div>
</div>
<div class='grid_6'>
<pre class="prettyprint linenums">
<div id="form1">
</div>
<script type="text/javascript">
$(function() {
$("#form1").alpaca({
"schema": {
"title":"User Feedback",
"description":"What do you think about Alpaca?",
"type":"object",
"properties": {
"name": {
"type":"string",
"title":"Name"
},
"feedback": {
"type":"string",
"title":"Feedback"
},
"ranking": {
"type":"string",
"title":"Ranking",
"enum":['excellent','ok','so so']
}
}
},
"options": {
"helper": "Tell us what you think about Alpaca!",
"fields": {
"name": {
"size": 20,
"helper": "Please enter your name.",
"placeholder": "Enter your name"
},
"feedback" : {
"type": "textarea",
"rows": 5,
"cols": 40,
"helper": "Please enter your feedback."
},
"ranking": {
"type": "select",
"helper": "Select your ranking.",
"optionLabels": ["Awesome!",
"It's Ok",
"Hmm..."]
}
}
}
});
});
</script>
</pre>
</div>
</div>
</div>
<div class="step">
<img src="examples/img/step-4.png"/>
<p>
The ultimate reason for using Alpaca forms service is to provide end users with a friendly interface for
collecting their inputs. Those collected information normally needs to be submitted to and stored in a backend
system such as database, CMS etc.
</p>
<p>
With the schema parameter and the optional options parameter, Alpaca has rendered a decent feedback form with
normal HTML form tags. To make it interact with backend services, we have a few options
</p>
<p>
1. Render the form inside a FORM tag instead of the div tag. The FORM tag can come with all required attributes
such as action, method etc. A submit button can also be added to the FORM tag. Clicking on the
submit button will result in form submission just like any other HTML form.
</p>
<p>
2. Or we can ask Alpaca to render the FORM tag for us by providing form related options in the options
parameter. As
shown in the following example, Alpaca now renders the FORM tag with the specified method and endpoint as well
as a SUBMIT button. Try to click the SUBMIT button and see what happens!
</p>
<p>
Alpaca sets the name attribute of a rendered field based on its schema property name in case we didn't
specify it
through the options parameter. For the following example, we will have "name" for the name field and
"your_feedback" for the feedback field.
</p>
<p>
We have also provide Alpaca with a new data parameter which is a JSON document used by Alpaca as the initial
data for
populating the form. We have also make both name and ranking field as required field. Now if you leave the name
field empty, Alpaca will automatically invalidate the field and display an error message.
</p>
<p>
You may also notice this rendered form has different styles than the previous example. That is because we set
Alpaca with a view parameter which makes Alpaca to pick a different set of templates for form rendition.
</p>
<div class="container_12">
<div class='grid_6'>
<div id="form3" class="tutorial-example"></div>
</div>
<div class='grid_6'>
<pre class="prettyprint linenums">
<div id="form1">
</div>
<script type="text/javascript">
$(function() {
$("#form1").alpaca({
"data": {
"name": "Britney Spears",
"feedback": "Alpaca is very cute.",
"ranking": "excellent"
},
"schema": {
"title":"User Feedback",
"description":"What do you think about Alpaca?",
"type":"object",
"properties": {
"name": {
"type":"string",
"title":"Name",
"required":true
},
"feedback": {
"type":"string",
"title":"Feedback"
},
"ranking": {
"type":"string",
"title":"Ranking",
"enum":['excellent','ok','so so'],
"required":true
}
}
},
"options": {
"renderForm":true,
"form":{
"attributes":{
"action":"examples/endpoints/save.php",
"method":"post"
},
"buttons":{
"submit":{}
}
},
"helper": "Tell us what you think about Alpaca!",
"fields": {
"name": {
"size": 20,
"helper": "Please enter your name."
},
"feedback" : {
"type": "textarea",
"name": "your_feedback",
"rows": 5,
"cols": 40,
"helper": "Please enter your feedback."
},
"ranking": {
"type": "select",
"helper": "Select your ranking.",
"optionLabels": ["Awesome!",
"It's Ok",
"Hmm..."]
}
}
},
"view" : "VIEW_WEB_EDIT_LIST"
});
});
</script>
</pre>
</div>
</div>
</div>
<div class="step">
<img src="examples/img/step-5.png"/>
<p>
Alpaca is capable of not only rendering a form based on JSON schema but also reassembling form inputs into a
JSON document according to the data structure described by the same JSON schema.
</p>
<p>
This brings us a better option for interacting Alpaca with backend services. As shown in the following example,
by introducing a "postRender"
callback function which gets invoked once the form rendition is finished, we are able to get hold of the
reassembled JSON document, validate it and post it to a backend service.
</p>
<p>
For developers, this will be the place to do any required post-processing since Alpaca will feed the callback
function with the top-most form control that it constructs. We can easily gain access to any other Alpaca form
control by using API such as <i>renderedForm.children</i>, <i>renderedForm.childrenByPropertyId</i>, <i>renderForm.getControlByPath</i>
etc. From any Alpaca form control, we can get the DOM element of its mapped form field by making a simple API
call, <i>myControl.field</i>.
</p>
<div class="container_12">
<div class='grid_6'>
<div id="form4" class="tutorial-example"></div>
<div class="tutorial-example"><input type="button" id="form4-button" value="Save"/></div>
</div>
<div class='grid_6'>
<pre class="prettyprint linenums">
<div id="form1"></div>
<div>
<input type="button" id="form1-button" value="Save">
</div>
<script type="text/javascript">
$(function() {
$("#form1").alpaca({
"data": {
"name": "Britney Spears",
"feedback": "Alpaca is very cute.",
"ranking": "excellent"
},
"schema": {
"title":"User Feedback",
"description":"What do you think about Alpaca?",
"type":"object",
"properties": {
"name": {
"type":"string",
"title":"Name",
"required":true
},
"feedback": {
"type":"string",
"title":"Feedback"
},
"ranking": {
"type":"string",
"title":"Ranking",
"enum":['excellent','ok','so so'],
"required":true
}
}
},
"options": {
"helper": "Tell us what you think about Alpaca!",
"fields": {
"name": {
"size": 20,
"helper": "Please enter your name."
},
"feedback" : {
"type": "textarea",
"name": "your_feedback",
"rows": 5,
"cols": 40,
"helper": "Please enter your feedback."
},
"ranking": {
"type": "select",
"helper": "Select your ranking.",
"optionLabels": ["Awesome!",
"It's Ok",
"Hmm..."]
}
}
},
"view" : "VIEW_WEB_EDIT_LIST",
"postRender": function(renderedForm) {
$('#form4-button').click(function() {
if (renderedForm.isValid(true)) {
var val = renderedForm.getValue();
$.ajax({
type: "POST",
url: "examples/endpoints/save2.php",
data: {json:JSON.stringify(val)},
success: function(msg) {
alert("Request received: " + msg);
}
});
}
});
}
});
});
</script>
</pre>
</div>
</div>
</div>
<div class="step">
<img src="examples/img/step-6.png"/>
<p>
Alpaca is a client-side forms engine which means form rendition and data processing all happen on client
side <i>e.g.</i> client browser. This provides a great foundation for high performance and scalability since
JSON is the native data format that all browsers understand and it is the easiest format for data manipulation.
Once the jQuery and Alpaca library are downloaded and cached on client side, there will be no other overhead.
</p>
<p>
Plugging Alpaca into your existing application should be fairly straightforward. All you need to do is to
provide an endpoint for serving Alpaca with data in JSON and another endpoint for taking re-assembled JSON
document with user inputs from Alpaca for data persistence. Most modern day backend systems may already
have those two endpoints.
</p>
<p>
As illustrated by the above examples, using Alpaca is mainly through configuration rather than coding. Since
schema and options parameters are also JSON documents, they can be stored in and served by your back
systems as well. If you can plugin an adapter that converts XML document to JSON document, you can use Alpaca for your
XML data as well.
</p>
<p>
If you are interested in learning more about Alpaca or planning to use Alpaca for your project, please check out
Alpaca <a href="resources.html" title="Alpaca Resources">resources</a> page
for download links, documentation, samples <i>etc.</i>
</p>
</div>
</div>
<div class="clear clearfix"></div>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="container_12">
<div class="grid_12 copyright">
Copyright © 2012 Gitana Software | All Rights Reserved
</div>
</div>
</footer>
<script>
$('#form1').alpaca({
"schema" : {
"title" : "User Feedback",
"description" : "What do you think about Alpaca?",
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"title" : "Name"
},
"feedback" : {
"type" : "string",
"title" : "Feedback"
},
"ranking" : {
"type" : "string",
"title" : "Ranking",
"enum" : ['excellent','ok','so so']
}
}
}
});
$('#form2').alpaca({
"schema" : {
"title" : "User Feedback",
"description" : "What do you think about Alpaca?",
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"title" : "Name"
},
"feedback" : {
"type" : "string",
"title" : "Feedback"
},
"ranking" : {
"type" : "string",
"title" : "Ranking",
"enum" : ['excellent','ok','so so']
}
}
},
"options" : {
"helper" : "Tell us what you think about Alpaca!",
"fields" : {
"name" : {
"size" : 20,
"helper" : "Please enter your name.",
"placeholder": "Enter your name"
},
"feedback" : {
"type" : "textarea",
"rows" : 5,
"cols" : 40,
"helper" : "Please enter your feedback."
},
"ranking" : {
"type" : "select",
"helper" : "Select your ranking.",
"optionLabels" : ["Awesome!","It's Ok","Hmm..."]
}
}
}
});
$('#form3').alpaca({
"data" : {
"name" : "Britney Spears",
"feedback" : "Alpaca is very cute.",
"ranking" : "excellent"
},
"schema" : {
"title" : "User Feedback",
"description" : "What do you think about Alpaca?",
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"title" : "Name",
"required" : true
},
"feedback" : {
"type" : "string",
"title" : "Feedback"
},
"ranking" : {
"type" : "string",
"title" : "Ranking",
"enum" : ['excellent','ok','so so'],
"required" : true
}
}
},
"options" : {
"renderForm":true,
"form":{
"attributes":{
"action":"examples/endpoints/save.php",
"method":"post"
},
"buttons":{
"submit":{}
}
},
"fields" : {
"helper" : "Tell us what you think about Alpaca!",
"name" : {
"size" : 20,
"helper" : "Please enter your name."
},
"feedback" : {
"type" : "textarea",
"name" : "your_feedback",
"rows" : 5,
"cols" : 30,
"helper" : "Please enter your feedback."
},
"ranking" : {
"type" : "select",
"helper" : "Select your ranking.",
"optionLabels" : ["Awesome!","It's Ok","Hmm..."]
}
}
},
"view" : "VIEW_WEB_EDIT_LIST"
});
$('#form4').alpaca({
"data" : {
"name" : "Britney Spears",
"feedback" : "Alpaca is very cute.",
"ranking" : "excellent"
},
"schema" : {
"title" : "User Feedback",
"description" : "What do you think about Alpaca?",
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"title" : "Name",
"required" : true
},
"feedback" : {
"type" : "string",
"title" : "Feedback"
},
"ranking" : {
"type" : "string",
"title" : "Ranking",
"enum" : ['excellent','ok','so so'],
"required" : true
}
}
},
"options" : {
"fields" : {
"helper" : "Tell us what you think about Alpaca!",
"name" : {
"size" : 20,
"helper" : "Please enter your name."
},
"feedback" : {
"type" : "textarea",
"name" : "your_feedback",
"rows" : 5,
"cols" : 30,
"helper" : "Please enter your feedback."
},
"ranking" : {
"type" : "select",
"helper" : "Select your ranking.",
"optionLabels" : ["Awesome!","It's Ok","Hmm..."]
}
}
},
"view" : "VIEW_WEB_EDIT_LIST",
"postRender": function(renderedForm) {
$('#form4-button').click(function() {
if (renderedForm.isValid(true)) {
var val = renderedForm.getValue();
$.ajax({
type: "POST",
url: "examples/endpoints/save2.php",
data: {json:JSON.stringify(val)},
success: function(msg) {
alert("Request received: " + msg);
}
});
}
});
}
});
</script>
<script>
window.prettyPrint && prettyPrint();
</script>
</body>
</html>