Start – Stop Magic in WinJS.Application Object

Introduction:
Start and Stop methods of WinJS.Application object explaining their functionality as by the names suggested.

Initial expectation:
These methods are responsible for the starting and stopping of the application, since we usually see them added to the default templates.

My experiment :
default.html file Content


  <body>
    <span id="deSpan" style="font-size:70px;"></span>
</body>


Step 1 execution
Default.JS file content

(function () {
    "use strict";
    var i = 0;
    WinJS.Application.onready = function () {
           document.getElementById("deSpan").innerText = "Completed";
    };

    WinJS.Application.onactivated = function () {
        document.getElementById("deSpan").innerText = "Activated";
        //WinJS.Application.stop();
    };

    WinJS.Application.start();

})();

Output

Output Without Stop()

Output Without Stop()

Description
The activated events get called first and then the ready event. So as the last text change to the span got displayed i.e. “Completed”.

Step 2 execution
Default.JS file content

(function () {
    "use strict";
    var i = 0;
    WinJS.Application.onready = function () {
           document.getElementById("deSpan").innerText = "Completed";
    };

    WinJS.Application.onactivated = function () {
        document.getElementById("deSpan").innerText = "Activated";
        WinJS.Application.stop();
    };

    WinJS.Application.start();

})();

Output

Output With Stop()

Output With Stop()

Description
The activated events get called first. But after that the Stop method reset the application object which prevented the ready event to occur. So as the last text change to the span got displayed i.e. “Activated”.

Final session learnt
Start and Stop are the methods, which are usually used to initialize and remove events associated with WinJS.Application object respectively.
On calling of Start method, it initializes all the application object level events like ready, load, activated etc. to the object.
On calling of Stop method, the execution of the app is not stopped but it resets the application state and prevents the application object level events get fired.
An application can also get started without the Start method. But we wont get the events in WinJS.Application.

Written By: Nirmal Hota, Cross-Platform Mobile Lead, Mindfire Solutions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s