You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Don Coleman <do...@gmail.com> on 2012/07/06 01:12:24 UTC

requiring javascript for plugins

Is there a new recommended way to include javascript for plugins?

Previously I just jammed an object onto navigator or a global variable

    navigator.toast = new Toasty();

https://github.com/m00sey/Toasty/blob/f9dbf58e11cf4ecf386f70331960d33e87566dd9/assets/www/phonegap-toast.js

I've rewritten this plugin's javascript as a module and I'm
recommending that the user requires the module in their code with

    var toast = cordova.require('toast');

https://github.com/don/Toasty/blob/bf61ff3c738cebcd285f82490920cf713ac095aa/assets/www/phonegap-toast.js

Is this the recommended way?

It works fine, it's just an extra step for the user, and different
from using a builtin cordova function like
navigator.notification.vibrate.

Should I still assign the plugin code to navigator or cordova?
Is there a way to have my plugin js required by plugin.xml,
cordova.addPlugin or some other function???
Are users to need to require built-in cordova for 2.0+?

Re: requiring javascript for plugins

Posted by Andrew Lunny <al...@gmail.com>.
+1 to everything Fil says.

On 6 July 2012 07:32, Filip Maj <fi...@adobe.com> wrote:

> I have been thinking about all of these questions for a while, Don.
>
> In my opinion, I do not think that Cordova should mandate a specific way
> in which the JavaScript side of a plugin should be written or laid out.
> The only requirement really boils down to using the `exec` method.
>
> That said, the core APIs, which are just plugins, are wrapped in a
> cordova.define call and use cordova.require to get references to any other
> internal Cordova javascript bits, so I would _recommend_ users use this
> pattern (exactly how your second phonegap-toast.js link does, Don).
>
> It is cleaner as it does not trample over any globals.
>
> As for where to assign the plugin code, again, I think it is up to the
> plugin. I do not think it matters if it is assigned to navigator or to
> window.plugins. Whatever works for the user. However, assigning to the
> `cordova` object is not recommended as it may trample over
> variables/properties that the cordova framework needs to function.
>
> Finally, assigning to plugins.xml. No way to do it programmatically / at
> run-time at this time, and I do not think we will do that in the future
> (security considerations: you want to vet out which native bits will be
> run before build time).
>
> On 7/5/12 7:12 PM, "Don Coleman" <do...@gmail.com> wrote:
>
> >Is there a new recommended way to include javascript for plugins?
> >
> >Previously I just jammed an object onto navigator or a global variable
> >
> >    navigator.toast = new Toasty();
> >
> >
> https://github.com/m00sey/Toasty/blob/f9dbf58e11cf4ecf386f70331960d33e8756
> >6dd9/assets/www/phonegap-toast.js
> >
> >I've rewritten this plugin's javascript as a module and I'm
> >recommending that the user requires the module in their code with
> >
> >    var toast = cordova.require('toast');
> >
> >
> https://github.com/don/Toasty/blob/bf61ff3c738cebcd285f82490920cf713ac095a
> >a/assets/www/phonegap-toast.js
> >
> >Is this the recommended way?
> >
> >It works fine, it's just an extra step for the user, and different
> >from using a builtin cordova function like
> >navigator.notification.vibrate.
> >
> >Should I still assign the plugin code to navigator or cordova?
> >Is there a way to have my plugin js required by plugin.xml,
> >cordova.addPlugin or some other function???
> >Are users to need to require built-in cordova for 2.0+?
>
>

Re: requiring javascript for plugins

Posted by Filip Maj <fi...@adobe.com>.
I have been thinking about all of these questions for a while, Don.

In my opinion, I do not think that Cordova should mandate a specific way
in which the JavaScript side of a plugin should be written or laid out.
The only requirement really boils down to using the `exec` method.

That said, the core APIs, which are just plugins, are wrapped in a
cordova.define call and use cordova.require to get references to any other
internal Cordova javascript bits, so I would _recommend_ users use this
pattern (exactly how your second phonegap-toast.js link does, Don).

It is cleaner as it does not trample over any globals.

As for where to assign the plugin code, again, I think it is up to the
plugin. I do not think it matters if it is assigned to navigator or to
window.plugins. Whatever works for the user. However, assigning to the
`cordova` object is not recommended as it may trample over
variables/properties that the cordova framework needs to function.

Finally, assigning to plugins.xml. No way to do it programmatically / at
run-time at this time, and I do not think we will do that in the future
(security considerations: you want to vet out which native bits will be
run before build time).

On 7/5/12 7:12 PM, "Don Coleman" <do...@gmail.com> wrote:

>Is there a new recommended way to include javascript for plugins?
>
>Previously I just jammed an object onto navigator or a global variable
>
>    navigator.toast = new Toasty();
>
>https://github.com/m00sey/Toasty/blob/f9dbf58e11cf4ecf386f70331960d33e8756
>6dd9/assets/www/phonegap-toast.js
>
>I've rewritten this plugin's javascript as a module and I'm
>recommending that the user requires the module in their code with
>
>    var toast = cordova.require('toast');
>
>https://github.com/don/Toasty/blob/bf61ff3c738cebcd285f82490920cf713ac095a
>a/assets/www/phonegap-toast.js
>
>Is this the recommended way?
>
>It works fine, it's just an extra step for the user, and different
>from using a builtin cordova function like
>navigator.notification.vibrate.
>
>Should I still assign the plugin code to navigator or cordova?
>Is there a way to have my plugin js required by plugin.xml,
>cordova.addPlugin or some other function???
>Are users to need to require built-in cordova for 2.0+?