garycourt / JSV

A JavaScript implementation of an extendable, fully compliant JSON Schema validator.
618 stars 84 forks source link

Support AMD for lazy loading #50

Open nickcmaynard opened 12 years ago

nickcmaynard commented 12 years ago

With Dojo etc. moving to AMD for lazy module loading, it would be great if JSV could support the AMD loader, rather than/as well as the CommonJS require() behaviour currently in place.

For minimal sites, Almond, a really small AMD shim, is only 2.3KB compiled.

There are instructions on converting "traditional" CommonJS loading behaviour to AMD here: http://requirejs.org/docs/commonjs.html

This means that those using the AMD loader method would be able to do the following, thus only loading JSV when required: 1) Register the module path for JSV 2) require(["jsv/json-schema-draft-03"], function(env) { // JSV code goes here });

aoboturovorange commented 12 years ago

All require calls use path-related addressing like "./" which makes them path dependent on installation inside project and hence requires configuration tweaks.

this.JSV = JSV;  //set global object
exports.JSV = JSV;  //export to CommonJS

There are globals.

There's a circular reference: jsv -> environments -> json-schema-draft-03 -> jsv.

@garycourt will have to do some refactoring to support AMD. Hope he'll find some time for it in near future.

neonstalwart commented 12 years ago

@aoboturovorange circular references are handled/allowed in AMD

rsolomon commented 12 years ago

This fork of GaryCourt's JSV manually converts all relevant commonJS modules to requireJS modules. Everything should continue to work like normal, except that schema definitions must now be explicitly imported (no default is set).

https://github.com/rsolomon/JSV

jeffrose commented 10 years ago

FYI, the UMD project provides patterns for supporting multiple module definitions.