Skip to content

Commit 4cb9efe

Browse files
committed
Added support for key/value typed constraints #177
1 parent 04b0441 commit 4cb9efe

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

grails-app/assets/javascripts/forms.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,10 @@ function orEmptyArray(v) {
667667

668668
};
669669

670-
function applyIncludeExclude(metadata, outputModel, observable, initialConstraints) {
670+
function applyIncludeExclude(metadata, outputModel, dataModelItem, initialConstraints) {
671671
var path = metadata.constraints.excludePath || metadata.constraints.includePath;
672672

673-
var currentValue = observable();
673+
var currentValue = dataModelItem();
674674
var selectedSoFar = [];
675675
outputModel.eachValueForPath(path, function (val) {
676676
if (_.isArray(val)) {
@@ -681,11 +681,17 @@ function orEmptyArray(v) {
681681
});
682682

683683
if (metadata.constraints.excludePath) {
684-
return _.filter(initialConstraints, function (value) {
684+
var filteredConstraints = _.filter(initialConstraints, function (value) {
685+
// Handle object valued constraints - however the functions are attached after the first call to
686+
// the computed observable, so we need to guard against that.
687+
value = _.isFunction(dataModelItem.constraints.value) ? dataModelItem.constraints.value(value) : value;
685688
var isCurrentSelection = _.isArray(currentValue) ? currentValue.indexOf(value) >= 0 : value == currentValue;
686689
return (isCurrentSelection || selectedSoFar.indexOf(value) < 0);
687690
});
691+
return filteredConstraints;
688692
} else {
693+
// Note that the "includePath" option does not support object (key/value) typed constraints as
694+
// it needs to collect values from other form selections which won't be object typed.
689695
var constraints = initialConstraints.concat(selectedSoFar);
690696
var currentSelection = _.isArray(currentValue) ? currentValue : [currentValue];
691697
for (var i = 0; i < currentSelection.length; i++) {

grails-app/controllers/au/org/ala/ecodata/forms/PreviewController.groovy

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import org.grails.io.support.Resource
88

99
class PreviewController {
1010

11+
static responseFormats = ['json', 'xml']
12+
1113
private static String EXAMPLE_MODEL = 'example.json'
1214
private static String EXAMPLE_MODELS_PATH = '/example_models/'
1315
private static String EXAMPLE_DATA_PATH = '/example_data/'

src/test/js/spec/DataModelItemSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,11 @@ describe("DataModelItem Spec", function () {
199199
vals = [['1', '2'], ['3']];
200200
dataItem = ko.observableArray().extend({metadata:{metadata:metadata, context:customContext, config:config}});
201201
expect(dataItem.constraints()).toEqual(['1', '2', '3']);
202+
203+
vals = ['v1'];
204+
metadata.constraints.default = [{text:'t1', value:'v1'}, {text:'t2', value:'v2'}];
205+
dataItem = ko.observableArray().extend({metadata:{metadata:metadata, context:customContext, config:config}});
206+
expect(dataItem.constraints()).toEqual(['v2']);
207+
202208
});
203209
});

0 commit comments

Comments
 (0)