0

I have the following angular service:

.factory("OTReady", ['$document', function ($document) {
  var ready = false;
  $document.context.addEventListener('deviceReady', function () {
    ready = true;
  });

  return (function(fn){
    if (ready) {
      fn();
    } else {
      $document.context.addEventListener('deviceReady', fn);
    }
  });
}])

And this is returning "undefined". It's a snippet from an opentok-angular library running in Cordova.

Many other $document properties are available at in this block of code. Why this is returning "undefined" ?

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
lilbiscuit
  • 2,109
  • 6
  • 32
  • 53

2 Answers2

1

I ran into a similar issue where my angular linter did not approve of document.addEventListener and was telling me to use the angular $document service like your initial service. However, simply switching document.addEventListener to $document.addEventListener returned undefined. By printing to the console I figured out $document is an array. Not sure if this is intentional or not but changing $document.addEventListener to $document[0].addEventListener worked for me.

So your new service would look like this:

.factory("OTReady", ['$document', function ($document) {
  var ready = false;
  $document[0].addEventListener('deviceReady', function () {
    ready = true;
  });

  return (function(fn){
    if (ready) {
      fn();
    } else {
      $document[0].addEventListener('deviceReady', fn);
    }
  });
}])

Someone else should shed some light on why this is set up this way. An array doesn't make much sense unless you might have multiple document objects.

0

This solved it for me:

document.addEventListener('deviceReady', fn);

I read that context is being deprecated from jQuery, but I don't think that was the issue.

lilbiscuit
  • 2,109
  • 6
  • 32
  • 53