Skip to content

Commit 6356e7b

Browse files
committed
Merge pull request #1 from topaz1008/master
Added support for promises on schema and startval.
2 parents 4be0fc4 + 32e03ea commit 6356e7b

17 files changed

+344
-116
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.idea
22
/bower_components
3+
/build
34
/node_modules
45

56
[Tt]humbs.db

.jscs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"excludeFiles": ["bower_components/**", "node_modules/**"],
2+
"excludeFiles": ["bower_components/**", "node_modules/**", "build/**", "dist/**"],
33
"disallowKeywords": ["with"],
44
"disallowTrailingWhitespace": true,
55
"requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"],

.jshintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
bower_components/**
2+
build/**
3+
dist/**
24
node_modules/**

.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"node": true,
5353
"nonstandard": false,
5454
"predef": [
55-
"angular"
55+
"angular",
56+
"JSONEditor"
5657
]
5758
}

README.md

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
JSON Editor Angular directive
22
=============================
33

4-
angular-json-editor is an angular module that provides a directive that wraps [jdorn's JSON Editor](https://github.com/jdorn/json-editor).
4+
A directive that wraps [jdorn's json-editor](https://github.com/jdorn/json-editor).
55

66
JSON Editor takes a JSON Schema and uses it to generate an HTML form.
77

8-
For further information about supported schema properties and usage, check out the original [JSON Editor repository](https://github.com/jdorn/json-editor).
8+
For further information about supported schema properties and usage, check out the original [json-editor](https://github.com/jdorn/json-editor).
99

1010
Requirements
1111
----------------
@@ -15,59 +15,36 @@ The module doesn't include the original json-editor code, but it is included in
1515
Installation
1616
------------
1717

18-
Install angular-json-editor using bower, which will install the source json-editor as well.
18+
Install via bower
1919

20-
If you are not using bower, please download the original [json-editor from here](https://github.com/jdorn/json-editor).
20+
bower install angular-json-editor --save
21+
22+
Then include the directive and json-editor in your html (you can also use the minified versions)
23+
24+
```html
25+
<script src="bower_components/json-editor/dist/jsoneditor.js"></script>
26+
<script src="bower_components/angular-json-editor/angular-json-editor.js"></script>
27+
```
2128

2229
Usage
2330
-----
2431

25-
Include the json-editor and angular scripts first, and then include angular-json-editor.
26-
27-
There are two optional ways of passing a json schema to the editor.
28-
29-
* Pass the schema using a scope object as the value of the directive's attribute
30-
31-
```js
32-
.controller('MyCtrl', function ($scope) {
33-
$scope.schemaObj = {
34-
"type": "object",
35-
"properties": {
36-
"name": {
37-
"type": "string",
38-
"required": true,
39-
"minLength": 1
40-
},
41-
"age": {
42-
"type": "integer",
43-
"title": "Age",
44-
"min": 0
45-
}
46-
}
47-
};
48-
}
49-
```
50-
51-
```html
52-
<div json-editor="schemaObj"></div>
53-
```
54-
55-
* Use an external JSON file as the schema and pass the url using the schema-url attribute
56-
57-
```html
58-
<div json-editor="" schema-url="/schema/person.json"></div>
59-
```
60-
61-
The editor object that is returned from JSON-Editor can be made available to the scope by using the editor="" attribute.
62-
```html
63-
<div json-editor="schemaObj" editor="editor1"></div>
64-
```
32+
The directive supports both synchronous and asynchronous values, all values can be either a scope object, or a promise returned from $q, $http, $timeout, $resource etc.
33+
Please check out `demo/index.html` and `demo/app.js` for an example usage of both scenarios.
6534

6635
### Validation
67-
The directive can attach an isValid property to the scope by using the is-valid attribute.
36+
The directive exposes an `isValid` property on the scope, which can be used to enable/disable show/hide buttons using `ng-disabled/ng-enabled` or `ng-hide/ng-show`.
6837
```html
69-
<form>
70-
<div json-editor="schemaObj" editor="editor1" is-valid="isValid"></div>
71-
<input type="submit" ng-disabled="!isValid"
72-
</form>
73-
```
38+
<button type="button" ng-disabled="!isValid">Button 1</button>
39+
```
40+
41+
Building
42+
---------
43+
44+
Clone the project, install bower and npm dependencies by running
45+
46+
bower install && npm install
47+
48+
If you dont have grunt-cli installed globally run `npm install -g grunt-cli`
49+
50+
Then run `grunt` and look in the `dist` folder.

bower.json

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
{
22
"name": "angular-json-editor",
33
"version": "0.0.1",
4-
"main": "src/angular-json-editor.js",
4+
"authors": [
5+
"Rodik Hanukaev <[email protected]>",
6+
"Topaz Bar <[email protected]>"
7+
],
8+
"description": "angular.js directive for jdorn's json-editor",
9+
"license": "MIT",
10+
"main": "src/angular-json-editor.js",
11+
"keywords": [
12+
"angular",
13+
"directive",
14+
"json-editor",
15+
"form",
16+
"schema"
17+
],
18+
"ignore": [
19+
"bower_components",
20+
"node_modules"
21+
],
522
"dependencies": {
6-
"json-editor": "*",
7-
"angular": "1.2.*"
23+
"json-editor": "~0.6.17",
24+
"angular": "*"
825
}
926
}

demo/app.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
'use strict';
22

3-
angular.module('demoApp', ['angular-json-editor'])
4-
.controller('MyCtrl', function ($scope) {
5-
$scope.ctrlSchema = {
3+
angular.module('demoApp', ['angular-json-editor']).config(function (JsonEditorConfig) {
4+
5+
// angular-json-editor configuration
6+
JsonEditorConfig.iconlib = 'bootstrap3';
7+
JsonEditorConfig.theme = 'bootstrap3';
8+
9+
// Configure the buttons controller
10+
JsonEditorConfig.controller = 'ButtonsController';
11+
12+
}).controller('SyncAppController', function ($scope) {
13+
14+
$scope.mySchema = {
615
type: 'object',
716
properties: {
817
name: {
@@ -20,16 +29,35 @@ angular.module('demoApp', ['angular-json-editor'])
2029
}
2130
};
2231

23-
$scope.startval = {
32+
$scope.myStartVal = {
2433
age: 20
2534
};
2635

36+
}).controller('AsyncAppController', function ($scope, $http, $timeout) {
37+
38+
// Load with $http
39+
$scope.mySchema = $http.get('schema.json');
40+
41+
// Values can be a promise from anywhere
42+
$scope.myStartVal = $timeout(function () {
43+
return {
44+
age: 20
45+
};
46+
47+
}, 2000);
48+
49+
}).controller('ButtonsController', function ($scope) {
50+
51+
/**
52+
* Custom actions controller which allows you to add any other buttons/actions to the form.
53+
*/
54+
2755
$scope.onSubmit = function () {
28-
var formData = $scope.editor.getValue();
29-
console.log('formData', formData);
56+
console.log('onSubmit Data', $scope.editor.getValue());
57+
};
58+
59+
$scope.onAction2 = function () {
60+
console.log('onAction2');
3061
};
31-
});
3262

33-
angular.element(window.document).ready(function () {
34-
angular.bootstrap(window.document, ['demoApp']);
3563
});

demo/index.html

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,45 @@
44
<meta charset="utf-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
66
<meta name="author" content="Rodik">
7+
<title>angular-json-editor Demo</title>
8+
79
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
810
</head>
9-
<body>
10-
<div class="container" ng-controller="MyCtrl">
11-
<form ng-submit="onSubmit()">
12-
<div json-editor="ctrlSchema" startval="startval" editor="editor" is-valid="isValid"></div>
13-
<input type="submit" value="Submit" ng-disabled="!isValid">
14-
</form>
15-
</div>
16-
17-
<script src="../bower_components/angular/angular.js"></script>
18-
<script src="../bower_components/json-editor/dist/jsoneditor.js"></script>
19-
<script src="../src/angular-json-editor.js"></script>
20-
<script src="../demo/app.js"></script>
11+
<body ng-app="demoApp">
12+
13+
<h2>Synchronous values</h2>
14+
<div class="container" ng-controller="SyncAppController">
15+
<json-editor schema="mySchema" startval="myStartVal" editor="myEditor">
16+
17+
<button type="submit" class="btn btn-success" ng-click="onSubmit($event)" ng-disabled="!isValid">
18+
<span class="glyphicon glyphicon-check"></span>
19+
Submit
20+
</button>
21+
22+
<button type="button" class="btn btn-danger" ng-click="onAction2($event)">
23+
<span class="glyphicon glyphicon-remove"></span>
24+
Action 2
25+
</button>
26+
27+
</json-editor>
28+
</div>
29+
30+
<h2>Asynchronous values</h2>
31+
<div class="container" ng-controller="AsyncAppController">
32+
<json-editor schema="mySchema" startval="myStartVal" editor="myEditor">
33+
34+
<button type="submit" class="btn btn-success" ng-click="onSubmit($event)" ng-disabled="!isValid">
35+
<span class="glyphicon glyphicon-check"></span>
36+
Submit
37+
</button>
38+
39+
</json-editor>
40+
</div>
41+
42+
<script src="../bower_components/json-editor/dist/jsoneditor.js"></script>
43+
<script src="../bower_components/angular/angular.js"></script>
44+
<script src="../src/angular-json-editor.js"></script>
45+
<script src="app.js"></script>
46+
2147
</body>
2248
</html>

dist/angular-json-editor.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

grunt/clean.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = function (grunt) {
4+
5+
grunt.config('clean', {
6+
all: ['dist/*']
7+
});
8+
9+
grunt.loadNpmTasks('grunt-contrib-clean');
10+
};

0 commit comments

Comments
 (0)