Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(printer): add printer plugin #225

Merged
merged 2 commits into from
Jun 15, 2016
Merged

Conversation

zakton5
Copy link
Contributor

@zakton5 zakton5 commented Jun 14, 2016

Added wrapper for the cordova-plugin-printer.

Here is the git repo: https://github.com/katzer/cordova-plugin-printer.git

@@ -122,6 +124,7 @@ export {
Insomnia,
Keyboard,
Network,
Printer,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line as it's not needed. export * takes care of this

@zakton5
Copy link
Contributor Author

zakton5 commented Jun 15, 2016

In summary, both functions should just contain return; and have parameters for callbacks? Should the the return signatures for both functions remain as Promises?

@ihadeed
Copy link
Collaborator

ihadeed commented Jun 15, 2016

TL;DR: No extra parameters are needed, just replace the inner functionality with return;


Explaination:
The @Cordova() decorator takes care of the whole process. The default behavior for the decorator is to return a promise. The resolve and reject will be added as arguments to your function, all the way at the end, after every argument you have there.

So for example, let's assume we have a plugin that has the following method:

plugins.myPlugin.myMethod = function(optionA, optionB, successCallback, errorCallback) {
  // do stuff here
  try {
    successCallback('return useful info');
  } catch (e) {
    errorCallback('return error');
  }
};

To wrap this up with ionic-native, we would write the following, and ionic-native will do the magic:

@Plugin({
  plugin: 'cordova-my-plugin',
  pluginRef: 'plugins.myPlugin',
  repo: 'https://my.github/myuser/myrepo'
})
export class MyPlugin {

  /**
  * Some useful docs here!
  */
  @Cordova()
  static myMethod(nameForOptionA: string, nameForOptionB: boolean): Promise<any> {return; }

}

Now if we call that method from our app. It will do the following:

  1. Check that plugins.myPlugin exists, if it doesn't, that means the plugin isn't installed.
  2. Creates a Promise (since it's the default behavior and we didn't pass config)
  3. Adds the resolve and reject variables from the promise, to the original plugin's function
  4. It calls the original method, and then the resolve or the reject will be triggered and it will transmit the data back to your app

You can configure the decorator to behave differently in case the original plugin is written in a different format.

So basically, when writing a wrapper, you don't need to add any functionality. That is because the decorators we use overwrites your method functionality. There are special cases, like defining a wrapper for a property, or defining an instance based plugin.

Thank you for contributing 😁

@zakton5
Copy link
Contributor Author

zakton5 commented Jun 15, 2016

Great explanation! Thank you for all of that info, it is really appreciated. I'll push a new commit in a couple of minutes

@zakton5
Copy link
Contributor Author

zakton5 commented Jun 15, 2016

There we go. Please let me know if I still missed something

@ihadeed
Copy link
Collaborator

ihadeed commented Jun 15, 2016

Looks good now. Thanks @zakton5 !

@ihadeed ihadeed merged commit 48ffcae into danielsogl:master Jun 15, 2016
@zakton5 zakton5 deleted the printer-plugin branch June 15, 2016 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants