Skip to content

Instantly share code, notes, and snippets.

@pke
Created March 10, 2015 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pke/36e63e557b0f7864fbfe to your computer and use it in GitHub Desktop.
Save pke/36e63e557b0f7864fbfe to your computer and use it in GitHub Desktop.
WinJS Load CSS from app folder and watch its changes
"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