Skip to content

Commit f60b846

Browse files
committed
Simplify framework module loading, while still keeping frameworks separate.
1 parent f53de82 commit f60b846

23 files changed

+3545
-1434
lines changed

README.md

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ A [JSON Schema](http://json-schema.org) Form builder for Angular 4 and 5, simila
99
* [Mozilla](https://blog.mozilla.org/services/)'s [React JSON Schema Form](https://github.com/mozilla-services/react-jsonschema-form) for [React](https://facebook.github.io/react/) ([examples](https://mozilla-services.github.io/react-jsonschema-form/)), and
1010
* [Joshfire](http://www.joshfire.com)'s [JSON Form](http://github.com/joshfire/jsonform/wiki) for [jQuery](https://jquery.com) ([examples](http://ulion.github.io/jsonform/playground/))
1111

12-
Note: This is currently a personal proof-of-concept project, and is NOT affiliated with any of the organizations listed above. (Though they are all awesome, and totally worth checking out.)
12+
Note: This is currently a personal proof-of-concept project, and is not affiliated with any of the organizations listed above. (Though they are all awesome, and totally worth checking out.)
13+
14+
## Breaking change in version 0.7.0-alpha.1 and above
15+
16+
You must now import both JsonSchemaFormModule and a framework module. (Don't worry, it's easy.)
17+
For full details, see 'To install from NPM and use in your own project', below.
1318

1419
## Check out the live demo and play with the examples
1520

@@ -54,22 +59,40 @@ npm install angular2-json-schema-form
5459
Then import JsonSchemaFormModule in your main application module, like this:
5560

5661
```javascript
57-
import { BrowserModule } from '@angular/platform-browser';
58-
import { NgModule } from '@angular/core';
62+
import { BrowserModule } from '@angular/platform-browser';
63+
import { NgModule } from '@angular/core';
5964

60-
import { JsonSchemaFormModule } from 'angular2-json-schema-form';
65+
import {
66+
JsonSchemaFormModule, MaterialDesignFrameworkModule
67+
} from 'angular2-json-schema-form';
6168

62-
import { AppComponent } from './app.component';
69+
import { AppComponent } from './app.component';
6370

6471
@NgModule({
6572
declarations: [ AppComponent ],
66-
imports: [ BrowserModule, JsonSchemaFormModule ],
67-
providers: [],
68-
bootstrap: [ AppComponent ]
73+
imports: [
74+
BrowserModule, MaterialDesignFrameworkModule,
75+
JsonSchemaFormModule.forRoot(MaterialDesignFrameworkModule)
76+
],
77+
providers: [],
78+
bootstrap: [ AppComponent ]
6979
})
7080
export class AppModule { }
7181
```
7282

83+
Note that you have to import both the main JsonSchemaFormModule and a separate framework module (in this example, MaterialDesignFrameworkModule).
84+
85+
The framework module is listed in your imports section twice, once by itself (to load the framework's components) and again in the JsonSchemaFormModule.forRoot() function (to load the framework's service and tell Angular JSON Schema Form to use it).
86+
87+
Four framework modules are currently included:
88+
89+
* MaterialDesignFrameworkModule — Material Design
90+
* Bootstrap3FrameworkModule — Bootstrap 3
91+
* Bootstrap4FrameworkModule — Bootstrap 4
92+
* NoFrameworkModule — plain HTML (for testing)
93+
94+
It is also possible to load multiple frameworks and switch between them at runtime, like the example playground on GitHub. But most typical sites will just load one framework.
95+
7396
#### Seed Application Examples
7497

7598
For complete examples of how to install and configure Angular JSON Schema Form to work with each included display framework, check out the following seed applications:
@@ -80,9 +103,9 @@ For complete examples of how to install and configure Angular JSON Schema Form t
80103

81104
#### Additional notes for Angular CLI
82105

83-
Make sure you are running the latest version of Angular CLI.
106+
Make sure you are running a recent version of Angular CLI.
84107

85-
Older versions of Angular CLI (e.g. 1.0.1) may fail with the error `Critical dependency: the request of a dependency is an expression` while trying to compile ajv (Another JSON Schema Validator). But this error has been fixed in newer versions. So if you receive that error, upgrade your Angular CLI to the latest version.
108+
Very old versions of Angular CLI (e.g. 1.0.1) may fail with the error `Critical dependency: the request of a dependency is an expression` while trying to compile ajv (Another JSON Schema Validator). But this error has been fixed in newer versions. So if you receive that error, upgrade your Angular CLI.
86109

87110
#### Additional notes for SystemJS
88111

@@ -95,6 +118,8 @@ Add these lines to the 'map' section of systemjs.config.js:
95118
'lodash': 'npm:lodash/lodash.min.js'
96119
```
97120

121+
(Note: These instructions have not been tested recently. If you use SystemJS and have problems, please post a bug report on GitHub.)
122+
98123
## Using Angular JSON Schema Form
99124

100125
### Basic use
@@ -103,38 +128,22 @@ For basic use, after loading JsonSchemaFormModule as described above, to display
103128

104129
```html
105130
<json-schema-form
106-
framework="bootstrap-3"
107131
loadExternalAssets="true"
108132
[schema]="yourJsonSchema"
109133
(onSubmit)="yourOnSubmitFn($event)">
110134
</json-schema-form>
111135
```
112136

113-
Where the `framework` is the name of the display framework to use (`bootstrap-3`, `bootstrap-4`, `material-design`, or `no-framework`), `schema` is a valid JSON schema object, and `onSubmit` calls a function to process the submitted JSON form data. If you don't already have your own schemas, you can find a bunch of samples to test with in the `src/demo/assets/example-schemas` folder, as described above.
114-
115-
Setting `loadExternalAssets="true"` will automatically load any additional assets needed by the display framework. It is useful when you are trying out this library, but should be removed in production sites, which should instead load all required assets separately. For full details see 'Changing or adding frameworks', below.
116-
117-
Note: If you prefer brackets around all your attributes, the following is functionally equivalent:
118-
119-
```html
120-
<json-schema-form
121-
[framework]="'bootstrap-3'"
122-
[loadExternalAssets]="true"
123-
[schema]="yourJsonSchema"
124-
(onSubmit)="yourOnSubmitFn($event)">
125-
</json-schema-form>
126-
```
127-
128-
If you use this syntax, make sure to include the nested quotes (`"'` and `'"`) around the framework name.
137+
Where `schema` is a valid JSON schema object, and `onSubmit` calls a function to process the submitted JSON form data. If you don't already have your own schemas, you can find a bunch of samples to test with in the `src/demo/assets/example-schemas` folder, as described above.
129138

139+
Setting `loadExternalAssets="true"` will automatically load any additional assets needed by the display framework. It is useful when you are trying out this library, but production sites should instead load all required assets separately. For full details see 'Changing or adding frameworks', below.
130140

131141
### Data-only mode
132142

133143
Angular JSON Schema Form can also create a form entirely from a JSON object—with no schema—like so:
134144

135145
```html
136146
<json-schema-form
137-
framework="bootstrap-4"
138147
loadExternalAssets="true"
139148
[(ngModel)]="exampleJsonObject">
140149
</json-schema-form>
@@ -166,13 +175,14 @@ Also, notice that the 'ngModel' input supports Angular's 2-way data binding, jus
166175

167176
For more control over your form, you may provide these additional inputs:
168177

169-
* `layout` - array with a custom form layout (see Angular Schema Form's [form definitions](https://github.com/json-schema-form/angular-schema-form/blob/master/docs/index.md#form-definitions) for information about how to construct a form layout)
170-
* `data` - object to populate the form with default or previously submitted values
171-
* `options` - object to set any global options for the form
172-
* `widgets` to add custom widgets
173-
* `framework` - string or object to set which framework to use
174-
set to 'bootstrap-3', 'bootstrap-4', 'material-design', or
175-
'no-framework' (the default), or pass in your own custom framework object
178+
* `layout` array with a custom form layout (see Angular Schema Form's [form definitions](https://github.com/json-schema-form/angular-schema-form/blob/master/docs/index.md#form-definitions) for information about how to construct a form layout)
179+
* `data` object to populate the form with default or previously submitted values
180+
* `options` object to set any global options for the form
181+
* `widgets` object to add custom widgets
182+
* `language` string to set the error message language (currently supports 'en' and 'fr')
183+
* `framework` string or object to set which framework to use
184+
185+
For `framework`, you can pass in your own custom framework object, or, if you've loaded multiple frameworks, you can specify the name of the framework you want to use. To switch between the included frameworks, use 'material-design', 'bootstrap-3', 'bootstrap-4', and 'no-framework'.
176186

177187
If you want more detailed output, you may provide additional functions for `onChanges` to read the values in real time as the form is being filled out, and you may implement your own custom validation indicators from the boolean `isValid` or the detailed `validationErrors` outputs.
178188

@@ -185,6 +195,7 @@ Here is an example:
185195
[(data)]="yourData"
186196
[options]="yourFormOptions"
187197
[widgets]="yourCustomWidgets"
198+
language="fr"
188199
framework="material-design"
189200
loadExternalAssets="true"
190201
(onChanges)="yourOnChangesFn($event)"
@@ -194,18 +205,40 @@ Here is an example:
194205
</json-schema-form>
195206
```
196207

208+
Note: If you prefer brackets around all your attributes, the following is functionally equivalent:
209+
210+
```html
211+
<json-schema-form
212+
[schema]="yourJsonSchema"
213+
[layout]="yourJsonFormLayout"
214+
[(data)]="yourData"
215+
[options]="yourFormOptions"
216+
[widgets]="yourCustomWidgets"
217+
[language]="'fr'"
218+
[framework]="'material-design'"
219+
[loadExternalAssets]="true"
220+
(onChanges)="yourOnChangesFn($event)"
221+
(onSubmit)="yourOnSubmitFn($event)"
222+
(isValid)="yourIsValidFn($event)"
223+
(validationErrors)="yourValidationErrorsFn($event)">
224+
</json-schema-form>
225+
```
226+
227+
If you use this syntax, make sure to include the nested quotes (`"'` and `'"`) around the language and framework names. (If you leave out the inner quotes, Angular will read them as a variable names, rather than strings, which will cause errors. All un-bracketed attributes, however, are automatically read as strings, so they don't need inner quotes.)
228+
197229
#### Single-input mode
198230

199231
You may also combine all your inputs into one compound object and include it as a `form` input, like so:
200232

201233
```javascript
202234
let yourCompoundInputObject = {
203-
schema: { ... }, // required
235+
schema: { ... }, // REQUIRED
204236
layout: [ ... ], // optional
205237
data: { ... }, // optional
206238
options: { ... }, // optional
207239
widgets: { ... }, // optional
208-
framework: { ... } // optional
240+
language: '...' , // optional
241+
framework: '...' // (or { ... }) optional
209242
}
210243
```
211244

@@ -216,9 +249,9 @@ let yourCompoundInputObject = {
216249
</json-schema-form>
217250
```
218251

219-
You can also mix these two styles depending on your needs. In the example playground, all examples use the combined `form` input for `schema`, `layout`, and `data`, which enables each example to control those three inputs, but the playground uses another input for `framework`, enabling you to change the framework independent of the example.
252+
You can also mix these two styles depending on your needs. In the example playground, all examples use the combined `form` input for `schema`, `layout`, and `data`, which enables each example to control those three inputs, but the playground uses separate inputs for `language` and `framework`, enabling it to change those settings independent of the example.
220253

221-
Combining inputs is useful if you have many unique forms and store each form's data and schema together. If you have one form (or many identical forms), it will likely be more useful to use separate inputs for your data and schema—though even in that case, if you use a custom layout, you could store your schema and layout together and use one input for both.
254+
Combining inputs is useful if you have many unique forms and store each form's data and schema together. If you have one form (or many identical forms), it will likely be more useful to use separate inputs for your data and schema. Though even in that case, if you use a custom layout, you could store your schema and layout together and use one input for both.
222255

223256
#### Compatibility modes
224257

@@ -254,9 +287,7 @@ JSON Form (jQuery) compatibility:
254287
</json-schema-form>
255288
```
256289

257-
Note: 2-way data binding will work with any dedicated data input,
258-
including 'data', 'model', 'ngModel', or 'formData'.
259-
However, 2-way binding will _not_ work with the combined 'form' input.
290+
Note: 2-way data binding will work with any dedicated data input, including 'data', 'model', 'ngModel', or 'formData'. However, 2-way binding will _not_ work with the combined 'form' input.
260291

261292
#### Debugging inputs and outputs
262293

@@ -377,7 +408,7 @@ maxItems | array | maximumItems, currentItems
377408
uniqueItems | array | duplicateItems
378409
contains * | array | requiredItem
379410

380-
* Note: The contains and dependencies validators are still in development, and do not yet work correctly.
411+
* Note: The `contains` and `dependencies` validators are still in development, and do not yet work correctly.
381412

382413
### Changing or adding widgets
383414

0 commit comments

Comments
 (0)