Skip to content

Commit

Permalink
Serialize custom attributes in ItemValue:
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 1, 2017
1 parent c7daca3 commit aa605c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/itemvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ export class ItemValue {
public static getData(items: Array<ItemValue>): any {
var result = new Array();
for (var i = 0; i < items.length; i++) {
var item = items[i];
var textJson = item.locText.getJson();
if (textJson) {
result.push({ value: item.value, text: textJson});
} else {
result.push(item.value);
}
result.push(items[i].getData());
}
return result;
}
Expand All @@ -63,7 +57,7 @@ export class ItemValue {
items[i].locText.onChanged();
}
}
private static itemValueProp = [ "text", "value", "hasText", "locOwner", "locText", "isValueEmpty"];
private static itemValueProp = [ "text", "value", "hasText", "locOwner", "locText", "isValueEmpty", "locTextValue"];
private itemValue: any;
private locTextValue: LocalizableString;
constructor(value: any, text: string = null) {
Expand Down Expand Up @@ -93,6 +87,19 @@ export class ItemValue {
public set text(newText: string) {
this.locText.text = newText;
}
public getData(): any {
var customAttributes = this.getCustomAttributes();
var textJson = this.locText.getJson();
if(!customAttributes && !textJson) return this.value;
var result = {value: this.value};
if(textJson) result["text"] = textJson;
if(customAttributes) {
for(var key in customAttributes) {
result[key] = customAttributes[key];
}
}
return result;
}
public setData(value: any) {
if (typeof (value.value) !== 'undefined') {
var exception = null;
Expand All @@ -119,4 +126,13 @@ export class ItemValue {
}
}
}
private getCustomAttributes(): any {
var result = null;
for (var key in this) {
if ((typeof this[key] == 'function') || ItemValue.itemValueProp.indexOf(key) > -1 || key == "itemValue") continue;
if(result == null) result = {};
result[key] = this[key];
}
return result;
}
}
13 changes: 12 additions & 1 deletion tests/surveyserializationtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,15 @@ QUnit.test("Survey deserialize/serialize localization survey", function (assert)
QUnit.test("Survey deserialize dynamic matrix with different locale, Issue #507", function (assert) {
var survey = new SurveyModel({ pages: [ { name: "p1", elements: [{type: "matrixdropdown", name: "q1", columns: [{ name: "Column 1"}], rows: ["Row 1","Row 2"]}] } ], locale: "zh-cn"});
assert.equal(survey.getQuestionByName("q1").name, "q1", "Matrix deserialized successful");
});
});

QUnit.test("Survey checkbox.choices serialize/deserialize custom properties", function (assert) {
var question = new QuestionCheckboxModel("q1");
var jsonObj = new JsonObject();
var originalJson = {name: "q1", choices: [ { value: "2", imageLink: "link to image" }]};
jsonObj.toObject(originalJson, question);
assert.equal((<ItemValue>question.choices[0]).text, "2", "The default locale is 2");
assert.equal((<ItemValue>question.choices[0])["imageLink"], "link to image", "Custom property is deserialized");
var json = jsonObj.toJsonObject(question);
assert.deepEqual(json, originalJson, "Custom property has serialized correctly");
});

0 comments on commit aa605c0

Please sign in to comment.