I have been seeing a few blog posts which are discussing RequireJS usage with Windows 8/WinJS; however, I have not seen any posts with working samples so far. I want to take this blog article a step further and provide a fully functioning sample.
Why RequireJS with Windows 8?
Structuring your code for crossing platforms
The code sample I have included is my first stab at how you should structure your code for cross-platform. This is only one approach, so let me know what you think. [It seems pretty slick to me, but I’ll let the community decide 🙂 ].
HTML/CSS – define id’s and classes appropriately. You will probably have a unique version of each one per platform.
Native Modules – intercept code as necessary to add in native functionality, wire-up handlers to HTML, and chose the id’s to which everything is wired.
Common Modules – define the logic that happens when actions are performed during your application. Also can define your viewmodels, your business logic, your ajax calls etc.
What about iOS and Android?
Well, I still don’t have a perfect answer here just yet. I’ll have to do some playing around. My first thoughts are to create an app that launches a browser to view my web app. Following the above pattern, you can hit Windows 8, the Windows phone, the Surface, and the web while allowing access (but not native functionality) to iOS and Android. So not ideal, but maybe one day it will be.
I hope the sample project is helpful for you. I will continue to develop this code base and put it on all platforms using this basic architecture and idea. I’ll post some more stuff as I work on this, so please continue to check and see how it’s going. I will field questions and post tips and tricks as I find them.