Use object between JavaScript functions - PhoneGap

1

I'm creating an object with onDeviceReady to create the connection to the application's SQLite database. It is being created as follows:

var app = {
    initialize: function() {
        this.bindEvents();
        this.db = null;
    },
    ...
    onDeviceReady: function() {
        app.db = window.sqlitePlugin.openDatabase({name:'4routes'});
        ...

However, there is a method called insertDailyServices which is responsible for storing the information in the database and it is returning error.

insertDailyServices: function(service) {
    app.db.transaction(function(tx){
        tx.executeSql('INSERT INTO file_servico (id_servico,id_os,id_fornecedor,id_motorista,id_veiculo,origem,destino,inicio,termino,created_at) VALUES (?,?,?,?,?,?,?,?,?,?)', 
        [service.id_servico,service.id_file,service.id_fornecedor,service.id_motorista,service.id_veiculo,service.origem,service.destino,service.inicio,service.termino,moment().format('YYYY-MM-DD HH:mm:ss')],
        function(tx,res){
            $('#listServicos').append('Serviço de N&deg; '+service.id_servico+' registrado localmente.<br>');
        });
    });
}

Error Displayed

08-31 01:54:03.100: E/Web Console(31269): Uncaught TypeError: Cannot call method 'transaction' of undefined at file:///android_asset/www/js/index.js:136

Call in file:

<script type="text/javascript">
    app.initialize();
    app.listOpenServices();
</script>

How to solve this problem ??? I just need to pass app.db to be read from any method within the file.

    
asked by anonymous 31.08.2014 / 06:59

1 answer

1

The initialize causes app.db to be null. You could do this in initialize :

this.db = window.sqlitePlugin.openDatabase({name:'4routes'});

It seems that with this SQLitePlugin you need to call openDatabase within onDeviceReady , if you call it this way:

<script type="text/javascript">
    app.initialize();
    app.listOpenServices();
</script>

It can be a problem. Remove this from the HTML and leave it as follows in the javascript:

var app = {
    initialize: function() {
        this.bindEvents();
        this.db = window.sqlitePlugin.openDatabase({name:'4routes'});;
    },
    ...
    onDeviceReady: function() {
        this.initialize();
        this.listOpenServices();
        ...
    
02.09.2014 / 03:44