0

JavaScript (phonegap): How can I pass variable between methods within the constructor function?

var myCodesDb = { 
  myCodes: null,
  //...
  loadMyCodes1: function (){
    db=window.openDatabase("Database","1.0","myCodes",200000);
    var query = 'SELECT * FROM my_table';
    db.transaction(function(tx) {tx.executeSql(query),[]}, 
                   this.errorDbCallBack, 
                   function(tx, results) { this.myCodes = results.rows });
  },
  loadMyCodes2: function (){
    db=window.openDatabase("Database","1.0","myCodes",200000);
    var query = 'SELECT * FROM my_table';
    db.transaction(function(tx) {tx.executeSql(query),[]}, 
                   this.errorDbCallBack, 
                   this.loadMyCodesSuccess);
},
loadMyCodesSuccess: function (tx, results){
    this.myCodes = results.rows;
},

};

Unfortunately in both methods (loadMyCodes1 and loadMyCodesSuccess) is value of results undefined (and myCode of course).

Fenix
  • 2,271
  • 1
  • 16
  • 24
  • 1
    Have a look at [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/q/20279484/218196) – Felix Kling Feb 27 '15 at 16:53
  • Thanks but I guess that the main problem is not with using `this` because in function `loadMyCodes1` there is result undefined even before assignment to `myCodes`. Maybe I am wrong. I am going to look at the link which you posted. – Fenix Mar 01 '15 at 19:21

1 Answers1

0

I am not sure what exactly you want to do at the end but you have a class with number of methods. You can use the following:

this.methodName.bind(this, x, y, z and so on...) 
KQI
  • 322
  • 1
  • 16
  • At the end I would like to set `results.rows` to property `myCodes`. Both methods `loadMyCodes1` and `loadMyCodesSuccess` (called from `loadMyCodes2`) should do the same work but `results` is undefined when I call `this.myCodes = results.rows`. – Fenix Mar 01 '15 at 19:17
  • 1
    Ok. Remember Always javascript functions have different 'this' so a different object, so your first method (or a function) 'loadMyCode1' you can use 'this' to refer to other sibling methods 'loadMyCode2' and 'loadMyCodeSuccess'. If you nested a function inside your method, 'this' will no longer refer to other siblings, you will again have to use 'bind' and force this to be used inside. Another (better and faster) way is to define 'that = this;' outside the nested fn and then use 'that' as an object to refer to your class without using 'bind' – KQI Mar 01 '15 at 20:55
  • Quick example: loadMyCodes1: function (){ var that = this; ... ... db.transaction(function(tx) {tx.executeSql(query),[]}, this.errorDbCallBack, function(tx, results) { that.myCodes = results.rows }); }, see how exactly I am using `that` and where it's defined and called. I hope this helps. – KQI Mar 01 '15 at 20:57
  • It was another problem I had in the code and it can not be related to the fact that `results` is undefined (I found that used SQLite db.transaction in wrong way). Title of my question was edited and now my previous problem is quite off topic. But thank you anyway it helped me to understand better how `this` works in js. +1 – Fenix Mar 03 '15 at 12:28