jdorn / json-editor

JSON Schema Based Editor
MIT License
5.8k stars 1.08k forks source link

Support AMD/RequireJS #405

Open mbrodala opened 9 years ago

mbrodala commented 9 years ago

It would be great if the JSON editor supports AMD/RequireJS.

Since Grunt is apparently already used to build dist scripts, grunt-umd should do the trick. See twitter/typeahead.js for an example.

svdoever commented 9 years ago

+1, using it from GitHub Electron shell, but have to include the js file in my index.html

mbrodala commented 9 years ago

Current workaround is to use shim and define exports to make it loadable via RequireJS but this provides no encapsulation at all:

requirejs.config({
  shim: {
    "path/to/json-editor": {
      exports: "JSONEditor"
    }
  }
});

Then use it as usual:

define(["path/to/json-editor"], function(JSONEditor) {});
jakefeasel commented 9 years ago

This doesn't seem to work with Handlebars. The latest version of Handlebars (as of this time, 3.0.3) supports AMD loading and so does not bind window.Handlebars (as is expected in https://github.com/jdorn/json-editor/blob/master/src/templates/handlebars.js#L2 )

The only workaround I've managed to get working at the moment is this:

    jsonEditor: {
        deps: ["handlebars"],
        init: function (Handlebars) {
            window.Handlebars = Handlebars;
            return this.JSONEditor;
        },
        exports: "JSONEditor"
    },

Which is pretty terrible since it breaks the whole module encapsulation pattern.

mbrodala commented 9 years ago

The actual issue here is that there are optional dependencies which doesn't work with the AMD pattern. Normally one should split all the different templating engines to separate AMD modules with proper dependencies.

Logodaedalus commented 8 years ago

+1, Just a chime in to say that this would be an amazing thing to add in!