0

I have a RequireJS module I am writing, that I am using to wrap the ASYNC methods used by LG's webOS SCAP library (this is a Cordova based library that allows easy interaction with webOS, on the "Signage" screens).

The SCAP methods rely on success and failure callbacks.

In one of my success callbacks, I need to call another method in the same module.

I tried using "var self = this" and then calling "self.methodName" but it is always undefined. If I debug, the "self" reference seems to be correctly populated, showing all the "window" data. But the method is undefined.

I have been able to call methods within the same module, in other modules, and it works. but this is the first time I am trying to call it from within a nested function.

Code:

define(["settings"], function(settings) {    
    return {
        updateProgress: function() {
            var self = this;

            // this is called when the statFile function succesfully finds the file and can read its properties
            var successCb = function(cbObject) {
                self.getDownloadSpeed(); // "self" is defined, getDownloadSpeed isn't.
            };

            var failureCb = function(cbObject) {
                // stuff here
            };

            var options = {
                // stuff here
            };

            settings.storage.statFile(successCb, failureCb, options); // SCAP method used to get file info (size, date modified etc)
        },

        getDownloadSpeed: function() {
            // stuff here
        }
    };
});
Mr Pablo
  • 4,109
  • 8
  • 51
  • 104
  • I've copied and pasted your code into a module, replaced the call to `settings.storage.stateFile` with a `setTimeout` that calls `successCb` and then required the module and called `updateProgress`. `getDownloadSpeed` was called properly. So the issue is not in the code you show. What calls `updateProgress`? – Louis Oct 29 '15 at 10:43
  • A setInterval (fired from a different module) calls updateProgress every second. I've noticed that var self = this, should NOT reference the window object. but it does in this module. in other modules, it correctly references the module. – Mr Pablo Oct 29 '15 at 10:49
  • Your question is a duplicate of [this one](http://stackoverflow.com/q/2749244/1906307) then. I've already voted to close your question for lack of information so I cannot vote to close it at duplicate. If you have a way to close it as duplicate, please do. Or delete it. Or if there is still a problem, you should edit your question to highlight the problem that remains. – Louis Oct 29 '15 at 11:04

0 Answers0