Created
June 4, 2014 14:42
-
-
Save pke/02c52efa6fde4bd0a902 to your computer and use it in GitHub Desktop.
WinRT XmlLoader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StorageFile = Windows.Storage.StorageFile | |
XmlDocument = Windows.Data.Xml.Dom.XmlDocument | |
define [], -> | |
exports = | |
loadFromFileAsync: (file) -> | |
return WinJS.Promise.wrapError(new TypeError("file isn't a Windows.Storage.StorageFile")) unless file instanceof Windows.Storage.StorageFile | |
settings = new Windows.Data.Xml.Dom.XmlLoadSettings() | |
settings.elementContentWhiteSpace = false | |
XmlDocument.loadFromFileAsync(file, settings) | |
loadFromPathAsync: (path) -> | |
return WinJS.Promise.wrapError(new TypeError("file can't be empty")) unless path != "" | |
try | |
uri = new Windows.Foundation.Uri(path) | |
exports.loadFromUriAsync(uri) | |
catch e | |
return WinJS.Promise.wrapError(e) | |
loadFromUriAsync: (uri) -> | |
return WinJS.Promise.wrapError(new TypeError("uri isn't a Windows.Foundation.Uri")) unless uri instanceof Windows.Foundation.Uri | |
StorageFile.getFileFromApplicationUriAsync(uri).then(exports.loadFromFileAsync) | |
loadAsync: (something) -> | |
return WinJS.Promise.wrapError(new TypeError("something can't be empty")) unless something | |
WinJS.Promise.as(something) | |
.then (result) -> | |
if typeof result is "string" | |
exports.loadFromPathAsync(result) | |
else if result instanceof Windows.Foundation.Uri | |
exports.loadFromUriAsync(result) | |
else if result instanceof Windows.Storage.StorageFile | |
exports.loadFromFileAsync(result) | |
else | |
WinJS.Promise.wrapError(new TypeError("parameter must be a string, URI, StorageFile or promise that returns such type")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment