javascriptandroidangularjscordovaonsen-ui

Is possible to call closeMenu method of ons-sliding-menu component from back button event?


I want to close ons-sliding-menu when the user presses android's back button. How can I achieve this? Current implementation is:

    var manager = {
        initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function(){
            document.addEventListener("backbutton", manager.onBackKeyDown, false);
            document.addEventListener("menubutton", manager.onMenuKeyDown, false);
        },
        onBackKeyDown: function(){
            // close ons-sliding-menu if opened
        },
        onMenuKeyDown: function(){
            // close or open ons-sliding-menu
        }
    };
    
    manager.initialize();

I've tried to call ons.slidingMenu.toggleMenu(), however ons is undefined when onBackKeyDown event triggers.


Solution

  • the OnsenuUi 1.1.x has the component ons-back-button