用于数组层次结构的SAPUI5绑定上下文

时间:2017-11-13 12:59:40

标签: sapui5

我有一个包含答案数组的问题对象

var oModel = new JSONModel({
    questions: [{
        order: 1,
        title: "",
        answers: [{
            sequence: 1,
            label: "Yes",
        }, {
            sequence: 3,
            label: "N/A",
        }]
    }]
});

上下文设置如下

this.getView().setModel(oModel, "viewmodel");
var oContext = oModel.createBindingContext("/questions/0/");
this.getView().setBindingContext(oContext, "viewmodel");

在我看来,我可以很好地约束问题

<Input value="{viewmodel>title}"/>

然而,更新答案绑定会更新每个问题上下文中的所有答案数组!下面我绑定到我当前的上下文(问题/ 0 /),但更新标签将更新所有问题的答案....

<f:Form id="formCustomRadio" editable="true" visible="true" formContainers="{viewmodel>answers}">
    <f:layout>
        <f:ResponsiveGridLayout/>
    </f:layout>
    <f:formContainers>
        <f:FormContainer title="Answer {viewmodel>sequence}">
            <f:formElements>
                    <f:FormElement label=" {i18n>radioLabel}">
                        <f:fields>
                            <Input value="{viewmodel>label}"/>
                        </f:fields>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题与创建新问题时阵列推送有关......

questionData.questions.push({
    order: questionData.questions.length + 1,
    title: questionData.questions[questionData.questions.length - 1].title,
    answers: questionData.questions[questionData.questions.length - 1].answers
});

由于答案是一系列对象,因此您无法简单地推送它,因为这会创建重复的引用。这就是每次更新后重复的原因。解决方案是在for循环中单独推送每个答案。

    for (var i = 0; i < answerArray.length; i++) {

            questionData.questions[newIndex].answers.push({
                sequence: answerArray[i].sequence,
                label: answerArray[i].label,
                points: answerArray[i].points,
                imageFlag: answerArray[i].imageFlag,
                image: answerArray[i].image,
                mandatoryComments: answerArray[i].mandatoryComments,
                additionalQuestion: answerArray[i].additionalQuestion
            });
        }