cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to invoke hook extension in controller extension: UI5 Adaptation project

SumitKundu
Active Participant
3,683

I am trying to implement a hook extension method in an adaptation project.

Application: Adjust Stock

Extension hook: ProductShrink.controller.js extHookValidUnitOfMeasures

Tried adding the method in 'Override' section of the generated controller extension file using SAP Visual Editor in an adaptation project but the hook method is not triggering.

/***
@controller Name:retail.store.stockcorrections1.controller.ProductShrink,
*@viewId:__component0---toShrink
*/
/*!
 * OpenUI5
 * (c) Copyright 2009-2020 SAP SE or an SAP affiliate company.
 * Licensed under the Apache License, Version 2.0 - see LICENSE.txt.
 */


sap.ui.define([
    'sap/ui/core/mvc/ControllerExtension'
    , 'sap/ui/core/mvc/OverrideExecution'
],
    function (
        ControllerExtension
        , OverrideExecution
    ) {
        "use strict";
        return ControllerExtension.extend("zadjuststock.ShrinkExt", {
            metadata: {
                // // 	// extension can declare the public methods
                // // 	// in general methods that start with "_" are private
                methods: {
                    // 		publicMethod: {
                    // 			public: true /*default*/ ,
                    // 			final: false /*default*/ ,
                    // 			overrideExecution: OverrideExecution.Instead /*default*/
                    // 		},
                    extHookValidUnitOfMeasures: {
                        public: true /*default*/,
                        final: false /*default*/,
                        overrideExecution: OverrideExecution.After /*default*/
                    }
                    // 		finalPublicMethod: {
                    // 			final: true
                    // 		},
                    // 		onMyHook: {
                    // 			public: true /*default*/ ,
                    // 			final: false /*default*/ ,
                    // 			overrideExecution: OverrideExecution.After
                    // 		},
                    // 		couldBePrivate: {
                    // 			public: false
                    // 		}
                    // 	}
                }
            },


            // // adding a private method, only accessible from this controller extension
            // _privateMethod: function() {},
            // // adding a public method, might be called from or overridden by other controller extensions as well
            // publicMethod: function() {},
            // // adding final public method, might be called from, but not overridden by other controller extensions as well
            // finalPublicMethod: function() {},
            // // adding a hook method, might be called by or overridden from other controller extensions
            // // override these method does not replace the implementation, but executes after the original method
            // onMyHook: function() {},
            // // method public per default, but made private via metadata
            // couldBePrivate: function() {},
            // // this section allows to extend lifecycle hooks or override public methods of the base controller


            extHookValidUnitOfMeasures: function () {
                debugger
                return a = [1, 2]
            }


            // override: {
            // 	/**
            // 	 * Called when a controller is instantiated and its View controls (if available) are already created.
            // 	 * Can be used to modify the View before it is displayed, to bind event handlers and do other one-time initialization.
            // 	 * @memberOf zadjuststock.ShrinkExt
            // 	 */
            // 	onInit: function() {
            // 	},


            // 	/**
            // 	 * Similar to onAfterRendering, but this hook is invoked before the controller's View is re-rendered
            // 	 * (NOT before the first rendering! onInit() is used for that one!).
            // 	 * @memberOf zadjuststock.ShrinkExt
            // 	 */
            // 	onBeforeRendering: function() {
            // 	},


            // 	/**
            // 	 * Called when the View has been rendered (so its HTML is part of the document). Post-rendering manipulations of the HTML could be done here.
            // 	 * This hook is the same one that SAPUI5 controls get after being rendered.
            // 	 * @memberOf zadjuststock.ShrinkExt
            // 	 */
            // 	onAfterRendering: function() {
            // 	},


            // 	/**
            // 	 * Called when the Controller is destroyed. Use this one to free resources and finalize activities.
            // 	 * @memberOf zadjuststock.ShrinkExt
            // 	 */
            // 	onExit: function() {
            // 	},


            // 	// override public method of the base controller
            // 	basePublicMethod: function() {
            // 	}


            // extension: {
            //     "zadjuststock.ShrinkExt": {
            //         extHookValidUnitOfMeasures: function () {
            //             debugger
            //             return a = [1, 2]
            //         }
            //     }
            // }
            // }
        });
    });

Also tried adding the method in metadata as a public method but the method is not recognized either.

What is the recommended way of implementing a controller extension hook method in an (flexibility based) adaptation project?

Should I use below?

this.base.extHookValidUnitOfMeasures = function () {

..
}

Best regards,

Sumit

Accepted Solutions (0)

Answers (4)

Answers (4)

meriton
Explorer
0 Likes

Hey Severin


Inside WebIde you already have what you need.

1. Inside webide

2. right click workspace

3. New -> Extension project

4. Find the application you want to extend.

5. Add the project to your workspace.

6. Right click your "ExtendedProject" -> press New -> Extension

7. Select what kind of extension you are looking for, > Hook, view, fragemnt, replace service, i18n and so on.

8 Everything will be generated for you.

SumitKundu
Active Participant
0 Likes

I am using Business Application Studio(BAS) and an adaptation project (i.e. flexibility-based extension approach) and not the old component configuration-based extension approach.

Best regards,

Sumit

sdebeaulieu
Participant
0 Likes

I'm asking the same question, which is pretty clear. Links in comments don't help.

Any new tips?

boghyon
Product and Topic Expert
Product and Topic Expert
0 Likes
junwu
SAP Champion
SAP Champion
0 Likes

https://sapui5.hana.ondemand.com/sdk/#/topic/b0b14bf4dcdb476fb0d63877c1beff7c

have you tried traditional way to extend controller?

RaminS
Active Participant
Just to clarify, in BAS we can only extend the traditional way if the app does not meet the Project Adaptation prerequisites. If it does, then we have no choice but to use the new Adaptation Project, which provide very limited extensibility to the controller functions and local models.