From aa605c0819a8039d5cc3b74ca9c13ec3090cef84 Mon Sep 17 00:00:00 2001 From: AndrewTelnov Date: Fri, 1 Sep 2017 12:32:32 +0300 Subject: [PATCH] Serialize custom attributes in ItemValue: https://github.com/surveyjs/editor/issues/132 --- src/itemvalue.ts | 32 +++++++++++++++++++++++-------- tests/surveyserializationtests.ts | 13 ++++++++++++- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/itemvalue.ts b/src/itemvalue.ts index 92dfa4f40f..ad9c987371 100644 --- a/src/itemvalue.ts +++ b/src/itemvalue.ts @@ -38,13 +38,7 @@ export class ItemValue { public static getData(items: Array): 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; } @@ -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) { @@ -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; @@ -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; + } } diff --git a/tests/surveyserializationtests.ts b/tests/surveyserializationtests.ts index 6a249bea13..f333283581 100644 --- a/tests/surveyserializationtests.ts +++ b/tests/surveyserializationtests.ts @@ -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"); -}); \ No newline at end of file +}); + +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((question.choices[0]).text, "2", "The default locale is 2"); + assert.equal((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"); +});