Code structure of NW.js apps

621 views
Skip to first unread message

aidan....@glassechidna.com.au

unread,
Feb 24, 2015, 3:54:33 AM2/24/15
to nwjs-g...@googlegroups.com
Hi all,

I really appreciate what you've done here. I was originally a native Mac app developer, but decided to use NW.js for my latest project and it has been a breeze to get up and running very quickly. However, I have a question about organising code in a NW.js project.

How do you organise your "classes" / modules? I know that in a normal IO.js project, you can require() files and so on, but I'm having a bit of trouble with that in NW.js and thought I may be barking up the wrong tree. 

I have seen that Popcorn Time does this: https://git.popcorntime.io/popcorntime/desktop/blob/master/src/app/index.html. It seems to have a file per "class" and include every JS file by using <script> tags in index.html. Is this the best practice in NW.js? How should classes in different files refer to each other? Just by name and assume that the classes are defined because they were all included in index.html?

Also, is the Popcorn Time approach "polluting" the global namespace by doing things like line 13 of database.js? https://git.popcorntime.io/popcorntime/desktop/blob/master/src/app/database.js. My limited understanding of Javascript is that a better option might be (roughly):

var Database = (function() {
  var startupTime = ...; // this is private

  var cons = function() {
    // constructor stuff
  };

  return cons;
})();

Is this better or do I have a fundamental misunderstanding of Javascript?

Thanks for any light you can shed to a JS newbie :)

Regards,
Aidan

VoidVolker

unread,
Feb 25, 2015, 2:17:56 AM2/25/15
to nwjs-g...@googlegroups.com, aidan....@glassechidna.com.au

Yes, this is normal use. One file - one module/submodule/class/fabric. It very simple and useful. If need compatibility with other browsers - use standard script tag. If need compatibility with nodejs/iojs - use require. I think, for simple js code need use standard script tag and for code for nodejs/iojs is use standard node modules for require.

jonatha...@gmail.com

unread,
Apr 19, 2015, 5:09:48 AM4/19/15
to nwjs-g...@googlegroups.com
IMO the guideline that one should not "pollute" the global namespace is more important for people developing javascript which will be shared or relied upon by others, in the browser.  It is a best practice when you're distributing javascript.

When you're distributing a packaged application, on the other hand, as you do with nwjs, then I think this rule is less important and you can choose to break it.

pavel...@gmail.com

unread,
Apr 20, 2015, 4:31:52 AM4/20/15
to nwjs-g...@googlegroups.com
I like to use global variable in JavaScript, because if use node's require for everything it will be hard to communicate between that classes.

I have different namespaces for different parts of project, e.g. classes to work with database inside object Model, interface related things inside object View. I use some small library to create classes easier and sometimes use Object.setPrototypeOf to assign parent constructor.

Small class looks like this:

global.Dialog.ImportFile = global.Dialog.extend({
  title
: "Import options",
  dialogClass
: "import-file-dialog",


  init
: function (filename, onSubmit) {
   
// ...
   
this.showWindow();
 
},



  showWindow
: function () { ... }
});



I also made node's standart libraries without require it in every file, I put it in 'node' object https://github.com/Paxa/postbird/blob/master/lib/node_lib.js

I think our JS code should not pollute global namespace, but carefully create global variables. I like OOP in ruby, so I always name classes with first capital latter.

var ClassOrModule = {}; // global class or module
var someGlobalVar = 123; // just variable, very rare, usually inside some module
var MY_APP_VERSION = "1.2.3"; // something like constants, can also use 'const' keyword


Alain Ekambi

unread,
Apr 20, 2015, 6:20:43 AM4/20/15
to nwjs-g...@googlegroups.com

This is one of the reason we decided to use a language that compiles to for NW.js apps instead of using JavaScript. Structuring code always feel like a hack in js.

--
You received this message because you are subscribed to the Google Groups "nw.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nwjs-general...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ken...@gmail.com

unread,
Apr 22, 2015, 8:05:53 PM4/22/15
to nwjs-g...@googlegroups.com
It doesn't need to feel like a hack if you're using es6 modules. There's a great thing called JSPM that does all the modules/packages handling for you. I'm using it in my project and it's great

pierred...@gmail.com

unread,
Apr 23, 2015, 7:43:38 AM4/23/15
to nwjs-g...@googlegroups.com
Alain Ekamb: I'd be interested to know which language you use and how you made your choice.

Alain Ekambi

unread,
Apr 24, 2015, 11:20:16 AM4/24/15
to nwjs-g...@googlegroups.com
@pierre

We use Java(GWT)
We code in java and let the GWT compiler generate JavaScript for us.
We have create a Java API for mw.js (node js ). 

Note you dont have to use Java.
There other language that compile to JS too.
But I m not sure they have a nw.js api


--

Alain Ekambi

Co-Founder

Ahomé Innovation Technologies

http://www.ahome-it.com/

Pier Bover

unread,
Apr 24, 2015, 1:15:52 PM4/24/15
to nwjs-general
Thanks for the great info Alain.

So you write in Java, then compile to JS, and use some helper APIs to interact with JS APIs?

What are the benefits (in your experience) of taking this "detour"?

Alain Ekambi

unread,
Apr 24, 2015, 2:01:21 PM4/24/15
to nwjs-g...@googlegroups.com
Yes we use some wrappers to access nw.js/node.js API from Java. and vice versa

We choose this road first the team is more confident with Java that JavaScript.
When I say Java I actually mean strong typing. We choose the Java language because our backend is already written in Java so we could leverage the knowledge we already have.

1) So main benefit is definitely strong typing. Catch most of the errors at compile time is key for us(specially for someone who does lots of typos like me).
2 ) Then come Strong tooling. We can leverage all the tools available to java devs (Eclipse/Intellij. etc) to write web apps.
3) Codestructure. Because we leverage java no need to invent anything here. We use the regular java idioms(packages, interface, design pattern, etc etc) 
4) Backend already in java.
5) Good java devs are easier to find than good Javascripters :)

That s basically what motivated us to use Java.



Reply all
Reply to author
Forward
0 new messages