Skip to content

Instantly share code, notes, and snippets.

@prettycode
Last active December 16, 2015 23:59
Show Gist options
  • Save prettycode/5517891 to your computer and use it in GitHub Desktop.
Save prettycode/5517891 to your computer and use it in GitHub Desktop.
Create toast with Windows.UI (Windows 8 JavaScript app).
// 1
(function(undefined) {
"use strict";
var Notifications = Windows.UI.Notifications;
var Xml = Windows.Data.Xml;
var ToastController = {
configToXML: function(config) {
config.version = config.version || "1";
config.template = config.template || "ToastText02";
return ""
+ "<toast>"
+ "<visual version='" + config.version + "'>"
+ "<binding template='" + config.template + "'>"
+ "<text id='1'>" + config.heading + "</text>"
+ "<text id='2'>" + config.message + "</text>"
+ "</binding>"
+ "</visual>"
+ "</toast>";
},
getToastDOM: function(xml) {
var toastDOM = new Xml.Dom.XmlDocument();
toastDOM.loadXml(xml);
return toastDOM;
},
showToast: function(config) {
var xml = this.configToXML(config),
toastDOM = this.getToastDOM(xml),
toast = new Notifications.ToastNotification(toastDOM);
Notifications.ToastNotificationManager
.createToastNotifier()
.show(toast);
}
};
})();
// 2
(function (undefined) {
"use strict";
// TODO: toast factory for xmlstring
var Notifications = Windows.UI.Notifications;
var Xml = Windows.Data.Xml;
var ToastController = {
getNotificationDOM: function (xmlString) {
var toastDOM = new Xml.Dom.XmlDocument();
toastDOM.loadXml(xmlString);
return toastDOM;
},
getNotification: function (notificationDOM) {
return new Notifications.ToastNotification(notificationDOM);
},
showNotification: function (notification) {
Notifications.ToastNotificationManager
.createToastNotifier()
.show(notification);
}
};
})();
// 3
(function () {
"use strict";
var Notifications = Windows.UI.Notifications;
var Xml = Windows.Data.Xml;
var ToastController = {
createToast: function (xmlString) {
var toastDOM = new Xml.Dom.XmlDocument();
toastDOM.loadXml(xmlString);
return new Notifications.ToastNotification(toastDOM);
},
showToast: function (toast) {
Notifications.ToastNotificationManager
.createToastNotifier()
.show(toast);
return toast;
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment