Last active
December 16, 2015 05:49
-
-
Save ekantola/5387404 to your computer and use it in GitHub Desktop.
RxJS Futurice Frontend Weeklies 2013-04-12
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
/* | |
* Sample 1 | |
* | |
* Fire up a content fetching request right away when still loading page, | |
* to reduce total loading time. But only render after document.ready | |
*/ | |
// Setup inputs | |
var dynamicContentStr = $.getAsObservable('/dynamic-content') | |
var documentReadyStr = $(document).readyAsObservable() | |
// Setup stateless streams, alternative 1: map into stream and switch | |
var dynamicContentAfterReadyStr = documentReadyStr | |
.map(always(dynamicContentStr)) | |
.switchLatest() // Bacon.js equivalent: flatMapLatest | |
// Setup stateless streams, alternative 2: zip as a "barrier" | |
var dynamicContentAfterReadyStr = documentReadyStr | |
.zip(dynamicContentStr) | |
.select(pickNthElement(1)) // synonym: map | |
// Get Stateful! | |
dynamicContentAfterReadyStr.subscribe(populateContent) | |
// Functional helpers | |
function always(x) { | |
return function() { | |
return x | |
} | |
} | |
function pickNthElement(n) { | |
return function(value) { | |
return value[n] | |
} | |
} | |
// State manipulation helpers | |
function populateCountries(response) { | |
$('.content').append(response.data.map(createContentItem)) | |
} | |
function createContentItem(itemData) { | |
return // something nice | |
} | |
function log(x) { | |
console.log(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment