From aed0889b3d9fc7b86cae172f9ca6e2f5007bf810 Mon Sep 17 00:00:00 2001 From: Michael Uzquiano Date: Wed, 7 Aug 2013 21:22:27 -0400 Subject: [PATCH] Fix to loader Added serialization example Updates to tests --- .../references.html | 0 .../jsonschema/serialization-data.json | 58 ++++++++ .../jsonschema/serialization-schema.json | 68 +++++++++ .../components/jsonschema/serialization.html | 139 ++++++++++++++++++ examples/js/example.js | 8 +- js/Alpaca.js | 39 ++--- tests/js/forms/CreateForm.js | 8 +- tests/js/forms/EditForm.js | 12 +- tests/js/wizards/Wizard.js | 12 +- 9 files changed, 310 insertions(+), 34 deletions(-) rename examples/components/{references => jsonschema}/references.html (100%) create mode 100644 examples/components/jsonschema/serialization-data.json create mode 100644 examples/components/jsonschema/serialization-schema.json create mode 100644 examples/components/jsonschema/serialization.html diff --git a/examples/components/references/references.html b/examples/components/jsonschema/references.html similarity index 100% rename from examples/components/references/references.html rename to examples/components/jsonschema/references.html diff --git a/examples/components/jsonschema/serialization-data.json b/examples/components/jsonschema/serialization-data.json new file mode 100644 index 000000000..dd7963fd5 --- /dev/null +++ b/examples/components/jsonschema/serialization-data.json @@ -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." + }] + }] +} \ No newline at end of file diff --git a/examples/components/jsonschema/serialization-schema.json b/examples/components/jsonschema/serialization-schema.json new file mode 100644 index 000000000..ca377778e --- /dev/null +++ b/examples/components/jsonschema/serialization-schema.json @@ -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" + } + } + } + } +} diff --git a/examples/components/jsonschema/serialization.html b/examples/components/jsonschema/serialization.html new file mode 100644 index 000000000..5cbaeaa3f --- /dev/null +++ b/examples/components/jsonschema/serialization.html @@ -0,0 +1,139 @@ + + + + + Alpaca - HTML5 Forms for jQuery - References and Serialization + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+

Easy Forms for jQuery

+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+ +
+ +

Using References with Serialization

+

+ 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. +

+ + +
+

Example #1: Serialized composite object with references

+

+ 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. +

+ + +
+ + + +
+ +
+ + +
+ + +
+
+
+
+
+
+
+ + + diff --git a/examples/js/example.js b/examples/js/example.js index eac26951e..2e3f152b5 100644 --- a/examples/js/example.js +++ b/examples/js/example.js @@ -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" } + ] }]; diff --git a/js/Alpaca.js b/js/Alpaca.js index 65c886363..8a52fa614 100644 --- a/js/Alpaca.js +++ b/js/Alpaca.js @@ -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)) { @@ -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); @@ -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); } diff --git a/tests/js/forms/CreateForm.js b/tests/js/forms/CreateForm.js index ea28e0a51..05049cccf 100644 --- a/tests/js/forms/CreateForm.js +++ b/tests/js/forms/CreateForm.js @@ -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); @@ -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); diff --git a/tests/js/forms/EditForm.js b/tests/js/forms/EditForm.js index 3a30b991a..dc176a1c8 100644 --- a/tests/js/forms/EditForm.js +++ b/tests/js/forms/EditForm.js @@ -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 @@ -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 diff --git a/tests/js/wizards/Wizard.js b/tests/js/wizards/Wizard.js index 6ee450fb8..40791e601 100644 --- a/tests/js/wizards/Wizard.js +++ b/tests/js/wizards/Wizard.js @@ -6,9 +6,9 @@ test("Configuration-based wizard without using a layout template.", function() { stop(); $("#wizard-1").alpaca({ - "data": "../examples/components/wizards/customer-profile-data.json", - "options": "../examples/components/wizards/customer-profile-options.json", - "schema": "../examples/components/wizards/customer-profile-schema.json", + "dataSource": "../examples/components/wizards/customer-profile-data.json", + "optionsSource": "../examples/components/wizards/customer-profile-options.json", + "schemaSource": "../examples/components/wizards/customer-profile-schema.json", "view": { "parent": "VIEW_WEB_EDIT_LIST", "wizard": { @@ -105,9 +105,9 @@ test("Template-based wizard.", function() { stop(); $("#wizard-2").alpaca({ - "data": "../examples/components/wizards/customer-profile-data.json", - "options": "../examples/components/wizards/customer-profile-options.json", - "schema": "../examples/components/wizards/customer-profile-schema.json", + "dataSource": "../examples/components/wizards/customer-profile-data.json", + "optionsSource": "../examples/components/wizards/customer-profile-options.json", + "schemaSource": "../examples/components/wizards/customer-profile-schema.json", "view": { "parent": "VIEW_JQUERYUI_EDIT_LIST", "layout": {