Skip to content

Commit

Permalink
Merge pull request #2 from topaz1008/master
Browse files Browse the repository at this point in the history
Refactored buttons controller to an attribute on json-editor
  • Loading branch information
rodikh committed Jun 25, 2014
2 parents 6356e7b + be79086 commit 5ca2227
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Install via bower
Then include the directive and json-editor in your html (you can also use the minified versions)

```html
<script src="bower_components/json-editor/dist/jsoneditor.js"></script>
<script src="bower_components/angular-json-editor/angular-json-editor.js"></script>
<script src="bower_components/json-editor/dist/jsoneditor.js"></script>
<script src="bower_components/angular-json-editor/angular-json-editor.js"></script>
```

Usage
Expand Down
15 changes: 9 additions & 6 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ angular.module('demoApp', ['angular-json-editor']).config(function (JsonEditorCo
JsonEditorConfig.iconlib = 'bootstrap3';
JsonEditorConfig.theme = 'bootstrap3';

// Configure the buttons controller
JsonEditorConfig.controller = 'ButtonsController';

}).controller('SyncAppController', function ($scope) {

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

}, 2000);
}, 1500);

}).controller('ButtonsController', function ($scope) {
}).controller('SyncButtonsController', function ($scope) {

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

$scope.onSubmit = function () {
console.log('onSubmit Data', $scope.editor.getValue());
console.log('onSubmit data in sync controller', $scope.editor.getValue());
};

$scope.onAction2 = function () {
console.log('onAction2');
};

}).controller('AsyncButtonsController', function ($scope) {

$scope.onSubmit = function () {
console.log('onSubmit data in async controller', $scope.editor.getValue());
};

});
5 changes: 3 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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

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

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

</body>
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-json-editor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions src/angular-json-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

angular.module('angular-json-editor', []).constant('JsonEditorConfig', {
iconlib: 'bootstrap3',
theme: 'bootstrap3',
controller: angular.noop
theme: 'bootstrap3'

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

Expand All @@ -13,9 +12,27 @@ angular.module('angular-json-editor', []).constant('JsonEditorConfig', {
scope: {
schema: '=',
startval: '=',
editor: '='
buttonsController: '@'
},
controller: JsonEditorConfig.controller,
controller: ['$scope', '$attrs', '$controller', function ($scope, $attrs, $controller) {

var controller, controllerScope, controllerName = $attrs.buttonsController;
if (angular.isString(controllerName) && controllerName !== '') {
controllerScope = {
$scope: $scope
};

try {
controller = $controller(controllerName, controllerScope);
} catch (e) {
// Any exceptions thrown will probably be because the controller specified does not exist
throw new Error('json-editor: buttons-controller attribute must be a valid controller.');
}
} else {
throw new Error('json-editor: buttons-controller attribute must be specified.');
}

}],
link: function (scope, element, attrs, controller, transclude) {
var valueToResolve,
startValPromise = $q.when({}),
Expand Down

0 comments on commit 5ca2227

Please sign in to comment.