在两个视图之间传输数据:上下文绑定

时间:2016-05-06 15:04:06

标签: routing sapui5

我在我的SApui5应用程序中遇到问题,我想在视图之间传递数据,我想得到oEvent.getBindingContext()= undefined,我不知道为什么会发生这种情况,请你帮助我解决这个问题。

我的代码如下:(FirstViewController.js)

    sap.ui.define([
    'jquery.sap.global',
    'sap/m/MessageToast',
    'sap/ui/core/mvc/Controller',
    'sap/ui/model/json/JSONModel'

],

function(jQuery, MessageToast, Controller, JSONModel) {
    "use strict";

    return Controller.extend("umicoreMP.controller.FirstView", {

        onInit: function() {
            this.myModel = new sap.ui.model.json.JSONModel();
            sap.ui.getCore().setModel(this.myModel, "data");
            this.getView().setModel(this.myModel, "data");


        },

        onCreate: function(oEvent) {

            sap.m.MessageToast.show("Item Criado", {
                duration: 3000
            });

            // Set busy indicator during view binding

            var oData = {

                mp: {
                    matDesc: this.oView.byId("idMatDesc").getValue(),
                    qtd: this.oView.byId("idQtd").getValue(),
                    um: this.oView.byId("idUm").getValue(),
                    utilizacao: this.oView.byId("idUtilizacao").getValue(),
                    setor: this.oView.byId("idSetor").getValue(),
                    dataFixInicio: this.oView.byId("idDtFixIni").getValue(),
                    dataFixFim: this.oView.byId("idDtFixFim").getValue()
                }
            };

            this.myModel.setData(oData);

            var oSource = oEvent.getSource();
            var oContext = oSource.getBindingContext("data");//undefined
            var oPath = oSource.getBindingContext("data").getPath(); 

            var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
            oRouter.navTo("third", oContext, false);

        }
    });

});

View.xml代码

    <mvc:View xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" controllerName="umicoreMP.controller.FirstView">
        <Page title="{i18n>title}">
            <content>
                <sap.ui.layout.form:SimpleForm xmlns:sap.ui.layout.form="sap.ui.layout.form" xmlns:sap.ui.core="sap.ui.core" editable="false" layout="ResponsiveGridLayout" id="form1" columnsM="2" labelMinWidth="100" singleContainerFullSize="false" width="100%" columnsL="2">
                    <sap.ui.layout.form:content xmlns:sap.ui.layout.form="sap.ui.layout.form">
                        <sap.ui.core:Title xmlns:sap.ui.core="sap.ui.core" text="Adicionar Item" id="__title1" emphasized="true" icon="sap-icon://sales-order-item" level="H1"/>
                        <Label text="Descrição Material" id="lbMatTxt" busy="false"/>
                        <Input width="80%" id="idMatDesc" maxLength="60" fieldWidth="" valueLiveUpdate="true" value="{/mp/matDesc}" description="{/mp/matDesc}"/>
                        <Label text="Quantidade" width="90%" id="lbQuantidade"/>
                        <Input width="30%" id="idQtd" maxLength="10" value="{/mp/qtd}" description="{/mp/qtd}" valueLiveUpdate = "true"/>
                        <Label text="UM" id="lbUM"/>
                        <Input width="20%" id="idUm" maxLength="3" maxSuggestionWidth="10px" fieldWidth="10%" dateFormat=""  value="{/mp/um}" description="{/mp/um}" valueLiveUpdate = "true"/>
                        <Label text="Utilização" width="90%" id="__label1"/>
                        <Input width="80%" id="idUtilizacao"  value="{/mp/utilizacao}" description="{/mp/utilizacao}" valueLiveUpdate = "true"/>
                        <Label text="Setor" width="90%" id="__label2"/>
                        <Input width="70%" id="idSetor"  value="{/mp/setor}" description="{/mp/setor}" valueLiveUpdate = "true"/>
                        <Label text="Data Fixing Inicio" width="90%" id="__label5"/>
                        <DatePicker width="60%" secondaryCalendarType="Gregorian" id="idDtFixIni"  value="{/mp/dataFixInicio}"/>
                        <Label text="Data Fixing Fim" width="100%" id="__label7"/>
                        <DatePicker width="60%" secondaryCalendarType="Gregorian" id="idDtFixFim"  value="{/mp/dataFixFim}"/>
                        <Label text="Observações" width="100%" id="__label8"/>
                        <TextArea wrapping="None" id="idObs" value="{Obs}"/>
                        <Label text="Label" width="100%" id="__label6"/>
                        <Label text="Label" width="100%" id="__label0"/>
                        <Label text="Label" width="100%" id="__label4"/>
                        <Label text="Label" width="100%" id="__label3"/></sap.ui.layout.form:content>
                </sap.ui.layout.form:SimpleForm>
            </content>
        <footer>
            <Bar id="__bar0">
                <contentRight>
                    <Button text="Proxima etapa" width="150px" id="__button2" icon="sap-icon://arrow-right" type="Emphasized" press="onNextStep"/>
                    <Button text="Adicionar Item" width="150px" id="__button4" icon="sap-icon://add" press="onCreate"/>
                </contentRight>
            </Bar>
        </footer>
        <headerContent>
            <Button width="100px" id="carrinho" icon="sap-icon://cart"/>
        </headerContent>
        </Page>

Component.js是:

     sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/Device",
"umicoreMP/model/models"
    ], function(UIComponent, Device, models) {
"use strict";

return UIComponent.extend("umicoreMP.Component", {

    metadata: {
        manifest: "json"
    },

    /**
     * The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
     * @public
     * @override
     */
    init: function() {
        // call the base component's init function
        UIComponent.prototype.init.apply(this, arguments);

        // set the device model
        this.setModel(models.createDeviceModel(), "device");

        // create the views based on the url/hash
        this.getRouter().initialize();
    }
});

    });

我的路由配置是:

            "routing": {
        "config": {
            "routerClass": "sap.m.routing.Router",
            "viewType": "XML",
            "viewPath": "umicoreMP.view",
            "controlId": "app",
            "controlAggregation": "pages"
        },
        "routes": [{
            "pattern": "",
            "name": "first",
            "target": "first"
        }, {
            "pattern": "secondview",
            "name": "second",
            "target": "second"
        }, {
            "pattern": "thirdview",
            "name": "third",
            "target": "third"

        }],
        "targets": {
            "first": {
                "viewName": "FirstView"
            },
            "second": {
                "viewName": "SecondView"
            },
            "third": {
                "viewName": "ThirdView"
            }
        }

    }

感谢您的建议!

2 个答案:

答案 0 :(得分:0)

var oSource = oEvent.getSource();
var oContext = oSource.getBindingContext("data");//undefined

事件的来源是ID为“__button4”的按钮。按钮本身没有任何绑定,取消您的输入字段(或您在示例中看到的一些列表项),因此无法从中获取任何绑定上下文。

如果要为按钮指定上下文,可以使用自定义数据为其添加绑定。您可以看到一个简短的示例here

但是,我建议只考虑传递oData变量。

答案 1 :(得分:0)

oSource.getBindingContext();

'oSource'会给你一个按钮实例,而且你正在尝试绑定它,显然你没有设置,所以它返回为未定义。

要在视图之间传递数据,请执行以下操作:

 oRouter.navTo("third", oData, false);