如何在openui5中刷新xml页面时将数据传送到控制器

时间:2015-10-27 07:26:25

标签: sapui5

我希望在刷新XML页面时从控制器获取数据,并且该数据来自另一个页面控制器。下面是我的代码。

header.controller.js中的这个函数

categoryfunc: function (oEvent) {
        var CatId = oEvent.getSource().mProperties.target;
        if (CatId !== "") {
            leftmenudata = this.nav.ajaxactioncalls({
                url: serviceData.leftmenuinfo,
                data: {categoryId: CatId},
                type: "POST"
            });
            if (leftmenudata.length === 1) {
                leftmenudetail = new sap.ui.model.json.JSONModel(leftmenudata.info);
                this.getView().setModel(leftmenudetail, "leftmenuinfo");
                var viewNavcontent = sap.ui.xmlview("acetrix.shopping.view.dashboard.navcontent");
                viewNavcontent.getController().leftmenufunc(leftmenudetail);
                var stockinfo = {stockdata: leftmenudata.info.stockdata};
                stockdetail = new sap.ui.model.json.JSONModel(stockinfo);
                this.getView().setModel(stockdetail, "stockdetail");
                var dataa = {link: 'dashboard.cartitems', leftNav: true};
                this.nav.navPageTo(dataa);
            } else {
                var durationtime = Number(serviceData.popupduration);
                sap.m.MessageToast.show(leftmenudata.message, {duration: durationtime, at: "center top"});
                $(".sapMMessageToast").addClass("errormessagetoast");
            }
        }
    }

单击菜单链接时,此功能可以工作并渲染到cartitems.view.xml。我在控制器中调用与此视图相关的数据,如下所示:

onAfterRendering: function (oEvent) {
        var stockinfo = stockdetail.getData();
        cartinfo = new sap.ui.model.json.JSONModel(stockinfo);
        this.getView().setModel(cartinfo, "stockdetail");
    }

它以正常方式工作,但当我刷新页面时会出现错误,如

  

错误:未定义stockdetail。

如何纠正这个?

1 个答案:

答案 0 :(得分:0)

It seems that when you refresh the page onAfterRendering function is called before categoryfunc function; this causes the value undefined of stockdetail variable.

The rigth way to define a model is do it in onInit function that is automatically called before any other function in the controller