0
votes

I'm new to sapui5 world and i'm stuck at the beginning and asking for your help i created the below OData service that contains two entities enter image description here

Then i created the app i started with the first view and the controller then in it i wrote the following code

onLogin: function(){
		var sURI = "proxy/http/localhost:9999/MIKMOWCFDataService.svc/";
		var oModel = new sap.ui.model.odata.ODataModel(sURI, false);
		oModel.oHeaders = {
			"DataServiceVersion": "2.0",
			"MaxDataServiceVersion": "2.0"
		};
		var aFilter = [];		
		aFilter.push(new sap.ui.model.Filter("VendorEmail", sap.ui.model.FilterOperator.EQ, this.getView().byId("txtUserName").getValue()));
		aFilter.push(new sap.ui.model.Filter("VendorPassword", sap.ui.model.FilterOperator.EQ,this.getView().byId("txtPassword").getValue() ));
		oModel.read("/tbl_Vendors", {
			filters : aFilter
		});
		sap.ui.getCore().setModel(oModel,"vendors");		
	}

What i'm trying to achieve from the code is to get the user id after he submits his email and password i managed to return the correct row from the DB but after that i was not able to read the ID property from the returned data it's only done through used list and press on the list item to fire another event and then read the ID which is not reasonable in the giving example can any one help me on how to read the property directly from the model Thanks in advance

1

1 Answers

0
votes

You need to put the Id into global variable, then binding variable into entity key.

    onInit: function() {
        var oEventBus = sap.ui.getCore().getEventBus();
        sap.ui.core.UIComponent.getRouterFor(this).attachRouteMatched(this.onRouteMatched, this);
    },

    //routing validation
    onRouteMatched: function(oEvent) {
        var oParameters = oEvent.getParameters();
        // when detail navigation occurs, update the binding context
        if (oParameters.name !== "CreateDoc") {

            return;
        }

        console.log(oParameters.name);
        //put your entity name here
        var sEntityPath = "/MasterSet(Id='"+ yourGlobalVariable +"')";
        this.bindView(sEntityPath);

    },

    //binding initial data to sapui5 page
    bindView: function(sEntityPath) {
        var oView = this.getView();
        oView.bindElement(sEntityPath);

        // check if the data already on the client
        if (!oView.getModel().getData(sEntityPath)) {
            // check that the entity specified actually was found
            oView.getElementBinding().attachEventOnce("dataReceived", jQuery.proxy(function() {
                var oData = oView.getModel().getData(sEntityPath);
                console.log(oData);
                if (!oData) {
                    // this.showEmptyView();
                } else {
                    // 
                }
            }, this));
        }

    },

Try to use console.log in every step to understand the flow.