Created
March 10, 2015 12:23
-
-
Save pke/36e63e557b0f7864fbfe to your computer and use it in GitHub Desktop.
WinJS Load CSS from app folder and watch its changes
This file contains 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
"ms-appdata:///local/conf/dailyPage.css".toStorageFileAsync() | |
.then (cssFile) => | |
style = document.createElement("style") | |
style.type = "text/css" | |
readFileAsync = -> | |
Windows.Storage.FileIO.readTextAsync(cssFile) | |
.then (cssText) -> | |
style.sheet.cssText = cssText | |
readFileAsync() | |
.then -> | |
document.head.appendChild(style) | |
# Now track the file and update the styles when the file is changed | |
# Since we cannot track individual files we have to watch the whole folder | |
queryOptions = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderByName, [".css"]) | |
cssFile.getParentAsync() | |
.then (folder) => | |
@fileQuery = folder.createFileQueryWithOptions(queryOptions) | |
@fileQuery.addEventListener("contentschanged", readFileAsync) | |
@fileQuery.getFilesAsync() | |
# Part of my custom WinJS PageNavigator unload hook | |
@unload["removeCssRefresher"] = -> | |
@fileQuery.removeEventListener("contentschanged", readFileAsync) | |
@fileQuery = null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment