source: Dev/trunk/src/node_modules/tv4/tv4.async-jquery.js @ 513

Last change on this file since 513 was 487, checked in by hendrikvanantwerpen, 11 years ago

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File size: 1.4 KB
Line 
1// Provides support for asynchronous validation (fetching schemas) using jQuery
2// Callback is optional third argument to tv4.validate() - if not present, synchronous operation
3//     callback(result, error);
4if (typeof (tv4.asyncValidate) === 'undefined') {
5        tv4.syncValidate = tv4.validate;
6        tv4.validate = function (data, schema, callback, checkRecursive, banUnknownProperties) {
7                if (typeof (callback) === 'undefined') {
8                        return this.syncValidate(data, schema, checkRecursive, banUnknownProperties);
9                } else {
10                        return this.asyncValidate(data, schema, callback, checkRecursive, banUnknownProperties);
11                }
12        };
13        tv4.asyncValidate = function (data, schema, callback, checkRecursive, banUnknownProperties) {
14                var $ = jQuery;
15                var result = tv4.validate(data, schema, checkRecursive, banUnknownProperties);
16                if (!tv4.missing.length) {
17                        callback(result, tv4.error);
18                } else {
19                        // Make a request for each missing schema
20                        var missingSchemas = $.map(tv4.missing, function (schemaUri) {
21                                return $.getJSON(schemaUri).success(function (fetchedSchema) {
22                                        tv4.addSchema(schemaUri, fetchedSchema);
23                                }).error(function () {
24                                        // If there's an error, just use an empty schema
25                                        tv4.addSchema(schemaUri, {});
26                                });
27                        });
28                        // When all requests done, try again
29                        $.when.apply($, missingSchemas).done(function () {
30                                var result = tv4.asyncValidate(data, schema, callback, checkRecursive, banUnknownProperties);
31                        });
32                }
33        };
34}
Note: See TracBrowser for help on using the repository browser.