cancel
Showing results for 
Search instead for 
Did you mean: 

I want to get all methods and function from my js view to my Controller.js

Former Member
0 Kudos

Hello I have JS View and One controller. I want to import all the data from js view into controller and store it in variable. How would I do that. I tried using

1.var oView = sap.ui.jsview('view.main') in controller but didnt work. can some one guide me. By doing this i want to write a function on Button press (in a view) which can function definition in controller and displays alert message. Below is my code.

2. what is use of oninit, onAfterrndering(), onBeforeRendering() etc. I didnt get it.

Any help/guidance will be really appreciated. Thanks 🙂

view.js file

sap.ui.jsview('view.main',{
    
    getControllerName: function(){
        return "view.main";
    },
    
    createContent: function(oController){
        var text = new sap.m.Text({
            text:"Hello SAP, We met finally!!",
        });
        
        var button = new sap.m.Button({
            text:"Press Me",
            press: function(){
               alert("Hello, Are you SAP"); 
            }
        });
        
        return new sap.m.Page({
            // title:"{/name}",
            content:[text, button]
        });
        
    }
    
});
------------------------------------
controller.js file
sap.ui.controller('view.main',{
  // var oView = sap.ui.jsview('view.main');
    onInit: function() {
        
	},
	
// 	onBeforeRendering: function() {


// 	},
	
// 	onAfterRendering: function() {


// 	},
	
// 		onExit: function() {	}
    
});
----------------------
Script.js file
sap.ui.getCore().attachInit(function(){
  
  sap.ui.localResources("view");
  
  var app = new sap.m.App({
		initialPage : "idViewDashboard"
	});
  
 var view = sap.ui.view({
		id : "idViewDashboard",
		viewName:"view.main",
		type : sap.ui.core.mvc.ViewType.JS
	});
	
	app.addPage(view);
	app.placeAt("content");


});

Accepted Solutions (0)

Answers (1)

Answers (1)

SergioG_TX
Active Contributor
0 Kudos

I think it would be very helpful if you read this tutorial

https://sapui5.hana.ondemand.com/#docs/guide/3da5f4be63264db99f2e5b04c5e853db.html

the main idea is not to get an answer right away but you should try to understand how the framework works (MVC, data models in your application, how does the view/controller work, etc)