There are tons of ways to make web pages dynamic, i.e. have them update in real-time. For many years, constant automatic full-page refreshes were the only game in town.
But that’s more or less ignoring the web evolution of the past decade. With JavaScript in the browser, you can manipulate the DOM (i.e. the structure underlying each web page) directly. This has led to an explosion of JavaScript libraries in recent years, of which the most widespread one by now is probably JQuery.
In JQuery, you can easily make changes to the DOM – here is a tiny example:
And sure enough, the result comes out as:
But there is a major problem – with anything non-trivial, this style quickly ends up becoming a huge mess. Everything gets mixed up – even if you try to separate the JavaScript code into its own files, you still need to deal with things like loops inside the HTML code (to create a repeated list, depending on how many data items there are).
And there’s no automation – the more little bits of dynamic info you have spread around the page, the more code you need to write to keep all of them in sync. Both ways: setting items to display as well as picking up info entered via the keyboard and mouse.
There are a number of ways to get around this nowadays – with a very nice overview about seven of the mainstream solutions by Steven Sanderson.
I used Knockout for the RFM12B configuration generator to explore its dynamics. And while it does what it says, and leads to delightfully dynamically-updating web pages, I still found myself mixing up logic and presentation and having to think about template expansion more than I wanted to.
Then I discovered AngularJS. At first glance, it looks like just another JavaScript all-in-the-browser library, with all the usual expansion and looping mechanisms. But there’s a difference: AngularJS doesn’t mix concepts, it embeds all the information it needs in HTML elements and attributes.
AngularJS manipulates the DOM structure (better than XSLT did with XML, I think).
Here’s the same example as above, in Angular (with apologies for abusing ng-init a bit):
The “ng-app” attribute is the key. It tells AngularJS to go through the element tree and do its magic. It might sound like a detail, but as a result, this page remains 100% HTML – it can still be created by a graphics designer using standard HTML editing tools.
More importantly, this sort of coding can grow without ever becoming a mix of concepts and languages. I’ve seen my share of JavaScript / HTML mashups and templating attempts, and it has always kept me from using JavaScript in the browser. Until now.
Here’s a better example (live demo):
Another little demo I just wrote can be seen here. More physical-computing related. As with any web app, you can check the page source to see how it’s done.
For an excellent introduction about how this works, see John Lindquist’s 15-minute video on YouTube. There will be a lot of new stuff here if you haven’t seen AngularJS before, but it shows how to progressively create a non-trivial app (using WebStorm).
If you’re interested in this, and willing to invest some hours, there is a fantastic tutorial on the AngularJS site. As far as I’m concerned (which doesn’t mean much) this is just about the best there is today. I don’t care too much about syntax (or even languages), but AngularJS absolutely hits the sweet spot in the browser, on a conceptual level.
AngularJS is from Google, with MIT-licensed source on GitHub, and documented here.
And to top it all off, there is now also a GitHub demo project which combines AngularJS on the client with SocketStream on the server. Lots of reading and exploring to do!
JCW,
Just a quick question – I’m finding hard to keep up with the pace! Do you actually sleep or do you have a life-time supply of red bull at jeelabs HQ?
Perhaps the secret is in the Hopjes ;-)
Definitely going to add some of those to my next order! I’ve dipped in and out of node stuff for some time and found it very promising but needing more of a community around it – it looks like it’s now happened – exciting times!
Heh – I do admit I’m spending a huge amount of time on this right now. My problem is that I can’t stand it when something truly fascinating like this comes along and I can’t get to grips with it…
Hope you like the recent JS frenzy. Just a few more, then back to a more normal pace :)
Loving it! Thanks for all the inspiration.
These are very nice examples, thank you! I had no idea this was possible.
jquery->Knockout->AngularJS Exactly the route I went for dynamic web apps (my core business)