windows 8 javascript (wonderland)

33
patterns & practices symposium 2013 HTML, CSS, JavaScript & Windows 8 christopher.bennage @microsoft.com

Upload: christopher-bennage

Post on 27-Jan-2015

546 views

Category:

Technology


0 download

DESCRIPTION

Things that were not obvious to me when I started building Windows Store apps with JavaScript.

TRANSCRIPT

Page 1: Windows 8 JavaScript (Wonderland)

patterns & practices symposium 2013

HTML, CSS, JavaScript &Windows 8

[email protected]

Page 2: Windows 8 JavaScript (Wonderland)

bennage

Page 3: Windows 8 JavaScript (Wonderland)

@bennagebennage

Page 4: Windows 8 JavaScript (Wonderland)

dev.bennage.combennage

Page 5: Windows 8 JavaScript (Wonderland)

WonderLand

Welcome to

“Curiouser and curiouser!” Cried Alice (she was so much surprised, that for the moment she quite forgot how to speak good English).

Page 6: Windows 8 JavaScript (Wonderland)

SandBoxLiving in a

Page 7: Windows 8 JavaScript (Wonderland)

LifeCycleUnderstanding the Application

Page 8: Windows 8 JavaScript (Wonderland)

LandscapeNavigating the

Windows Runtime Windows Library for JavaScript DOM JavaScript Visual Studio Templates

Page 9: Windows 8 JavaScript (Wonderland)

JavaScriptFascinating & Mesmerizing Bits Regarding

Page 10: Windows 8 JavaScript (Wonderland)
Page 11: Windows 8 JavaScript (Wonderland)

Responsiveness

Strange Things Affecting

JavaScript is Single Thread Everything Blocks the UI Even Unexpected Things

Page 12: Windows 8 JavaScript (Wonderland)

function blocking() {

var items = getBigArrayOfItems();

for(var i = items.length - 1; i >= 0; i--) { doExpensiveComputationOn(items[i]); }

}

Page 13: Windows 8 JavaScript (Wonderland)

function not_blocking() {

var items = getBigArrayOfItems(); var i = items.length - 1;

function processNext() { doExpensiveComputationOn(items[i]); i--; if(i >= 0) { setImmediate(processNext); } }

processNext();}

Page 14: Windows 8 JavaScript (Wonderland)

bindA very useful thing is

function add(x, y) { return x + y;}

var add7 = add.bind(null, 7);

var sum =add7(3); // sum is 10

Partial Application Returns a Function 1st arg resolves to this

Page 15: Windows 8 JavaScript (Wonderland)

function pretendToBeAsync(adder) { var sum = adder(); console.log(sum);}

var someObject = {

someNumber: 10,

add: function() { return this.someNumber + 1; },

problem: function() { pretendToBeAsync(this.add); }

};

someObject.problem();

Page 16: Windows 8 JavaScript (Wonderland)

jQuery?Did I miss

document.querySelector() WinJS.xhr() WinJS.Utilities.addClass()

Page 17: Windows 8 JavaScript (Wonderland)

Memory Leaks

Wat? This is JavaScript…

apps are long running higher chance of

memory leaks URL.createObjectURL(bl

ob)

Page 18: Windows 8 JavaScript (Wonderland)

Windows Library for JavaScript

Things to Know about the

Page 19: Windows 8 JavaScript (Wonderland)

PromisesUnderstand

Objects represent a task Composable Cancellable

Page 20: Windows 8 JavaScript (Wonderland)

AsyncBe sure to Cancel

Be aware of what’s happening A common scenario is

Navigating Away Custom promises probably

need to be cancelled too

Page 21: Windows 8 JavaScript (Wonderland)

WinJS.UI.Pages.define("some/page", {

ready: function(element, options) { this.promise = kickOffAsynProcess(); },

unload: function() { this.promise.cancel(); }

});

Page 22: Windows 8 JavaScript (Wonderland)

_createViewModels: function (files) { var count = files.length; var results = new Array(count); var index = count - 1; var proceed = true;

function onCancellation() { proceed = false; }

return new WinJS.Promise(function (complete, error) {

function processNextFile() { var file = files[index]; results[index] = new Hilo.Picture(file); index--;

if (index < 0) { complete(results); } else if (!proceed) { error("Cancel"); } else { setImmediate(processNextFile); } }

processNextFile();

}, onCancellation);}

Page 23: Windows 8 JavaScript (Wonderland)

NavigationUnderstand the built-in

WinJS.Navigation WinJS.UI.Pages navigator.js

Page 24: Windows 8 JavaScript (Wonderland)

miscellaniaVarious & Sundry

use a single AppBar built-in message queue manually create two-way

binding

Page 25: Windows 8 JavaScript (Wonderland)

Windows RuntimeA few things about the

Page 26: Windows 8 JavaScript (Wonderland)

WinRT Objects

They are not JavaScript, those

Objects originating within WinRT are not mutable like plain ol’ JavaScript objects.

WinJS.Binding.as()

Page 27: Windows 8 JavaScript (Wonderland)

var fileQuery = Windows.Storage.KnownFolders.picturesLibrary.createFileQuery();fileQuery.getFilesAsync(0, 2).then(function (results) { var storageFile = results[0]; var observable = WinJS.Binding.as(storageFile); // sad panda});

Page 28: Windows 8 JavaScript (Wonderland)

File SystemTips about the

queryOptions.applicationSearchFilter = "System.ItemDate:2013-01-01T00:00:00Z..2013-01-01T00:00:00Z";

some queries behave differently for different folders

Advanced Query Syntax file properties

Page 29: Windows 8 JavaScript (Wonderland)

Some of the Properties Available System.AcquisitionID System.ApplicationName System.Author System.Capacity System.Category System.Comment System.Company System.ComputerName System.ContainedItems System.ContentStatus System.ContentType System.Copyright System.DateAccessed System.DateAcquired System.DateArchived System.DateCompleted System.DateCreated System.DateImported System.DateModified System.DueDate System.EndDate System.FileAllocationSize System.FileAttributes System.FileCount System.FileDescription System.FileExtension System.FileFRN System.FileName System.FileOwner System.FileVersion System.FindData

System.FlagColor System.FlagColorText System.FlagStatus System.FlagStatusText System.FreeSpace System.FullText System.Identity System.Identity.Blob System.Identity.DisplayName System.Identity.IsMeIdentity System.Identity.PrimaryEmailAddress System.Identity.ProviderID System.Identity.UniqueID System.Identity.UserName System.IdentityProvider.Name System.IdentityProvider.Picture System.ImageParsingName System.Importance System.ImportanceText System.IsAttachment System.IsDefaultNonOwnerSaveLocation System.IsDefaultSaveLocation System.IsDeleted System.IsEncrypted System.IsFlagged System.IsFlaggedComplete System.IsIncomplete System.IsLocationSupported System.IsPinnedToNameSpaceTree System.IsRead System.IsSearchOnlyItem

System.IsSendToTarget System.IsShared System.ItemAuthors System.ItemClassType System.ItemDate System.ItemFolderNameDisplay System.ItemFolderPathDisplay System.ItemFolderPathDisplayNarrow System.ItemName System.ItemNameDisplay System.ItemNamePrefix System.ItemParticipants System.ItemPathDisplay System.ItemPathDisplayNarrow System.ItemType System.ItemTypeText System.ItemUrl System.Keywords System.Kind System.KindText System.Language System.LayoutPattern.ContentViewModeForB

rowse System.LayoutPattern.ContentViewModeForS

earch System.MileageInformation System.MIMEType System.Null System.OfflineAvailability System.OfflineStatus

Page 30: Windows 8 JavaScript (Wonderland)

StructureAn application’s

standard, historical

modules (AMD) something new &

magical

Page 31: Windows 8 JavaScript (Wonderland)
Page 32: Windows 8 JavaScript (Wonderland)

Unit TestsSundry Means of Accomplishing

how to structure frameworks functional style makes it easier

chutzpah.codeplex.com visionmedia.github.com/mocha

Page 33: Windows 8 JavaScript (Wonderland)

The End

• hilojs.codeplex.com• github.com/NobleJS/WinningJS