If you want to impress your friends, try telling them you’re in the middle of “building an app.” A few billion-dollar acquisitions have turned app-building from a nerdy hobby into an indication that if you can build one, you’re inevitably going places.
I built some HTML5 “apps” in an hour by blatantly “plagiarizing” the WinJS library’s prewritten code. That’s the beauty of open-source libraries—instead of starting from scratch, you shop around for solutions other developers have already written. (I say “plagiarizing” jokingly—the whole point of these open code libraries is for you to use them.) Copying library scripts has taught me a lot about how apps work, and what I would need to do in order to make them my own. Here’s the first one, and here’s the second.
I’m just a journalist with an English degree, so what WinJS has taught me is that if you know which prewritten code components your app needs, you too can copy and paste your way to an app.
Here’s a tutorial for building your first WinjS app—even if you’re a complete beginner:
1. For the first seven steps, we need to actually install the WinJS library. If you do this kind of thing all the time, just read Microsoft’s own tutorial on GitHub. Otherwise, I’ve provided a highly detailed version, explaining why we’re doing each of these steps.
2. Open up the command line program on your Mac or PC. You’ll want to type a git command to make a local copy, or clone, of the WinJS Git repository:
git clone https://github.com/winjs/winjs.git
It’ll take a few seconds to clone, and it should look something like this when done:
3. When the command line is free, you’ll need to change to the directory where WinJS is actually stored:
If this doesn’t work, try the ls (list directory contents) command to make sure that “winjs” is one of the listed directories. If not, try the “cd ..” command to move up one directory higher.
In order to do this, we’re going to use npm, the free Node Package Manager. If you use Node.js, npm is essential for installing and managing Node programs. If you have Node, npm downloaded along with it.
Open a new command line window, make sure you are in your top directory, and type:
npm install -g grunt-cli
It’ll look a little something like this if it installs correctly:
(I had to use the superuser privilege—the sudo command—to make this install, so try that if the installer warns you that you “don’t have permission” to install grunt. Ignore any warnings the command line gives you about using sudo.)
5. Now we’ve installed Grunt globally. But that doesn’t mean it works on every cloned repository you have on your computer. So to install it in the WinJS clone and type these two lines:
You’ll notice this Grunt install takes a lot longer and is installing a lot more components. Why? Because when you install it inside a repository, it isn’t necessarily connected to your global Grunt install. So the programmers made sure that if you push your WinJS clone whole, it will work regardless of whether npm installed the package locally on that machine.
Here’s what it will look like if it worked, complete with that green text I’m always happy to see:
7. Congratulations! You’ve built a local copy of the WinJS library on your machine. Now we can actually begin coding the app. Open up a text editor. I adore Smultron for Mac, but you can also use the built-in TextEdit, or Notepad on a PC.
Create three blank documents that will become the heart of your new app. Save them all on the desktop of your computer for simplicity reasons.
8. Now we’re going to take a WinJS script in order to get started with how script borrowing works. Go to the WinJS preview site, select ListView on the lefthand column, and scroll down to Datasource Edits at the bottom. This is a pretty cool script that looks like the beginning of the code you’d need to write to build a Scrabble app.
See the JS, CSS, and HTML screens for this script? Copy and paste those directly into index.html, YourName.css, and YourName.js. Save and close the CSS and JS files. We still have a bit of work to do on index.html.
9. Let’s turn index.html into a full Web page. Right now it’s just a division, or <div>.
All HTML works by “nesting” tags, or making sure that each opening tag has a respective closing tag. So we need to put everything into one HTML document:
And then we need to make sure the <div> section is inside the visible part of the Web page, also known as the body. So above the div and below it put:
Finally, we need to connect the JS and CSS pages to the index page. We’ll do that in the head of the document, so they load when the page loads, but don’t show up in the body. Below the <html> tag but above the <body> tag, put:
<link rel="stylesheet" href="YourName.css" type="text/css" />
Great! But in order for the script to show up, we need to call the WinJS library that we installed on our computer earlier. Remember when Grunt put a ton of things inside the /bin folder? We need to call some of those out now.
If you look inside the /bin folder, you’ll see Microsoft.Phone.WinJS2.1 and Microsoft.WinJS.2.1. We’re building a Web app only for this tutorial, so we only need the second. Click that and inside it you’ll see the /css and /js folders. Inside those are the relevant CSS and JS scripts that make the WinJS library scripts appear the way they do on the preview site.
I didn’t want to call scripts from all the way inside the /bin, so I pulled the /js and /css folders out and put them in the same directory as my index.html file—my computer desktop. That way I only had to identify them as “js/FileINeed.js.” And then I called to them in the head of my document, which now looks like this:
<link rel="stylesheet" href="scrabble.css" type="text/css" />
<link rel="stylesheet" href="css/ui-dark.css" type="text/css" />
Here’s a GitHub Gist of what your entire index.html page should look like now.
10. If you’ve done everything right, you should be able to click on index.html and see your script running locally in a Web page! From here you can FTP it up to your server, like I did, in order to share it with your family and friends.
Now that you can make WinJS scripts functional, you’re well on your way to building your own original app. Come up with an idea, and mix and match the scripts in the WinJS library that you can put together to make that idea a reality.