如何使用函数返回的数据数组设置json模型

时间:2017-07-20 13:59:02

标签: javascript json sapui5

我有一个sapui5应用程序:

我有一个名为“Submission.view和相应的控制器”的xml视图,名为“submission.controller”。

我的方法是使用某些输入参数从服务中获取一些问题ID。

例如,如果我传递“null”和“previous”,它会给我一个json响应:

[
{
        "__type__": "QuestionOutput",
        "QuestionType": "",
        "QuestionID": 51,
        "YesNo": "",
        "Mandatory": false
    },
    {
        "__type__": "QuestionOutput",
        "QuestionType": "",
        "QuestionID": 52,
        "YesNo": "",
        "Mandatory": false
    },
    {
        "__type__": "QuestionOutput",
        "QuestionType": "",
        "QuestionID": 53,
        "YesNo": "",
        "Mandatory": false
    }
]

现在我想将每个像51,52,53这样的问题传递给另一个方法,该方法调用休息服务从hana数据库中获取上述问题的描述。

getBpmRules: function(sToken, sQuestionId, sAnswer) {
        var that = this;

        this.showBusy();
        jQuery.ajax({
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            async: false,
            url: "/bpmrulesruntime/rules-service/v1/rules/invoke?rule_service_name=SelfServiceRule::QuestionRuleService",
            context: that,
            xhrFields: {
                withCredentials: true
            },
            headers: {
                'X-CSRF-Token': sToken
            },
            data: JSON.stringify([{
                "__type__": "QuestionInput",
                "QuestionID": "" + sQuestionId + "",
                "Answer": "" + sAnswer + ""
            }]),

            error: function(jqXHR, textStatus, errorThrown) {
                that.hideBusy();
                sap.m.MessageBox.alert(textStatus + " - " + errorThrown + " : " + jqXHR.responseText, {
                    icon: sap.m.MessageBox.Icon.ERROR,
                    title: this.getText("error")
                });
            },
            success: function(data, textStatus, jqXHR) {
                var questiondesc = [];

                if (data.length !== 0) {
                    var desc = [];
                    //this.getModel("questionModel").setProperty("/questionDescription", data);
                    for (var i = 0; i < data.length; i++) {
                        desc = that.getQuestionDescriptionById(data[i].Question, sToken);

                    }
                }

                that.hideBusy();
                this._oManagerBusy.setVisible(false);
                this._showSendDialog();

            }
        });
    }

获取json数据后,我只是循环并将问题ID传递给另一个方法来获取每个问题的描述,这也是json响应。

getQuestionDescriptionById: function(id, token) {
        jQuery.sap.log.debug("Getting question by Id");
        var questionDescription = [];
        this.getComponent().ajax({
            method: "GET",
            url: "/selfservice/rest/question/admin/questionid/" + id,

            contentType: "application/json",
            context: this,
            headers: {
                'X-CSRF-Token': token
            },
            error: function(jqXHR, textStatus, errorThrown) {
                this.getModel("resources").setProperty("/ticketSubmitMessage", this.getModel("i18n").getProperty("sendDialogText"));
                this._oManagerBusy.setVisible(false);
                this._showSendDialog();
            },
            success: function(data, textStatus, jqXHR) {
                //oModel.loadData(“json/Item.json”);
                //  this.getView().setModel(new JSONModel(data.question,"questionDescription"));

                if (data.length !== 0) {

                    questionDescription.push(data);

                }

            }

        });
        return questionDescription;
    },

问题:

我无法使用我从第二种方法获得的json响应为视图设置模型,因为对于每个questionid,它转到下一个方法“getQuestionDescriptionById”,即使我返回响应..也没有不行。

请告诉我如何为每个问题ID获得的每个响应设置一个json模型。

谢谢!

0 个答案:

没有答案