cancel
Showing results for 
Search instead for 
Did you mean: 

How to put navigation function inside odata model read nested success function ?

Former Member
0 Kudos

Hello experts, I successfully implemented navigation between views inside my shell ui5 application. But problem is that I don't know how implement navigation inside my odata model read success function. My function onRedirect works, but I want to implement this within function _OnSuccess and inside function press for button.

Here is my code snippet from controller:

jQuery.sap.require( 'jquery.sap.resources' );  
sap.ui.define([
	"sap/ui/core/mvc/Controller",
		
], function (Controller) {
	"use strict";
 
	return Controller.extend("sap.ui.iyc.routepages.First", {
	        getRouter : function () {
			return sap.ui.core.UIComponent.getRouterFor(this);
		},
		
		onRedirect : function () {
		this.getRouter().navTo("second");
		},
		
	    onSearch: function(){
						
			 var oModel = new sap.ui.model.odata.ODataModel("http://<server>/sap/opu/odata/sap/ZBAPI_ISUFINDER2_SRV/");
		     var oODataJSONModel =  new sap.ui.model.json.JSONModel();
			 var oView = this.getView();
			 var oTable = this.getView().byId("idTable");
			 var oObjecttype = this.getView().byId("inp").getValue();
			
			     
    oModel.read("/BusPartnerSet?$filter=Objecttype eq '",
    		      null,
    		      null,
	              false,
       
	              function _OnSuccess(oData, oResponse){
	              // create JSON model
	              var oODataJSONModel =  new sap.ui.model.json.JSONModel();
	              // set the odata JSON as data of JSON model
	              oODataJSONModel.setData(oData);
	              
	              // store the model
	              oTable.setModel(oODataJSONModel);
	              var oTemplate = new sap.m.ColumnListItem(
cells: [new sap.m.Text({text : '{Buspartner}'}), 
       new sap.m.Button({text : "Display details", type : "Emphasized",
       press: function(e){var obj = e.getSource().getBindingContext().getObject().Buspartner;
	var oGlobalJSON = new sap.ui.model.json.JSONModel();
	oGlobalJSON.setProperty('/Buspartner', obj);
	sap.ui.getCore().setModel(oGlobalJSON, "BuspartnerNumber"); 
        // This is where I want to place navigation
	}})        		        
	]  
	 });   
     	}})]  
	});
	         
 oTable.bindAggregation("items", { path: "/results", template : oTemplate});
	},
     	function _OnError(oError){  
    	 })             
	},
	});
});

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

I am guessing that you are not able to access the "this" parameter of controller, for access the this.router nav to functionality of component.

so do the below changes.

1. change function _OnSuccess(oData, oResponse){ }.bind(this) - Add .bind(this) at the end of the onSucecss function as it has to access the controller onRedirect function.

2. inside the _onSuccess, call this.onRedirect() function.

Best Regards,
Mahesh

Former Member
0 Kudos

Hi Mahesh, thank you for reply, but this is the same answer like before. It works within _OnSuccess function but inside this function there is pattern with table where is button and there I wish to implement this navigation. In this source code there is a comment "// This is where I want to impelent navigation."

function _OnSuccess(oData, oResponse){
var oTemplate = new sap.m.ColumnListItem(  
	            		   {cells: [     		                     		  	       new sap.m.Button({text : "Display details", type : "Emphasized",
	       press: function(e){
	           var obj = e.getSource().getBindingContext().getObject().Buspartner;
	           var oGlobalJSON = new sap.ui.model.json.JSONModel();
	           oGlobalJSON.setProperty('/Buspartner', obj);
	           sap.ui.getCore().setModel(oGlobalJSON, "BuspartnerNumber");
	           oGlobalJSON.setDefaultBindingMode("TwoWay");
	           // This is where I want to impelent navigation.
	            	       	       }   	   
	           })]  
},
maheshpalavalli
Active Contributor

oh ok.. bind this to success and now change the press event handler to this:

 press: jQuery.proxy(function(e){
	          // Code
	           // This is where I want to do Navigation	

                         this.onRedirect();
	           }, this)
Former Member
0 Kudos

Thank you, solved.

Answers (1)

Answers (1)

PMarti
Active Participant

Hi Martin, try to pass the context:

function _OnSuccess() { this.onRedirect() }.bind(this)

Regards

Former Member
0 Kudos

Hi thanks for reply.

It works _OnSuccess function.

But I wish to implement this inside press function within my _OnSuccess function.