Skip to content

Commit 128dc4a

Browse files
committed
Make definitionsCallback be possible to reach with string-based method resolution
1 parent 1913041 commit 128dc4a

File tree

5 files changed

+143
-73
lines changed

5 files changed

+143
-73
lines changed

angular-schema-form-complex-ui.js

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,50 @@ var ComplexUIController = (function () {
1515
this.toggleModal = function () {
1616
this.directiveScope.showModal = !this.directiveScope.showModal;
1717
};
18+
this.getCallback = function (callback) {
19+
if (typeof (callback) == "string") {
20+
var _result = _this.directiveScope.$parent.evalExpr(callback);
21+
if (typeof (_result) == "function") {
22+
return _result;
23+
}
24+
else {
25+
throw ("A callback string must match name of a function in the parent scope");
26+
}
27+
}
28+
else if (typeof (callback) == "function") {
29+
return callback;
30+
}
31+
else {
32+
throw ("A callback must either be a string matching the name of a function in the parent scope or a " +
33+
"direct function reference");
34+
}
35+
};
1836
this.getDefinitions = function () {
1937
if (_this.directiveScope.form["options"]) {
38+
var schemaRef = void 0;
2039
if ("schemaRef" in _this.directiveScope.form["options"]) {
21-
var schemaRef = _this.directiveScope.form["options"]["schemaRef"];
40+
schemaRef = _this.directiveScope.form["options"]["schemaRef"];
2241
}
2342
else {
24-
var schemaRef = null;
25-
}
26-
var _defs = _this.directiveScope.form["options"]["definitionsCallback"](schemaRef);
27-
if ("form" in _defs) {
28-
_this.form = _defs["form"];
43+
schemaRef = null;
2944
}
30-
else if ("complexForm" in _this.directiveScope.form["options"]) {
31-
_this.form = _this.directiveScope.form["options"]["complexForm"];
32-
}
33-
else {
34-
_this.form = ["*"];
45+
if ("definitionsCallback" in _this.directiveScope.form["options"]) {
46+
var callback = _this.getCallback(_this.directiveScope.form["options"]["definitionsCallback"]);
47+
var _defs = callback(schemaRef);
48+
// TODO: This is probably in the wrong order, it should be possible to read form and schema the usual way.
49+
// How can some get a schema and some not.
50+
if ("form" in _defs) {
51+
_this.form = _defs["form"];
52+
}
53+
else if ("complexForm" in _this.directiveScope.form["options"]) {
54+
_this.form = _this.directiveScope.form["options"]["complexForm"];
55+
}
56+
else {
57+
_this.form = ["*"];
58+
}
59+
_this.form.onChange = _this.directiveScope.form.onChange;
60+
_this.schema = _defs["schema"];
3561
}
36-
_this.schema = _defs["schema"];
3762
}
3863
};
3964
this.innerSubmit = function (form) {
@@ -45,7 +70,7 @@ var ComplexUIController = (function () {
4570
this.directiveScope = $scope;
4671
}
4772
return ComplexUIController;
48-
})();
73+
}());
4974
;
5075
angular.module('schemaForm').directive('modal', function () {
5176
return {
@@ -80,7 +105,7 @@ angular.module('schemaForm').directive('complexUiDirective', function () {
80105
return {
81106
require: [],
82107
restrict: 'A',
83-
// Do not create a isolate scope, makeCamelCase should be available to the button element
108+
// Do not create a isolate scope, pass on
84109
scope: false,
85110
// Define a controller, use the function from above, inject the scope
86111
controller: ['$scope', ComplexUIController],

angular-schema-form-complex-ui.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.

example.js

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,51 @@ exampleApp.controller("exampleController", ["$scope", function ($scope) {
1818
// This function returns the schema and form for the field.
1919
$scope.getDefinitions = function(_ref) {
2020
return {
21-
schema: {
21+
schema:
22+
{
2223
type: "object",
2324
title: "Complex UI test",
2425
properties: {
2526
test1: {
26-
type: "string",
27-
description: "So this is inside the nested ASF instance"
27+
"properties": {
28+
"delete": {
29+
"type": "boolean"
30+
},
31+
"insert": {
32+
"type": "boolean"
33+
},
34+
"mappings": {
35+
"items": {
36+
"properties": {
37+
"dest_reference": {
38+
"type": "string"
39+
},
40+
"is_key": {
41+
"type": "boolean"
42+
},
43+
"src_datatype": {
44+
"type": "string"
45+
},
46+
"src_reference": {
47+
"type": "string"
48+
},
49+
"substitution": {
50+
"properties": {},
51+
"type": "object"
52+
}
53+
},
54+
"type": "object"
55+
},
56+
"type": "array"
57+
},
58+
"post_execute_sql": {
59+
"type": "string"
60+
},
61+
"update": {
62+
"type": "boolean"
63+
}
64+
},
65+
"type": "object"
2866
},
2967
test2: {
3068
type: "string",
@@ -33,30 +71,8 @@ exampleApp.controller("exampleController", ["$scope", function ($scope) {
3371
},
3472
required: ["test1"]
3573
},
36-
form : [
37-
{
38-
"key": "test1",
39-
"title": "Inside the schema form.",
40-
"type": "string"
41-
},
42-
{
43-
"key": "test2 ",
44-
"title": "Inside the schema form.",
45-
"type": "select",
46-
"titleMap": [
47-
{"value": "value1", "name": "text1"},
48-
{"value": "value2", "name": "text2"},
49-
{"value": "value3", "name": "text3"},
50-
{"value": "value4", "name": "text4"}
51-
]
52-
},
53-
{
54-
type: "button",
55-
style: "btn-ok",
56-
title: "OK inner",
57-
onClick: "controller.innerSubmit(this)"
58-
}
59-
]
74+
form : ["*"]
75+
6076
}
6177
};
6278

@@ -87,29 +103,32 @@ exampleApp.controller("exampleController", ["$scope", function ($scope) {
87103
"title": "Example of complex structure editor",
88104
"type": "complex-ui",
89105
"options": {
90-
"definitionsCallback": $scope.getDefinitions,
106+
"definitionsCallback": "getDefinitions",
91107
"modal": true,
92108
"buttonCaption": "..",
93109
"includeURI": "example_include.html"
94-
}
110+
},
111+
"onChange": $scope.onChange
112+
95113
},
96114
{
97115
"key": "anyfield",
98116
"title": "A string",
99117
"options": {
100-
"definitionsCallback": $scope.getDefinitions
101-
}
118+
"definitionsCallback": "getDefinitions"
119+
},
120+
"onChange": $scope.onChange
102121
},
103122
{
104123
type: "submit",
105124
style: "btn-ok",
106125
title: "OK outer"
107126
}
108127
];
109-
// Initiate the model
110-
$scope.model = {};
111-
// Initiate one of the inputs
112-
$scope.model.complexUIField = {"test1": "A value."};
128+
129+
$scope.onChange = function (modelValue, key) {
130+
console.log("Value: " + modelValue + " Key:" + key)
131+
};
113132

114133
// This is called by asf on submit, specified in example.html, ng-submit.
115134
$scope.submitted = function (form) {

src/angular-schema-form-complex-ui.html

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
<!--
2-
This is the implementation of the view. When the button is clicked, the value in the model is camelCase:d.
31

4-
It uses a directive to get to the ngModel properly.
5-
If you have no need to modify ngModel directly, you can also just use a controller, no need for a directive.
6-
If you have no need to for any local, behind-the-scenes logic, you might not need a controller at all.
7-
8-
All references to css classes here are for bootstrap classes, for them to work, Bootstrap has to be installed.
9-
10-
IMPORTANT: The ASF magic $$value$$ will be changed by ASF into model['camelcase_format'] or model['camelcase_form_type']
11-
during compilation. This is the recommended way to access the model until there is a better solution.
12-
-->
132
<!-- hasError is implemented by ASF and returns true if there is a validation error. -->
143
<div ng-class="{'has-error': hasError()}">
154
<!-- Specify the model for the directive and add the directive, making the controller available to the children-->

0 commit comments

Comments
 (0)