Wrapping Plugin for ngCordova


#1

Hi everyone! I’m in the process of wrapping the phonegap-nfc Cordova plugin for Ionic/Angular. I’m trying to wrap it in a promise-based interface, and have the gist of the idea. However, many of these functions are of the form: function(callback, success, error). How would you promisify this to be in Angular/Ionic/ngCordova standards? I’ve been looking through the currently done plugins but haven’t found anything.


#2

Check out this plugin example

var q = $q.defer();

In this example, you can see how the callbacks for errors and success and handled with promises


#3

Thanks for the response! I should have been a bit more specific. Here’s an example of the code I’m wrapping:

Currently, I’m doing

addTagDiscoveredListener: function(callback) {
  var d = $q.defer();
  $window.nfc.addTagDiscoveredListener(callback,
    function(message) {
      d.resolve(message);
    },
    function(err) {
      d.reject(err);
    }
  );
  return d.promise;
}

I’m pretty new to Angular, and attaching an event listener to the document without going through Angular seems like a bad way to go about things. Is there some better method?


#4
$window.addEventListener
$document.addEventListener

Either of these should work, plus it will be handled though angular

https://docs.angularjs.org/api/ng/service/$document

https://docs.angularjs.org/api/ng/service/$window


#5

Awesome. Thanks so much!