Skip to content

Commit 7deab97

Browse files
committed
Fix: "set" property editor doesn't support ajax request for getting choices #720
1 parent 23b36b8 commit 7deab97

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/propertyEditors/propertyDefaultValueEditor.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export class SurveyPropertySetEditor extends SurveyPropertyDefaultValueEditor {
261261
var hasTagbox = !!Survey.Serializer.findClass("tagbox");
262262
question.hasSelectAll = !hasTagbox;
263263
if (!!this.property) {
264-
question.choices = this.property.getChoices(this.object);
264+
question.choices = this.getPropertyChoices();
265265
}
266266
var json = SurveyPropertyDefaultValueEditor.createJsonFromQuestion(
267267
question,
@@ -272,6 +272,18 @@ export class SurveyPropertySetEditor extends SurveyPropertyDefaultValueEditor {
272272
}
273273
return json;
274274
}
275+
private setChoices(choices: Array<any>) {
276+
var survey = this.koSurvey();
277+
if (!survey || survey.getAllQuestions().length > 1) return;
278+
survey.getAllQuestions()[0].choices = choices;
279+
}
280+
private getPropertyChoices(): Array<any> {
281+
if (!this.property) return [];
282+
var self = this;
283+
return this.property.getChoices(this.object, function(choices: any) {
284+
self.setChoices(choices);
285+
});
286+
}
275287
}
276288

277289
SurveyPropertyEditorFactory.registerEditor("value", function(

src/propertyEditors/propertyEditorFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export class SurveyDropdownPropertyEditor extends SurveyPropertyEditorBase {
221221
protected getPropertyChoices(): Array<any> {
222222
if (!this.property) return [];
223223
var self = this;
224-
return (<any>this.property.getChoices)(this.object, function(choices: any) {
224+
return this.property.getChoices(this.object, function(choices: any) {
225225
self.setChoices(choices);
226226
});
227227
}

tests/propertyEditors/propertyEditorsTests.ts

+46-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import {
3737
editorLocalization
3838
} from "../../src/editorLocalization";
3939
import { SurveyPropertyConditionEditor } from "../../src/propertyEditors/propertyConditionEditor";
40-
import { SurveyPropertyDefaultValueEditor } from "../../src/propertyEditors/propertyDefaultValueEditor";
40+
import {
41+
SurveyPropertyDefaultValueEditor,
42+
SurveyPropertySetEditor
43+
} from "../../src/propertyEditors/propertyDefaultValueEditor";
4144
import { SurveyPropertyCellsEditor } from "../../src/propertyEditors/propertyCellsEditor";
4245

4346
export default QUnit.module("PropertyEditorsTests");
@@ -1490,6 +1493,48 @@ QUnit.test("Triggers property editor and setvalue trigger", function(assert) {
14901493
);
14911494
});
14921495

1496+
QUnit.test("'set' property editor", function(assert) {
1497+
Survey.Serializer.addProperty("survey", {
1498+
name: "region:set",
1499+
choices: ["Africa", "Americas", "Asia", "Europe", "Oceania"]
1500+
});
1501+
var survey = createSurvey();
1502+
var setValueEditor = new SurveyPropertySetEditor(
1503+
Survey.Serializer.findProperty("survey", "region")
1504+
);
1505+
setValueEditor.object = survey;
1506+
setValueEditor.beforeShow();
1507+
var question = setValueEditor.koSurvey().getAllQuestions()[0];
1508+
assert.deepEqual(question.choices.length, 5, "There are 5 choices");
1509+
Survey.Serializer.removeProperty("survey", "region");
1510+
});
1511+
1512+
QUnit.test("'set' property editor, get choices on callback, Bug#720", function(
1513+
assert
1514+
) {
1515+
var choices = ["Africa", "Americas", "Asia", "Europe", "Oceania"];
1516+
var callback = null;
1517+
Survey.Serializer.addProperty("survey", {
1518+
name: "region:set",
1519+
choices: function(obj, choicesCallback) {
1520+
callback = choicesCallback;
1521+
return [];
1522+
}
1523+
});
1524+
var survey = createSurvey();
1525+
var setValueEditor = new SurveyPropertySetEditor(
1526+
Survey.Serializer.findProperty("survey", "region")
1527+
);
1528+
setValueEditor.object = survey;
1529+
setValueEditor.beforeShow();
1530+
var question = setValueEditor.koSurvey().getAllQuestions()[0];
1531+
assert.deepEqual(question.choices.length, 0, "There is no choices yet");
1532+
callback(choices);
1533+
question = setValueEditor.koSurvey().getAllQuestions()[0];
1534+
assert.deepEqual(question.choices.length, 5, "There are 5 choices now");
1535+
Survey.Serializer.removeProperty("survey", "region");
1536+
});
1537+
14931538
QUnit.test("Validators property editor", function(assert) {
14941539
var survey = createSurvey();
14951540
var validator = new Survey.NumericValidator(10, 100);

0 commit comments

Comments
 (0)