Skip to content

Commit 5ca2227

Browse files
committed
Merge pull request #2 from topaz1008/master
Refactored buttons controller to an attribute on json-editor
2 parents 6356e7b + be79086 commit 5ca2227

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Install via bower
2222
Then include the directive and json-editor in your html (you can also use the minified versions)
2323

2424
```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>
25+
<script src="bower_components/json-editor/dist/jsoneditor.js"></script>
26+
<script src="bower_components/angular-json-editor/angular-json-editor.js"></script>
2727
```
2828

2929
Usage

demo/app.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ angular.module('demoApp', ['angular-json-editor']).config(function (JsonEditorCo
66
JsonEditorConfig.iconlib = 'bootstrap3';
77
JsonEditorConfig.theme = 'bootstrap3';
88

9-
// Configure the buttons controller
10-
JsonEditorConfig.controller = 'ButtonsController';
11-
129
}).controller('SyncAppController', function ($scope) {
1310

1411
$scope.mySchema = {
@@ -44,20 +41,26 @@ angular.module('demoApp', ['angular-json-editor']).config(function (JsonEditorCo
4441
age: 20
4542
};
4643

47-
}, 2000);
44+
}, 1500);
4845

49-
}).controller('ButtonsController', function ($scope) {
46+
}).controller('SyncButtonsController', function ($scope) {
5047

5148
/**
5249
* Custom actions controller which allows you to add any other buttons/actions to the form.
5350
*/
5451

5552
$scope.onSubmit = function () {
56-
console.log('onSubmit Data', $scope.editor.getValue());
53+
console.log('onSubmit data in sync controller', $scope.editor.getValue());
5754
};
5855

5956
$scope.onAction2 = function () {
6057
console.log('onAction2');
6158
};
6259

60+
}).controller('AsyncButtonsController', function ($scope) {
61+
62+
$scope.onSubmit = function () {
63+
console.log('onSubmit data in async controller', $scope.editor.getValue());
64+
};
65+
6366
});

demo/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<h2>Synchronous values</h2>
1414
<div class="container" ng-controller="SyncAppController">
15-
<json-editor schema="mySchema" startval="myStartVal" editor="myEditor">
15+
<json-editor schema="mySchema" startval="myStartVal" buttons-controller="SyncButtonsController">
1616

1717
<button type="submit" class="btn btn-success" ng-click="onSubmit($event)" ng-disabled="!isValid">
1818
<span class="glyphicon glyphicon-check"></span>
@@ -29,7 +29,7 @@ <h2>Synchronous values</h2>
2929

3030
<h2>Asynchronous values</h2>
3131
<div class="container" ng-controller="AsyncAppController">
32-
<json-editor schema="mySchema" startval="myStartVal" editor="myEditor">
32+
<json-editor schema="mySchema" startval="myStartVal" buttons-controller="AsyncButtonsController">
3333

3434
<button type="submit" class="btn btn-success" ng-click="onSubmit($event)" ng-disabled="!isValid">
3535
<span class="glyphicon glyphicon-check"></span>
@@ -42,6 +42,7 @@ <h2>Asynchronous values</h2>
4242
<script src="../bower_components/json-editor/dist/jsoneditor.js"></script>
4343
<script src="../bower_components/angular/angular.js"></script>
4444
<script src="../src/angular-json-editor.js"></script>
45+
<!--<script src="../dist/angular-json-editor.min.js"></script>-->
4546
<script src="app.js"></script>
4647

4748
</body>

dist/angular-json-editor.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-json-editor.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
angular.module('angular-json-editor', []).constant('JsonEditorConfig', {
44
iconlib: 'bootstrap3',
5-
theme: 'bootstrap3',
6-
controller: angular.noop
5+
theme: 'bootstrap3'
76

87
}).directive('jsonEditor', ['$q', 'JsonEditorConfig', function ($q, JsonEditorConfig) {
98

@@ -13,9 +12,27 @@ angular.module('angular-json-editor', []).constant('JsonEditorConfig', {
1312
scope: {
1413
schema: '=',
1514
startval: '=',
16-
editor: '='
15+
buttonsController: '@'
1716
},
18-
controller: JsonEditorConfig.controller,
17+
controller: ['$scope', '$attrs', '$controller', function ($scope, $attrs, $controller) {
18+
19+
var controller, controllerScope, controllerName = $attrs.buttonsController;
20+
if (angular.isString(controllerName) && controllerName !== '') {
21+
controllerScope = {
22+
$scope: $scope
23+
};
24+
25+
try {
26+
controller = $controller(controllerName, controllerScope);
27+
} catch (e) {
28+
// Any exceptions thrown will probably be because the controller specified does not exist
29+
throw new Error('json-editor: buttons-controller attribute must be a valid controller.');
30+
}
31+
} else {
32+
throw new Error('json-editor: buttons-controller attribute must be specified.');
33+
}
34+
35+
}],
1936
link: function (scope, element, attrs, controller, transclude) {
2037
var valueToResolve,
2138
startValPromise = $q.when({}),

0 commit comments

Comments
 (0)