Skip to content

Commit

Permalink
Fix to loader
Browse files Browse the repository at this point in the history
Added serialization example
Updates to tests
  • Loading branch information
uzquiano committed Aug 8, 2013
1 parent 942632a commit aed0889
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 34 deletions.
58 changes: 58 additions & 0 deletions examples/components/jsonschema/serialization-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"title": "The Griswold Family Movie Collection",
"movies": [{
"title": "Tron",
"year": "1982",
"actors": [{
"name": "Jeff Bridges"
}, {
"name": "Bruce Boxleitner"
}]
}, {
"title": "Breakin' 2: Electric Boogaloo",
"year": "1984",
"actors": [{
"name": "Lucinda Dickey"
}, {
"name": "Adolfo Quinones"
}, {
"name": "Michael Chambers"
}],
"reviews": [{
"author": "Ozone",
"review": "Nothing's changed here. Story's still the same. People are still break dancing! Gotta love the 80's."
}]
}, {
"title": "Groundhog Day",
"year": "1993",
"actors": [{
"name": "Bill Murray"
}, {
"name": "Andie MacDowell"
}],
"reviews": [{
"author": "Cassius Clay",
"review": "This is an intriguing comedy about repeating the past."
}, {
"author": "Iron Mike",
"review": "This is one of the best and most clever comedies I've ever seen!"
}]
}, {
"title": "The Big Lebowski",
"year": "1999",
"actors": [{
"name": "Jeff Bridges"
}, {
"name": "John Goodman"
}, {
"name": "Steve Buscemi"
}],
"reviews": [{
"author": "Donnie",
"review": "What movie was that again?"
}, {
"author": "Walter",
"review": "Shut up Donnie."
}]
}]
}
68 changes: 68 additions & 0 deletions examples/components/jsonschema/serialization-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"type": "object",
"properties": {
"title": {
"type": "string",
"title": "Name of Movie Collection",
"required": true
},
"movies": {
"type": "array",
"title": "Movies",
"items": {
"$ref": "#/definitions/movie"
}
}
},
"definitions": {
"movie": {
"type": "object",
"properties": {
"title": {
"type": "string",
"title": "Movie Name"
},
"year": {
"type": "string",
"title": "Year"
},
"actors": {
"type": "array",
"title": "Movie Actors",
"items": {
"$ref": "#/definitions/actor"
}
},
"reviews": {
"type": "array",
"title": "Movie Reviews",
"items": {
"$ref": "#/definitions/review"
}
}
}
},
"actor": {
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name"
}
}
},
"review": {
"type": "object",
"properties": {
"author": {
"type": "string",
"title": "Author"
},
"review": {
"type": "string",
"title": "Review"
}
}
}
}
}
139 changes: 139 additions & 0 deletions examples/components/jsonschema/serialization.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Alpaca - HTML5 Forms for jQuery - References and Serialization</title>
<meta name="keywords"
content="jquery,bootstrap,forms,form,jquerymobile,jqueryui,mobile,json,json-schema,cloudcms,gitana,cms,content,uzquiano"/>
<meta name="description" content="Customer Profile Form with Custom View Template."/>
<meta name="author" content="Gitana Software Inc."/>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" type="image/x-icon" href="../../../favicon.ico" />

<!-- jQuery -->
<script type="text/javascript" src="../../../lib/jquery-latest.min.js"></script>
<script type="text/javascript" src="../../../lib/jquery.tmpl.js"></script>

<!-- Alpaca -->
<script type="text/javascript" src="../../../components/alpaca/alpaca.js"></script>
<link type="text/css" href="../../../components/alpaca/alpaca.css" rel="stylesheet"/>
<link type="text/css" href="../../../components/alpaca/alpaca-jqueryui.css" rel="stylesheet"/>

<!-- jQuery UI Support -->
<script type="text/javascript" src="../../../lib/jquery-ui-latest/jquery-ui-latest.custom.min.js"></script>
<link type="text/css" href="../../../lib/jquery-ui-latest/jquery-ui-latest.custom.css" rel="stylesheet"/>

<!-- Additional CSS -->
<link type="text/css" href="../../css/960.fluid.css" rel="stylesheet"/>

<!-- Required for the Editor field (sample code viewing) -->
<script src="../../../lib/ace/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>

<!-- Web Site -->
<script type="text/javascript" src="../../lib/google-code-prettify/prettify.min.js"></script>
<script type="text/javascript" src="../../js/themeswitchertool.js"></script>
<script type="text/javascript" src="../../js/example.js"></script>
<script type="text/javascript" src="../../js/footer.js"></script>
<link type="text/css" href="../../css/site.css" rel="stylesheet"/>
<link type="text/css" href="../../lib/google-code-prettify/prettify.css" rel="stylesheet"/>
<script type="text/javascript" src="../../lib/tweet/jquery.tweet.js"></script>
<link type="text/css" href="../../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="../../img/alpaca.png"><span>Easy Forms for jQuery</span></h1>
<div class="resource-bar">
<a href="../../../index.html">Home</a>
<a href="../../../web/download.html">Downloads</a>
<a href="javascript:void(0);" class="current">Documentation</a>
<a href="../../forms/customer-profile/edit-form.html">Forms</a>
<a href="../../../web/tutorials.html">Tutorials</a>
<a href="../../../web/consulting.html">Consulting</a>
</div>
</div>
</div>
</div>
</header>
<div class='container_12'>
<div class="actual-body">
<div class="grid_3">
<div class="container_12 side-bar">
</div>
</div>

<div class="grid_9">
<div class="container_12">
<div class="widget">
<div class="widget-body">

<div class='grid_12 alpaca-example-header' id='serialization' alpaca-types=''>

<h1>Using References with Serialization</h1>
<p>
Serialization is the process of taking the data contained in a form and converting
it to JSON. Alpaca serializes references for you automatically, taking into account
nested structures and types.
</p>

<!-- Example #1: Serialized composite object (with references) -->
<div class='grid_12 alpaca-example-case'>
<h2>Example #1: Serialized composite object with references</h2>
<p>
This example provides a complex object with nested references. It is loaded with
data. When you click the "serialize" button, the JSON is produced and displayed.
</p>

<button id="serialize">Serialize</button>
<div id="field1"></div>

<script type="text/javascript" id="field1-script">
$(function() {
$("#field1").alpaca({
"schemaSource": "./serialization-schema.json",
"dataSource": "./serialization-data.json",
"postRender": function(form) {

$("#serialize").click(function() {

var json = form.getValue();

alert(JSON.stringify(json));
});

}
});
});
</script>

</div>

</div>

<!-- BEGIN DISCUSSIONS -->
<div class="alpaca-discussions"></div>
<!-- END DISCUSSIONS -->

<div class="clear height-fix"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="container_12">
<div class="grid_12 copyright">
Copyright &copy; 2013 Gitana Software, Inc. | All Rights Reserved
</div>
</div>
</footer>
</body>
</html>
8 changes: 7 additions & 1 deletion examples/js/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,14 @@ $(function() {
{
"id":"references",
"title":"Using References",
"link":"../../components/references/references.html"
"link":"../../components/jsonschema/references.html"
},
{
"id":"serialization",
"title":"Serialization",
"link":"../../components/jsonschema/serialization.html"
}

]
}];

Expand Down
39 changes: 22 additions & 17 deletions js/Alpaca.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,6 @@
connector = new connectorClass("default");
}

// handle case for null data
// if schema exits, we will use the settings from the schema
// we assume a text field
if (Alpaca.isEmpty(data)) {
if (Alpaca.isEmpty(schema) && (Alpaca.isEmpty(options) || Alpaca.isEmpty(options.type))) {
if (Alpaca.isEmpty(options)) {
data = "";
options = "text";
} else if (options && Alpaca.isObject(options)) {
data = "";
options.type = "text";
}
}
}

// container can either be a dom id or a dom element
if (el) {
if (Alpaca.isString(el)) {
Expand Down Expand Up @@ -250,6 +235,26 @@
loadedOptions = loadedOptions ? loadedOptions : options;
loadedView = loadedView ? loadedView : view;

// some defaults for the case where data is null
// if schema + options are not provided, we assume a text field

if (Alpaca.isEmpty(loadedData))
{
if (Alpaca.isEmpty(loadedSchema) && (Alpaca.isEmpty(loadedOptions) || Alpaca.isEmpty(loadedOptions.type)))
{
loadedData = "";

if (Alpaca.isEmpty(loadedOptions))
{
loadedOptions = "text";
}
else if (options && Alpaca.isObject(options))
{
loadedOptions.type = "text";
}
}
}

// init alpaca
return Alpaca.init(el, loadedData, loadedOptions, loadedSchema, loadedView, initialSettings, callback, _renderedCallback, connector, errorCallback, isDynamicCreation);

Expand Down Expand Up @@ -2211,10 +2216,10 @@

if (Alpaca.logLevel <= level)
{
var method = methodMap[level];

if (typeof console !== 'undefined' && console[method])
{
var method = methodMap[level];

if ("debug" == method) {
console.debug(obj);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/js/forms/CreateForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
test("Form for creating new content.", function() {
stop();
$("#createform-1").alpaca({
"options": "../examples/forms/customer-profile/options.json",
"schema": "../examples/forms/customer-profile/schema.json",
"optionsSource": "../examples/forms/customer-profile/options.json",
"schemaSource": "../examples/forms/customer-profile/schema.json",
"view": "VIEW_WEB_CREATE",
"postRender": function (renderedField) {
expect(13);
Expand Down Expand Up @@ -38,8 +38,8 @@
test("Simple form for creating new content.", function() {
stop();
$("#createform-2").alpaca({
"options": "../examples/forms/customer-profile/simple-options.json",
"schema": "../examples/forms/customer-profile/schema.json",
"optionsSource": "../examples/forms/customer-profile/simple-options.json",
"schemaSource": "../examples/forms/customer-profile/schema.json",
"view": "VIEW_WEB_CREATE",
"postRender": function (renderedField) {
expect(1);
Expand Down
12 changes: 6 additions & 6 deletions tests/js/forms/EditForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
test("Edit form with readonly fields.", function() {
stop();
$("#editform-1").alpaca({
"data": "../examples/forms/customer-profile/data.json",
"options": "../examples/forms/customer-profile/simple-options.json",
"schema": "../examples/forms/customer-profile/schema.json",
"dataSource": "../examples/forms/customer-profile/data.json",
"optionsSource": "../examples/forms/customer-profile/simple-options.json",
"schemaSource": "../examples/forms/customer-profile/schema.json",
"view": {
"parent": "VIEW_WEB_EDIT",
"displayReadonly": true
Expand All @@ -29,9 +29,9 @@
test("Simple form for editing content.", function() {
stop();
$("#editform-2").alpaca({
"data": "../examples/forms/customer-profile/data.json",
"options": "../examples/forms/customer-profile/simple-options.json",
"schema": "../examples/forms/customer-profile/schema.json",
"dataSource": "../examples/forms/customer-profile/data.json",
"optionsSource": "../examples/forms/customer-profile/simple-options.json",
"schemaSource": "../examples/forms/customer-profile/schema.json",
"view": {
"parent": "VIEW_JQUERYUI_EDIT",
"displayReadonly": false
Expand Down
Loading

0 comments on commit aed0889

Please sign in to comment.