Skip to Content
0

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

May 04, 2017 at 02:11 PM

226

avatar image
Former Member

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){  
    	 })             
	},
	});
});

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Mahesh kumar palavalli May 08, 2017 at 05:17 AM
1

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

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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.
	            	       	       }   	   
	           })]  
},
0

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)
1
Former Member
Mahesh kumar palavalli

Thank you, solved.

0
avatar image
Former Member May 04, 2017 at 02:40 PM
1

Hi Martin, try to pass the context:

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

Regards

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi thanks for reply.

It works _OnSuccess function.

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

0