Search code examples
javascriptangularjscordovaonsen-ui

CordovaApp - AngularJs - app.initialize() -> undefined is not a function


I've an hybrid for android, made with cordova + onsen + angularJs.

This is the index.html file:

<html>
<head>
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <title>AppModel</title>  

    <!-- <link rel="stylesheet" href="css/plugins.css"/> -->
    <link rel="stylesheet/less" href="css/plugins.less"/>

    <link rel="stylesheet" href="lib/onsen/css/onsenui.css">  
    <link rel="stylesheet" href="lib/onsen/css/onsen-css-components.css">  

    <link rel="stylesheet/less" href="css/app.less"/>
    <link rel="stylesheet/less" href="css/base-layout.less"/>

</head>

<body>    

<ons-sliding-menu
    menu-page="modules/core/menu.html" main-page="modules/account_profile/login.html" side="left"
    var="menu" type="reveal" max-slide-distance="260px" swipable="true" swipe-target-width="50">
</ons-sliding-menu>


<script src="css/less.min.js" type="text/javascript"></script>


<script src="lib/onsen/js/angular/angular.js"></script>   
<script src="lib/onsen/js/angular/angular-touch.js"></script>    
<script src="lib/onsen/js/onsenui.min.js"></script>  

  <script src="http://maps.google.com/maps/api/js?sensor=false"></script>         
  <script src="js/lodash_3.2.0.min.js"></script>  
  <script src="js/angular-google-maps_2.0.12.min.js"></script>
    <script src="js/jquery.min.js"></script>   
<script src="js/plugins.js"></script>   

<script src="js/appStart.js"></script>      
<script src="js/data/data.js"></script>    
<script src="js/angularApp/controllers.js"></script>      
<script src="js/angularApp/directives.js"></script>      
<script src="js/angularApp/filters.js"></script>    
<script type="text/javascript" src="cordova.js"></script>

  <script>
    app.initialize();
   </script>

  </body>
 </html>

And this is the appStart.js file:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'

exitFunction : function(){
     if (navigator.notification.confirm("Vuoi chiudere l'app?", 
            function(index) {
                if (index == 1) { // OK button
                    navigator.app.exitApp(); // Close the app
                }
            },
            "AppModel",
            ["Ok","Annulla"]
        ));
},

onDeviceReady: function() {
    app.receivedEvent('deviceready');

    ons.setDefaultDeviceBackButtonListener(function() {
        if (navigator.notification.confirm("Vuoi chiudere l'app?", 
            function(index) {
                if (index == 1) { // OK button
                    navigator.app.exitApp(); // Close the app
                }
            },
            "AppModel",
            ["Ok","Annulla"]
        ));
    });

    /*
    // Open any external link with InAppBrowser Plugin
    $(document).on('click', 'a[href^=http], a[href^=https]', function(e){

        e.preventDefault();
        var $this = $(this); 
        var target = $this.data('inAppBrowser') || '_blank';

        window.open($this.attr('href'), target);

    });
    */

    $(document).on('click', 'a', function(e){

        e.preventDefault();
        var $this = $(this); 
        //var target = $this.data('inAppBrowser') || '_blank';

        window.open($this.attr('href'), "_system");

    });


},
// Update DOM on a Received Event
receivedEvent: function(id) {
    //var parentElement = document.getElementById(id);
    //var listeningElement = parentElement.querySelector('.listening');
    //var receivedElement = parentElement.querySelector('.received');

    //listeningElement.setAttribute('style', 'display:none;');
    //receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}

};


(function() {

var app = angular.module('AppModel', ['onsen', 'angular-carousel', 'uiGmapgoogle-maps'])    
.config( [
    '$compileProvider',
    function( $compileProvider )
    {   
        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|ftp|mailto|tel|geo):/);            
    }
]);


document.addEventListener('deviceready', function() {
    angular.bootstrap(document, ['AppModel']);       
}, false);    


})();

But the chrome inspect console says, undefined is not a function, referred to the script in index.html

   <script>app.initialize();</script>

What's the problem? Thank you


Solution

  • Found solution by myself:

    i created a new file and called index.js, here is the content

    var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    
    exitFunction : function(){
         if (navigator.notification.confirm("Vuoi chiudere l'app?", 
                function(index) {
                    if (index == 1) { // OK button
                        navigator.app.exitApp(); // Close the app
                    }
                },
                "AppModel",
                ["Ok","Annulla"]
            ));
    },
    
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    
        ons.setDefaultDeviceBackButtonListener(function() {
            if (navigator.notification.confirm("Vuoi chiudere l'app?", 
                function(index) {
                    if (index == 1) { // OK button
                        navigator.app.exitApp(); // Close the app
                    }
                },
                "AppModel",
                ["Ok","Annulla"]
            ));
        });
    
        /*
        // Open any external link with InAppBrowser Plugin
        $(document).on('click', 'a[href^=http], a[href^=https]', function(e){
    
            e.preventDefault();
            var $this = $(this); 
            var target = $this.data('inAppBrowser') || '_blank';
    
            window.open($this.attr('href'), target);
    
        });
        */
    
        $(document).on('click', 'a', function(e){
    
            e.preventDefault();
            var $this = $(this); 
            //var target = $this.data('inAppBrowser') || '_blank';
    
            window.open($this.attr('href'), "_system");
    
        });
    
    
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        //var parentElement = document.getElementById(id);
        //var listeningElement = parentElement.querySelector('.listening');
        //var receivedElement = parentElement.querySelector('.received');
    
        //listeningElement.setAttribute('style', 'display:none;');
        //receivedElement.setAttribute('style', 'display:block;');
    
        console.log('Received Event: ' + id);
    }
    
     };
    
    
      app.initialize(); //moved here this instruction
    

    and then in the index.html body, as the last imported script:

            <script type="text/javascript" src="js/index.js"></script>  
    

    And everything works.