Introduction
In this article I describe how to create a Windows Store App for DataChanged event using JavaScript. For roaming of settings or files, the app needs to know when the data has changed to update its UI accordingly. The datachanged event will be invoked anytime a sync from the roaming handler occurs.
This app will respond to the datachanged event and update the UI. Run this app on two machines logged in with the same Microsoft Account to see the UI change as the updated setting is roamed down.
I assume you can create a simple Windows Store App using JavaScript. For more help visit Simple Windows Store Apps using JavaScript.
To start the creation of the app, add two JavaScript pages by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then give an appropriate name. In the same way, add one HTML page to your project.
Write the following code in default.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />
<script src="//Microsoft.WinJS.1.0/js/base.js"></script>
<script src="//Microsoft.WinJS.1.0/js/ui.js"></script>
<link rel="stylesheet" href="/css/default.css" />
<script src="/js/script1.js"></script>
<script src="/js/default.js"></script>
</head>
<body role="application" style="background-color: lightgray">
<center><div id="rootGrid">
<div id="content">
<h1 id="featureLabel"></h1>
<div id="contentHost"></div>
</div>
</div></center>
</body>
</html>
Write the following code in default.js:
(function () {
"use strict";
var sampleTitle = "";
var scenarios = [
{ url: "page.html" }
];
function activated(eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
eventObject.setPromise(WinJS.UI.processAll().then(function () {
var url = WinJS.Application.sessionState.lastUrl || scenarios[0].url;
return WinJS.Navigation.navigate(url);
}));
}
}
WinJS.Navigation.addEventListener("navigated", function (eventObject) {
var url = eventObject.detail.location;
var host = document.getElementById("contentHost");
host.winControl && host.winControl.unload && host.winControl.unload();
WinJS.Utilities.empty(host);
eventObject.detail.setPromise(WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {
WinJS.Application.sessionState.lastUrl = url;
}));
});
WinJS.Namespace.define("SdkSample", {
sampleTitle: sampleTitle,
scenarios: scenarios
});
WinJS.Application.addEventListener("activated", activated, false);
WinJS.Application.start();
})();
Write the following code in page.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/css/default.css" />
<script src="/js/script.js"></script>
</head>
<body>
<div data-win-control="SdkSample.ScenarioInput">
<p>
Add a Name:
<input type="text" id="roamingUserName" />
<button id="roamingSimulateRoaming">Add</button>
</p>
</div>
<div data-win-control="SdkSample.ScenarioOutput">
<div class="item" id="roamingOutput"></div>
</div>
</body>
</html>
Write the following code in script.html:
(function () {
"use strict";
var page = WinJS.UI.Pages.define("page.html", {
ready: function (element, options) {
document.getElementById("roamingSimulateRoaming").addEventListener("click", roamingSimulateRoaming, false);
Windows.Storage.ApplicationData.current.addEventListener("datachanged", roamingDataChangedHandler);
roamingDisplayOutput();
},
unload: function () {
Windows.Storage.ApplicationData.current.removeEventListener("datachanged", roamingDataChangedHandler);
}
});
var roamingSettings = Windows.Storage.ApplicationData.current.roamingSettings;
var settingName = "userName"; function roamingDataChangedHandler() {
var value = roamingSettings.values[settingName];
if (value) {
document.getElementById("roamingOutput").innerText = "Name: \"" + value + "\"";
} else {
document.getElementById("roamingOutput").innerText = "Name: <empty>";
}
}
function roamingSimulateRoaming() {
roamingSettings.values[settingName] = document.getElementById("roamingUserName").value;
Windows.Storage.ApplicationData.current.signalDataChanged();
}
function roamingDisplayOutput() {
roamingDataChangedHandler();
}
})();
Write the following code in script1.html:
(function () {
var ScenarioOutput = WinJS.Class.define(
function (element, options) {
element.winControl = this;
this.element = element;
new WinJS.Utilities.QueryCollection(element)
.setAttribute("role", "region")
.setAttribute("aria-labelledby", "outputLabel")
.setAttribute("aria-live", "assertive");
element.id = "output";
this._addOutputLabel(element);
this._addStatusOutput(element);
}, {
_addOutputLabel: function (element) {
var label = document.createElement("h2");
label.id = "outputLabel";
label.textContent = "";
element.parentNode.insertBefore(label, element);
},
_addStatusOutput: function (element) {
var statusDiv = document.createElement("div");
statusDiv.id = "statusMessage";
element.insertBefore(statusDiv, element.childNodes[0]);
}
}
);
var currentScenarioUrl = null;
WinJS.Navigation.addEventListener("navigating", function (evt) {
currentScenarioUrl = evt.detail.location;
});
WinJS.log = function (message, tag, type) {
var statusDiv = document.getElementById("statusMessage");
};
function activated(e) {
WinJS.Utilities.query("#featureLabel")[0].textContent = SdkSample.sampleTitle;
}
WinJS.Application.addEventListener("activated", activated, false);
WinJS.Namespace.define("SdkSample", {
ScenarioOutput: ScenarioOutput
});
})();
Output:
Summary
In this app I described the Datachanged Event in a Windows Store app using JavaScript. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.