avatar image
Former Member

Referencing custom control methods directly in a SAPUI5 XML view

Hello,

I am extending a control (sap.m.SearchField) which has a built in event "search". If you want to launch a specific method of a controller, you simply put the method name as the "search" attributes value.

The control is gonna appear in various places of the app. I don't want to refer to a method from every views controller, every time the search event launches, depending on the view I'm actually on. Instead, I would like to call a (private/internal) custom method of the control itself. How to achieve this? Is it possible to reference the control itself (and therefore its methods) in a XML view? How to "self reference" a control within the XML view?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    avatar image
    Former Member
    Mar 01, 2017 at 09:55 AM

    To always call your custom control's function on its search event, you can attach the function to the search event in that control's init function. Like so:

    sap.ui.define([
    	"sap/m/SearchField",
    	"sap/m/SearchFieldRenderer"
    ], function(SearchField, SearchFieldRenderer) {
    	jQuery.sap.declare("custom.control.SearchField");
    	"use strict";
    
    	return SearchField.extend("custom.control.SearchField", {
    		init: function() {
    			var oControl = this;
    			SearchField.prototype.init.apply(oControl, arguments);
    			oControl.attachSearch("search", oControl._someFunction);
    		},
    
    		_someFunction: function(){
    		},
    
    		renderer: {
    			render: function(oRM, oControl) {
    				SearchFieldRenderer.render(oRM, oControl);
    			}
    		},
    	});
    });
    
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      I have also another idea (but still stuck with the solution):

      _someFunction: function(oEvent){
          // do something with oEvent, to get the view/controller?
      },
Skip to Content