How to cook a complete Windows 8 application with HTML5, CSS3 and JavaScript in a week – Day 5

The Windows 8 Release Preview (RP) is now out and you can download it here:

It is then obvious that I must port my little UrzaGatherer for the Release Preview.

I also take this opportunity to talk about some upgrades I added like the data binding support.

The complete version can be found here:

The complete series can be found here:


Porting to Release Preview

To help you porting your own app, we (at Microsoft) released a great document:

Furthermore, I invite you to read the following blog where the Windows 8 developers team talk about the differences between CP and RP:

From my point of view here are the main concerns I encountered:

The new navigator.js

The first point is about the navigator.js file where I added some cool new things.

First of all I added animations during navigation process. I also added a new function in the pages called afterPageEnter. This function is called when the enterPage animation is played. This allows your code to be launched when everything is ready. But it also allows your animations to get all the power in order to keep them fluid even on small computers:

_navigated: function (args) {
    var that = this;
    var newElement = that._createPageElement();
    var parentedComplete;
    var parented = new WinJS.Promise(function (c) { parentedComplete = c; });

        WinJS.Promise.timeout().then(function () {
            if (that.pageElement.winControl && that.pageElement.winControl.unload) {
            return WinJS.UI.Pages.render(args.detail.location, newElement, args.detail.state, parented);
        }).then(function parentElement(control) {
            that._previousPage = newElement.winControl;


            var offset = { top: "0px", left: "50px" };
            var enterPage = WinJS.UI.Animation.enterPage(newElement, offset);
            enterPage.then(function () {

                setImmediate(function () {
                    if (that._previousPage.afterPageEnter) {

The complete new navigator.js file is available here:

The default page

The first page of your project must be something like that now:

(function () {
    "use strict";

    var app = WinJS.Application;
    var nav = WinJS.Navigation;
    var ui = WinJS.UI;

    app.onactivated = function (eventObject) {
        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch ||
            eventObject.detail.kind === ||
            eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.fileOpenPicker) {

            eventObject.setPromise(WinJS.UI.processAll().then(function () {
                WinJS.Resources.processAll().then(function () {

if (nav.location) { nav.history.current.initialPlaceholder = true; return nav.navigate(nav.location, nav.state); } else { return nav.navigate(UrzaGatherer.navigator.home); } }); })); } }; app.oncheckpoint = function (args) { }; app.start(); })();

The application activation is now handled by a promise presented by the argument of the onactivated event. The navigation is also launched by your code with the nav.navigate function.

Multi-sized listView

Another important point is the new way used to define multi-sized listViews.

listView.layout= new ui.GridLayout({
        groupInfo: function () {
            return {
                enableCellSpanning: true,
                cellWidth: 28,
                cellHeight: 13

This new naming was created in order to underline the fact that each item must have a proportional size relatively to a specified cell size (cellWidth / cellHeight).

Semantic Zoom bug workaround

The Release Preview is not the final version so some little bugs may be found.

One of them was really hard to fix so I decided to share the solution with you Sourire

On the main page, there is a semantic zoom. Under certain circumstances (linked to the power of the computer) the base listView just don’t display. Obviously this is not reproductible in debug mode (cool!) and not everytime (cool again !).

After fighting during a whole day, I finally find a workaround: After setting the datasources, you just have to launch again the layout process of the semantic zoom control:

var zoomedListView = element.querySelector(".zoomedList").winControl;

var groupDataSource = UrzaGatherer.ExpansionsListWithCardOfTheDay.createGrouped(
this.groupKeySelector, this.groupDataSelector, this.groupCompare); listView.itemDataSource = groupDataSource.dataSource; listView.groupDataSource = groupDataSource.groups.dataSource; zoomedListView.itemDataSource = groupDataSource.groups.dataSource; if (onlyOnce)


Please note the usage of the onlyOnce variable to be sure that you apply the patch only the first time.

The magic of setImmediate

A last important point of this migration is the understanding of the setImmediate (or msSetImmediate) function:

This function is used to execute your code right after the current block and also right after any remaining operations (animations, UI,etc.).

This will be really useful to handle some specifics problems like for instance the “click-of-the-dead” when your users click too quickly the back button. Indeed, in this case, using a setImmediate allows you to go back to the previous page but after the completion of the page rendering (if you don’t do that, WinJS can try to manipulate some disposed UI elements).

To take this problem in account, I updated the navigator.js file in order to change the back button click handler:

if (backButton) {
    backButton.onclick = function () {
        setImmediate(function () {

    if (nav.canGoBack) {
    } else {
        backButton.setAttribute("disabled", "disabled");

Another example can be found in the code used for the scrolling:

setImmediate(function () {
    if (currentIndex)
        listView.indexOfFirstVisible = currentIndex;


Adding binding with

I didn’t covered data binding during the previous episodes. It is indeed an important point and finally I used it broadly inside UrzaGatherer.

For instance, I wanted to add a random card in the home screen and I wanted to change the card every two minutes:


To do so, I created a binding list with all expansions. I added to this list a specific object used to represent the random card.

To make this card bindable (I mean: every change applied to the card is automaticaly synchronized with the UI), it is really simple because you just have to to use a class:

 var bindableCard = new;

Using this class, you create a completely bindable object.

<div class="cardOfTheDayTemplate" data-win-control="WinJS.Binding.Template">
    <div class="card-image-container" data-win-control="UrzaGatherer.Tools.DelayImageLoader"
        data-win-options="{root: 'cards'}">
        <img class="card-image" data-win-bind="src: url" src="#" />
    <div class="card-overlay">
    <h4 class="card-title" data-win-bind="textContent: name"></h4>
    <div class="item-border">
    <div class="item-check" 
data-win-bind="style.display: card.isChecked UrzaGatherer.Tools.BoolToDisplayConverter"> <div class="checkBackground"></div> <div class="checkmark">&#xE18A</div> </div> </div>

Everything is based on the data-win-bind. The templates used by the listviews are fully compatible with this object and when a property of the object change, the UI responds automatically (like in XAML!!):

var updateCardOfTheDay = function () {
    var card;

    do {
        var cardNumber = Math.floor(Math.random() * cards.length);
        card = cards[cardNumber];
    while (card.size.isPlane);

    if (cardOfTheDayBinding.element) {
        WinJS.Utilities.query("img", cardOfTheDayBinding.element).forEach(function (img) {
            WinJS.Utilities.removeClass(img, "loaded");

    cardOfTheDayBinding.url = card.logo; =;
    cardOfTheDayBinding.card = card;

With this superb technology, I decided to update all my templates. So for instance, when the user chooses to use local or distant files, I just have to change the according properties of my objects:

var updateBlockOfflineState = function (block, offline) {
            if (offlineMode) {
               block.logo = "ms-appdata:///local/blocks/" +":", "_") + ".png";
               block.banner = "ms-appdata:///local/blocks/" +":", "_") + "_banner.png";
            else {
               block.logo = root + "/blocks/" +":", "_") + ".png";
               block.banner = root + "/blocks/" +":", "_") + "_banner.png";



That’s it!!

That’s it! You’re done. The application can be published on the store (already done for UrzaGatherer:

Now, feel free to use my tutorials as a basis for your project. You’re almost ready now Clignement d'œil

Comments (5)

  1. Eustace says:


    I am referencing your amazing project tutorial to begin with mine.

    I initially also have started from Consumer Preview and now am porting to Release Preview.

    However, when doing so,

    I have this following issue:

    Splash Screen.html will load for 5 seconds and then switch to "default.html" which is suppose to have a DIV as contentHost to host home.html. However, Currently, when it navigates to home.html, the 'body content' doesn't load at all, but appbar, headers, logo, etc are all working fine. Only when I enter a 2nd page let's say index.html, and back to home.html will everything then be loaded properly.

    Do you know what problem I might be facing here?

    Thank you!

  2. have you change the navigator.js? And are you navigating from your code in the default.js?

    Look also in my sample at the premise I used to launch the process.all


  3. pom says:


  4. Kamran says:

    OMG, I ran into this Semantic Zoom bug a couple days ago and spent ALL DAY searching and trying to debug it. Finally discovered, by copying code into a blank project and trial and error, it was only when SZ was on the page and stepping through ui.js and base.js.

    The problem was I didn't know what I was searching for, I guess.

    Thanks for the rest of the tips, `setImmediate` also saved my butt (launching a URI from secondary tile right on startup).

  5. Kamran says:

    One last thing. I like but the problem with it is it only supports basic properties; I can't actually create a good view model off it (with functions, computed, properties, etc.).

    I found MvvmJS useful but documentation lacking. Still, it's the best I found without resorting to using a 3rd party library. Until I explore Knockout integration more, MvvmJS is pretty good stopgap.