<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Matthew Podwysocki's Blog</title><link>https://weblogs.asp.net:443/podwysocki/</link><description>Architect, Develop, Inspire...</description><item><title>Introduction to the Reactive Extensions for JavaScript – Conditionals</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-conditionals</link><description>&lt;P&gt;After spending the past couple of posts talking about how we integrate and why, let’s get back to the basic operators of the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt;.&amp;nbsp; This time, we’ll cover conditional logic that we can do with RxJS (which also applies to Rx.NET as well).&amp;nbsp; With traditional LINQ, we have the Where operator which allows us to filter operations, but it doesn’t allow us to do one operation or another easily.&amp;nbsp; Instead, the Reactive Extensions team has included two operators, If and Case which allow us some flexibility on how we want to execute conditional logic.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx"&gt;Refactoring a Game&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx"&gt;Async Method Chaining&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx"&gt;Custom Schedulers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/05/17/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/05/17/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer.aspx"&gt;Countdown Timers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/05/28/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/05/28/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api.aspx"&gt;Wrapping the Dojo Toolkit&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/06/04/introduction-to-the-reactive-extensions-for-javascript-mootools-integration.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/06/04/introduction-to-the-reactive-extensions-for-javascript-mootools-integration.aspx"&gt;MooTools Integration&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Conditional Branching with Observable.If&lt;/H2&gt;
&lt;P&gt;The first conditional operator we’ll cover is the Observable.If function.&amp;nbsp; This function acts as a normal if statement would in that it allows us to specify a conditional function and then have a branch for the “then” logic and another branch for the “else” logic.&amp;nbsp; The signature of the function is below:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:33644202-2711-4aa6-8aa4-1f4934c308e9 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; condition  : () -&amp;gt; bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; thenSource : Observable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; elseSource : Observable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; returns : Observable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
Rx.Observable.If(
    condition,
    thenSource,
    elseSource);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;For example, I could determine whether I want to use the cached value of an observable or start a fresh request to get some data based upon some setting.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ba106c54-7fd7-4f48-8553-37e1912c5841 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; cachedObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Subject();

...

Rx.Observable.If(
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; shouldCache; },
    cachedObservable
    Rx.Observable.Defer(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.getJSONAsObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someurl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
        })
    );&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;You’ll notice that I deferred the getJSONAsObservable and there’s a very good reason for that.&amp;nbsp; Such AJAX calls as this one are hot observables which mean that they fire immediately instead of waiting for the Subscribe call being made.&amp;nbsp; We’ll get into the whole hot versus cold observables in another post.&lt;/P&gt;
&lt;P&gt;So, you might be thinking, what’s the difference then between this and if I had written it the old fashioned way using an if statement?&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d5164ae-d54c-4abf-86c6-3cf9cc91b96f class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; cachedObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Subject();

...

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (shouldCache) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; cachedObservable;
}
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Defer(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.getJSONAsObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someurl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
        });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Well, the difference between the two is that the Rx.Observable.If function will evaluate the condition on subscription, whereas the if statement will evaluate eagerly for example on a hot observable.&amp;nbsp; The if statement is nice, but what if we have more than just a simple binary case?&lt;/P&gt;
&lt;H2&gt;Conditional Branching with Observable.Case&lt;/H2&gt;
&lt;P&gt;Another mechanism we have for branching logic is be being able to handle a switch statement.&amp;nbsp; What we want to give is the ability to write your traditional switch statement, but have it lazily execute just as we have the If function above.&amp;nbsp; We can take the following as an example where based upon n, return the appropriate Observable value.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:45950815-719d-4e73-bd80-876ee0f765c9 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;switch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(n)
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;case&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;:
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;case&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;:
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;default&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;:
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;3&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
}        &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In order to make this happen, we need to use the Rx.Observable.Case function which allows us to specify a condition, such as checking for the value of n, a hash object which contains key/value pairs with the key and their Observable value, and finally an optional default case.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ebbba2dc-9789-4232-8c6d-fe22e9844586 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; selector : () -&amp;gt; Key&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; sources  : Dictionary&amp;lt;Key, Observable&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; defaultSource : Observable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; returns : Observable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
Rx.Observable.Case(
    selector,
    sources,
    defaultSource);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In the case of our above switch statement, we’ll first want to create the hash of our sources.&amp;nbsp; Basically, the property name is the key and its value is the value of the key/value pair.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bd1b89a4-7ef6-4b83-aa51-00321fef88d9 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sources &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; : Rx.Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;),
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; : Rx.Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have the sources, let’s finish off the implementation.&amp;nbsp; First, for our selector, we’ll need to provide a function which takes no parameters and returns our desired key.&amp;nbsp; Next, we’ll provide the sources from above, and finally we’ll provide a default case if no others match.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1e3367b2-5866-4b8c-9bb3-af090c245479 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; cased &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Case(
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; n; },
    sources,
    Rx.Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;3&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;));&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;So, in the case of n being 1, then the first source will be selected, and so on for those along the line.&amp;nbsp; If n does not match either 1 nor 2, the defaultSource will be selected.&amp;nbsp; Let’s run through a quick example where we check if the detected language is a language we support, either French or Spanish, and if we do, then we translate.&amp;nbsp; Else, we will go ahead and throw an exception via the Rx.Observable.Throw function as part of the workflow to say that the given language isn’t supported.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3eec8b3f-eb88-4054-a0a0-d42a51e649e0 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; The variables could be something like:&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; var detected = "fr";&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; var text = "Nous allons traduire un texte";&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sources &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
    es : Rx.Observable.Defer(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translateFromSpanish(text);
         }),
    fr : Rx.Observable.Defer(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translateFromFrench(text);
         })
};

Rx.Observable.Case(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detected; },
        sources,
        Rx.Observable.Throw(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Not supported!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;))
    .Subscribe(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
            alert(data);
        },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(err) {
            alert(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;An error occurred: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; err);
        });
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And there you have it, you now have two ways of dealing with lazy conditional logic within your observable workflow.&lt;/P&gt;
&lt;H2&gt;Back to .NET&lt;/H2&gt;
&lt;P&gt;Even though my examples here are primarily in JavaScript, we have the exact same functionality in the Reactive Extensions for .NET and Silverlight.&amp;nbsp; For example, the If function is implemented as well with the signature as follows:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:19f66718-acdb-4bc2-bd53-1389dfda07c9 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; If&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    Func&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; condition,
    IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; thenSource,
    IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; elseSource
)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And a simple example of using this, such as an even/odd scenario:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bda3cdeb-81ad-4bc1-97ba-03288ac3d21b class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Observable.If&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    () &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; n &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;even&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;),
    Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;odd&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;));&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Just as well, we can use the Case statement, which has the following signature:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:018d3dc1-08e3-4138-bbce-995fe056d20f class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Case&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TValue, TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    Func&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TValue&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; selector,
    IDictionary&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TValue, IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sources,
    IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;TResult&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; defaultSource
)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we could walk through a simple example of creating our cases and then call the case with our variable n which could be either our 1, 2 or anything else.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:278d271c-cc52-4175-b0ef-484d3e86289a class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;var sources &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Dictionary&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
{
    { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) },
    { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) }
};

var cased &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable.Case&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    () &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; n,
    sources,
    Observable.Return(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;3&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;));&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;As with most examples I give on this blog, most of the core operators work nicely when going back and forth from RxJS to RxNET.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community.&amp;nbsp; At &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt;, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition.&amp;nbsp; By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;When we start creating more advanced workflows through the Reactive Extensions, we need powerful ways of expressing such concepts as conditional logic outside of the Where function we’re already accustomed to.&amp;nbsp; With the If and Case functions, we have two powerful pieces at our disposal to make those more complicated pieces a reality.&lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Fri, 18 Jun 2010 23:33:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-conditionals</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – MooTools Integration</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-mootools-integration</link><description>&lt;P&gt;In &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/28/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/28/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api.aspx"&gt;the previous post&lt;/A&gt;, I covered a bit about how we’ve integrated the Dojo Toolkit into the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript (RxJS)&lt;/A&gt; where we can leverage Dojo’s eventing and AJAX programming model.&amp;nbsp; Following onto that post, I wanted to move onto a different framework and show how we integrated it into RxJS.&amp;nbsp; This time, it’s &lt;A href="http://mootools.net/" mce_href="http://mootools.net/"&gt;MooTools&lt;/A&gt; up to the plate.&lt;/P&gt;
&lt;P&gt;Now the question comes, why then if the given framework works as well as it does, why would we care to integrate it into RxJS?&amp;nbsp; What does it buy us?&amp;nbsp; What if we want to combine two mouse moves together, one offset from the other so that we get a delta from the previous?&amp;nbsp; Or how about throttling the values coming from our events, either the first value only or a delay between each value?&amp;nbsp; Between all the callbacks, setTimeouts and the global state that you need to track, it becomes very hand to manage.&amp;nbsp; That’s the reason why we’ve created this layer where we can go seamlessly from MooTools and other JavaScript frameworks to RxJS.&amp;nbsp; We can either hop out of the world of our framework and into RxJS, or integrate it underneath the covers to manage the composable nature of your framework.&amp;nbsp; Both are great options…&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx"&gt;Refactoring a Game&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx"&gt;Async Method Chaining&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx"&gt;Custom Schedulers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/17/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/17/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer.aspx"&gt;Countdown Timers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/28/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/28/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api.aspx"&gt;Wrapping the Dojo Toolkit&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Diving Into MooTools Events&lt;/H2&gt;
&lt;P&gt;Once again the first part of our journey is to cover the eventing model of MooTools.&amp;nbsp; MooTools has the ability through any number of classes to be able to listen to events.&amp;nbsp; We’ll start with the Element class method addEvent, which attaches an event listener to a given DOM element.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:954b037e-0757-4e44-ab5f-0f3c8c471d05 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;someElement.addEvent(
    type, 
    fn);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Each parameter is described below:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;type (string) – the name of the event to monitor such as click, mousemove, etc. &lt;/LI&gt;
&lt;LI&gt;fn (function) – the callback function to execute when the event is fired. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The fn callback as noted above gives us a &lt;A href="http://mootools.net/docs/core/Native/Event" mce_href="http://mootools.net/docs/core/Native/Event"&gt;Event&lt;/A&gt; which has any number of properties including page and client position, keys used, the target and related targets as well as some methods to stop propagation.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Conversely to our addEvent, we can remove our handler from the Element by using the removeEvent method.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:316e235e-5607-4656-aee0-f93791afae3f class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;someElement.removeEvent(
    type, 
    fn);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Each parameter is described below:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;type (string) – the name of the event to remove &lt;/LI&gt;
&lt;LI&gt;fn (function) – the callback function to remove &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;We can then listen to events in MooTools such as a keyup event on a given input element.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e6efb614-ad81-4c7a-9a7d-4a5a2b340afe class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someInput&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).addEvent(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;keyup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;whatkey&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).set(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, key);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can wrap these events by using the Rx.Observable.Create function which hands us an observer to use and then we return a disposable which allows us to remove our listener.&amp;nbsp; Inside the Create function, we’ll create a handler which takes the event object from MooTools and passes it to the observer.OnNext.&amp;nbsp; After we’ve defined the handler, we call addEvent on our MooTools Element with the event type and we’ll pass in the handler as the method to invoke.&amp;nbsp; Finally, we return a function which is invoked when we call Dispose on our subscription.&amp;nbsp; Below is a wrapped version called Rx.Observable.FromMooToolsEvent.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eca5eb74-56ca-42d9-97f4-869650d231e4 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: white; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.FromMooToolsEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; function(obj, eventType) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observable.Create(function(observer) {
    
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Create handler for OnNext&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        var handler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; function(eventObject) {
            observer.OnNext(eventObject);
        };
        
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Subscribe to event&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        obj.addEvent(eventType, handler);
        
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Return function used to remove event handler&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; function() {
            obj.removeEvent(eventType, handler);
        };
    });
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;What this allows is for us to use a MooTools element to attach an event much like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:437633b9-d2e0-4478-84e6-2f235b511134 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; element &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someElement&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

Rx.Observable.FromMooToolsEvent(element, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousemove&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Where(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; event.client.x &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; event.client.y;
        })
    .Subscribe(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousePosX&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).set(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, event.client.x);
        });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;That’s all well and good, but I think it’s better to live and breathe within MooTools itself.&amp;nbsp; In order to do so, we need to extend a few objects that expose adding and removing event handlers, such as Element, Elements, Window, Document as well as any other class which inherits from the Event class.&amp;nbsp; To do that, we’ll use the &lt;A href="http://mootools.net/docs/core/Class/Class#Class:implement" mce_href="http://mootools.net/docs/core/Class/Class#Class:implement"&gt;Class method implement&lt;/A&gt; in which we pass in properties which alters the base class so that we can modify existing classes.&amp;nbsp; For example, I can extend the Element and all other classes mentioned above by doing the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:697fd050-27bd-48d1-8888-7bc28b230525 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; fromMooToolsEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(type) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; fromMooToolsEvent(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, type);
}

Window.implement({
    addEventAsObservable : fromMooToolsEvent
});

Document.implement({
    addEventAsObservable : fromMooToolsEvent
});

Element.implement({
    addEventAsObservable : fromMooToolsEvent
});

Elements.implement({
    addEventAsObservable : fromMooToolsEvent
});    

Events.implement({
    addEventAsObservable : fromMooToolsEvent
});  &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;By doing this, we can take the above example of hooking a mouse move listener to report the x position relative to the viewport when both the x and y values are the same:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c5e0f72c-966b-498e-94b9-1769a6c5ebf2 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someElement&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).addEventAsObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousemove&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Where(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; event.client.x &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; event.client.y;
        })
    .Subscribe(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousePosX&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).set(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, event.client.x);
        });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have the basics down here, what about the AJAX APIs?&lt;/P&gt;
&lt;H2&gt;Diving into the Request&lt;/H2&gt;
&lt;P&gt;Another area where the Reactive Extensions for JavaScript can integrate well is with AJAX APIs.&amp;nbsp; MooTools exposes three such classes in the Core library, &lt;A href="http://mootools.net/docs/core/Request/Request" mce_href="http://mootools.net/docs/core/Request/Request"&gt;Request&lt;/A&gt;, &lt;A href="http://mootools.net/docs/core/Request/Request.HTML" mce_href="http://mootools.net/docs/core/Request/Request.HTML"&gt;Request.HTML&lt;/A&gt; and &lt;A href="http://mootools.net/docs/core/Request/Request.JSON" mce_href="http://mootools.net/docs/core/Request/Request.JSON"&gt;Request.JSON&lt;/A&gt;.&amp;nbsp; Each of those do as you suspect with one handling general requests such as text, whereas the HTML and JSON classes handle their respective data types.&amp;nbsp; But the one I’m going to focus on is in the More MooTools library called &lt;A href="http://mootools.net/docs/more/Request/Request.JSONP" mce_href="http://mootools.net/docs/more/Request/Request.JSONP"&gt;Request.JSONP&lt;/A&gt;, which allows me to make cross-domain JSON calls.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b23450c2-cf0d-4d00-9bd8-8c7e41a51c67 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Request.JSONP(options);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The documentation for options is as follows:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;url - (&lt;EM&gt;url&lt;/EM&gt;) the url to get the JSON data &lt;/LI&gt;
&lt;LI&gt;callbackKey - (&lt;EM&gt;string&lt;/EM&gt;) the key in the url that the server uses to wrap the JSON results. So, for example, if you used &lt;EM&gt;callbackKey: 'callback'&lt;/EM&gt; then the server is expecting something like &lt;EM&gt;http://..../?q=search+term&amp;amp;callback=myFunction&lt;/EM&gt;; defaults to "callback". This must be defined correctly. &lt;/LI&gt;
&lt;LI&gt;data - (&lt;EM&gt;object&lt;/EM&gt;) additional key/value data to append to the url &lt;/LI&gt;
&lt;LI&gt;retries - (&lt;EM&gt;integer&lt;/EM&gt;; defaults to &lt;EM&gt;zero&lt;/EM&gt;) if this value is a positive integer, the JSONP request will abort after the duration specified in the &lt;EM&gt;timeout&lt;/EM&gt; option and fire again until the number of retries has been exhausted. &lt;/LI&gt;
&lt;LI&gt;timeout - (&lt;EM&gt;integer&lt;/EM&gt;; defaults to &lt;EM&gt;zero&lt;/EM&gt;) the duration to wait before aborting a request or retrying. &lt;/LI&gt;
&lt;LI&gt;injectScript - (&lt;EM&gt;mixed&lt;/EM&gt;; defaults to document head) where to inject the script elements used for the calls &lt;/LI&gt;
&lt;LI&gt;log - (&lt;EM&gt;boolean&lt;/EM&gt;) if &lt;EM&gt;true&lt;/EM&gt;, sends logging messages to &lt;A href="http://mootools.net/docs/more/Core/Log" mce_href="http://mootools.net/docs/more/Core/Log"&gt;Log&lt;/A&gt; (to console if it's defined). Defaults to &lt;EM&gt;false&lt;/EM&gt;. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;For example, should we want to search on Twitter once again, we can pass in our URL, the data for the query string, the callback and a function used for the callback.&amp;nbsp; We can then create the Request.JSONP instance with our options and send our request.&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:3fd441fe-1826-413b-ae41-9fa1662e5f94 class=wlWriterEditableSmartContent&gt;&lt;PRE class="brush: jscript; gutter: false; first-line: 1; tab-size: 4;  toolbar: true; "&gt;var options = {
    url: 'http://search.twitter.com/search.json',
    data: { rpp: "100", q: "4sq.com" }
    callbackKey: "callback" 
    onComplete: 
        function(data) {
            $("results").set("html", data.results[0].text);
        }
};
var req = new Request.JSONP(options);
req.send();&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We could also cancel our request as well should we need to by using the cancel function.&amp;nbsp; Once again, the issue arises of composability as well as error handling.&amp;nbsp; How could we integrate with key up events and throttle our request?&amp;nbsp; Sooner or later when we start composing things together via callbacks, our code could walk right off the screen.&amp;nbsp; Instead, RxJS could allow us some flexibility to compose events and AJAX calls together.&amp;nbsp; To make this happen, we can wrap the API once again like we did with Dojo and jQuery, but this time we’ll take a different approach.&lt;/P&gt;
&lt;P&gt;As before, we’ll create an AsyncSubject, which is used for asynchronous communication that happens once and then caches the value.&amp;nbsp; Next, we need to handle both the success and error conditions by adding the functions load and error respectively to the options object.&amp;nbsp; The load function simply passes the data where we call the subject’s OnNext with the data and then mark it as complete with the OnCompleted call.&amp;nbsp; The error function simply is given an error object so that we can see what went wrong and act accordingly.&amp;nbsp; Now what’s different between the two approaches is that we’re supporting a cancel option.&amp;nbsp; If we should call Dispose on our subscription before the request is complete, we want to have the ability to cancel our request.&amp;nbsp; To make this happen we’ll create a RefCountDisposable which allows us to cancel our request should we need to.&amp;nbsp; And finally we’ll return a CompositeDisposable which takes the Disposable from the AsyncSubject and from the cancel together so that if one ends before the other, then we can continue with the second.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6cde3b60-fbc4-43a4-927e-7ae242f90e5a class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;observable.MooToolsJSONPRequest &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(options) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; subject &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; root.AsyncSubject();
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; request &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;try&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
        options.onSuccess &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(html) {
            subject.OnNext(html);
            subject.OnCompleted();
        };
    
        options.onFailure &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(xhr) {
            subject.OnError({ kind: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;failure&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, xhr: xhr });
        };
    
        options.onException &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(headerName, value) {
            subject.OnError({ kind: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;exception&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, headerName: headerName, value: value });
        };
        
        request &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Request.JSONP(options);
        request.send();
    }
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;catch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(err) {
        subject.OnError(err);
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; refCount &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; root.RefCountDisposable(root.Disposable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(request) {
            request.cancel();
        }
    }));
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observable.CreateWithDisposable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(subscriber) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; root.CompositeDisposable(subject.Subscribe(subscriber), refCount.GetDisposable());
    });    
}    &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can also extend the Request.JSONP class (and thanks for the assistance from &lt;A href="http://twitter.com/sebmarkbage" mce_href="http://twitter.com/sebmarkbage"&gt;Sebastian Markbåge&lt;/A&gt;) by using the implement function once again.&amp;nbsp; We’ll create a toObservable function to implement this same functionality.&amp;nbsp; There is a difference though that the options are private to the class.&amp;nbsp; So, we can either use the setOptions function to set the options or since the Request.JSONP class inherits from the Events class, we can call addEvents directly to add properties for success, failure and exception cases.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9ef5b8d5-56df-4572-b84d-1ecf3053821d class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Request.JSONP.implement({
   
    toObservable: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; () {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; subject &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; root.AsyncSubject();
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; request &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;try&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
            
            request.addEvents({
    
                success: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
                    subject.OnNext(data);
                    subject.OnCompleted();
                },
    
                failure: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(xhr) {
                    subject.OnError({ kind: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;failure&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, xhr: xhr });
                },
    
                exception: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(headerName, value) {
                    subject.OnError({ kind: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;exception&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, headerName: headerName, value: value });
                }
    
            });
            
            request.send();
        }
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;catch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (err) {
            subject.OnError(err);
        }
    
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; refCount &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; root.RefCountDisposable(root.Disposable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; () {
            request.cancel();
        }));
    
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observable.CreateWithDisposable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (subscriber) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; root.CompositeDisposable(subject.Subscribe(subscriber), refCount.GetDisposable());
        });
    }        
}); &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once we’ve implemented this, we’re able to redo example where we query Twitter for those who are posting to FourSquare and has geolocation turned on and get the maximum user ID and then print their text.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:878a2c80-9d80-4b6a-904d-dfa5dd8c465c class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;window.addEvent(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;domready&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; options &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
        url: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        data: { rpp : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq.com&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; },
        callbackKey: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;callback&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    };

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Request.JSONP(options)
        .toObservable()
        .SelectMany(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(data.results);
            })
        .Where(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.geo &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
            })
        .Max(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.from_user_id;
            })
        .Subscribe(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                alert(result.text);
            },
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(error) {
                alert(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;An error!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; error.description);
            });
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;You can find the complete MooTools integration &lt;A href="http://gist.github.com/426252" mce_href="http://gist.github.com/426252"&gt;here&lt;/A&gt; and will be available in the next release of RxJS.&lt;/P&gt;
&lt;P&gt;Instead of this example where we go from MooTools to RxJS, we could also extend a number of classes to create an API totally in MooTools with RxJS underneath the covers.&amp;nbsp; With this, we could implement behavior similar to that of &lt;A href="http://www.toweljs.org/" mce_href="http://www.toweljs.org/"&gt;Towel.js&lt;/A&gt;, an extension to MooTools for reactive programming. &lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community.&amp;nbsp; At &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt;, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition.&amp;nbsp; By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;One important aspect of the Reactive Extensions for JavaScript is how well we play with other frameworks.&amp;nbsp; By giving out of the box extensions points, this allows for you to still use your framework of choice along with the Reactive Extensions to take care of the event-based and asynchronous behavior. &lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Sat, 05 Jun 2010 03:03:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-mootools-integration</guid><category>Event-based Programming</category><category>Functional Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Wrapping the Dojo API</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api</link><description>&lt;P&gt;Recently in some of the comments I’ve received (keep them coming BTW), I get questions about taking existing APIs and moving them towards using the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript (RxJS)&lt;/A&gt;.&amp;nbsp; How can we get away from constant callback hell and move towards composable asynchronous and event-based blocks instead?&amp;nbsp; In this post, I’m going to walk through how we wrapped the &lt;A href="http://dojotoolkit.org/" mce_href="http://dojotoolkit.org/"&gt;Dojo Toolkit&lt;/A&gt; APIs to provide both event handling and AJAX functionality.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx"&gt;Refactoring a Game&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx"&gt;Async Method Chaining&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx"&gt;Custom Schedulers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/17/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/17/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer.aspx"&gt;Countdown Timers&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Wrapping Dojo Events&lt;/H2&gt;
&lt;P&gt;The first part of connecting the &lt;A href="http://dojotoolkit.org/" mce_href="http://dojotoolkit.org/"&gt;Dojo Toolkit&lt;/A&gt; to RxJS is by wrapping the eventing model.&amp;nbsp; The Dojo Toolkit has the &lt;A href="http://dojotoolkit.org/reference-guide/dojo/connect.html#dojo-connect" mce_href="http://dojotoolkit.org/reference-guide/dojo/connect.html#dojo-connect"&gt;dojo.connect&lt;/A&gt; function which allows us to listen in on the execution of either DOM events or even function calls with a listener to be invoked when it is fired.&amp;nbsp; Conversely, if we wish to stop listening, we can call &lt;A href="http://dojotoolkit.org/reference-guide/dojo/disconnect.html#dojo-disconnect" mce_href="http://dojotoolkit.org/reference-guide/dojo/disconnect.html#dojo-disconnect"&gt;dojo.disconnect&lt;/A&gt; to remove the listener.&amp;nbsp; The connect method is as follows:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f9267360-02a9-4c09-a227-14ae01b69757 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.connect(
    obj, 
    event, 
    context, 
    method, 
    dontFix);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Each parameter is described below:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;obj – the source object for the event function &lt;/LI&gt;
&lt;LI&gt;event – the name of the event, such as “click” &lt;/LI&gt;
&lt;LI&gt;context – the object that the listener will receive as “this” &lt;/LI&gt;
&lt;LI&gt;method – a listener function to invoke when the event is fired &lt;/LI&gt;
&lt;LI&gt;dontFit – prevent the delegation of this connection to the DOM event manager &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;And the connect function returns a handle for which we can use to disconnect.&amp;nbsp; The disconnect function takes this handle for us to stop listening to the event.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:521f7021-a685-4199-8dc8-154e3c703205 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.disconnect(handle);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can wrap these events by using the Rx.Observable.Create function which hands us an observer to use and then we return a disposable which allows us to disconnect our listener.&amp;nbsp; Inside the Create function, we’ll create a handler which takes the event object from Dojo and passes it to the observer.OnNext.&amp;nbsp; After we’ve defined the handler, we call connect with the parameters and we’ll pass in the handler as the method to invoke.&amp;nbsp; That function returns to us a handle which we can then use to disconnect the listener.&amp;nbsp; Finally, we return a function which is invoked when we call Dispose on our subscription.&amp;nbsp; Below is a wrapped version called Rx.Observable.FromDojoEvent.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bc1f2ec7-5e05-4330-9444-a075e17057a1 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; fromDojoEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromDojoEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(obj, event, context, dontFix) {
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(observer) {
   
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Handle on next calls&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; handler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(eventObject) {
            observer.OnNext(eventObject);
        };
       
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Connect and get handle for disconnecting&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; handle &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dojo.connect(obj, event, context, handler, dontFix);
       
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Return disposable used to disconnect&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
            dojo.disconnect(handle);
        };
    });
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;If we want to make it look and feel more like it belongs in Dojo itself, we could also alias it as dojo.connectAsObservable as we so wish.&amp;nbsp; This part isn’t actually in the codebase just yet, but if the feedback is positive, then it can be.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:99425db2-6b9c-4321-aaea-612b25aafcd4 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.connectAsObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(obj, event, context, dontFix) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; fromDojoEvent(obj, event, context, dontFix);
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Below is an example to connect to a given button and then listen for one out of every four clicks.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:70a374a5-5fa6-4c86-a468-53c0002e76b9 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; obj &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dojo.byId(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sub1 &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromDojoEvent(obj, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Sample(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { alert(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Fourth click!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;); });

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Or&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sub2 &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dojo.connectAsObservable(obj, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Sample(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { alert(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Fourth click!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;); });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;H2&gt;Wrapping Callback Scenarios&lt;/H2&gt;
&lt;P&gt;Another part of an API we can wrap is around asynchronous behavior with callbacks, especially for such things as AJAX calls and effects.&amp;nbsp; For this part, we’re going to cover taking the &lt;A href="http://dojotoolkit.org/reference-guide/dojo/io/script.html#dojo-io-script" mce_href="http://dojotoolkit.org/reference-guide/dojo/io/script.html#dojo-io-script"&gt;dojo.io.script.get&lt;/A&gt; function and creating an observable from it.&amp;nbsp; The dojo.io.script.get function is an alternate IO mechanism to &lt;A href="http://dojotoolkit.org/reference-guide/dojo/xhrGet.html#dojo-xhrget" mce_href="http://dojotoolkit.org/reference-guide/dojo/xhrGet.html#dojo-xhrget"&gt;dojo.xhrGet&lt;/A&gt; that has the capability of doing cross-site data access by dynamically inserting a &amp;lt;script&amp;gt; tag into your web page.&amp;nbsp; This mechanism also supports JSONP for named callback scenarios.&amp;nbsp; Let’s look at the signature for the get function.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d5c3239-e322-47a4-a3bc-476f88543ec2 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.io.script.get &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    options);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The options takes the following properties:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;url – The URL to request data &lt;/LI&gt;
&lt;LI&gt;callbackParamName – The callback string used for JSONP &lt;/LI&gt;
&lt;LI&gt;checkString – A type of check to ensure the script has been loaded. Not used in &lt;/LI&gt;
&lt;LI&gt;preventCache – True to apply a unique parameter to prevent the browser from caching &lt;/LI&gt;
&lt;LI&gt;content – A JavaScript object of key/value pairs for parameter names and values &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;For example, we can create the options much like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:98f0ccdf-9614-4e60-bcb2-4f7a5e269d8e class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; jsonpArgs &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
    url: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    callbackParamName: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;callback&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    content: { rpp: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq.com&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; } };&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In order to use the get function, we also have to ensure that we’ve reference the dojo.io.script module such as the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6a5b4145-c906-44fc-91aa-e465ee383c0c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.require(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.io.script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);   &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now we can get down to how we’d wrap the functionality.&amp;nbsp; First, we’ll create an AsyncSubject, which is used for asynchronous communication that happens once and then caches the value.&amp;nbsp; Next, we need to handle both the success and error conditions by adding the functions load and error respectively to the options object.&amp;nbsp; The load function simply passes the data where we call the subject’s OnNext with the data and then mark it as complete with the OnCompleted call.&amp;nbsp; The error function simply is given an error object so that we can see what went wrong and act accordingly.&amp;nbsp; We can then call the get function with our options and then finally return our AsyncSubject.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9962277f-fd41-4057-8260-40a3670d4bdb class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.FromDojoScriptGet &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(options) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Create subject&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; subject &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.AsyncSubject();
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Handle success&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    options.load &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
        subject.OnNext(data);
        subject.OnCompleted();
    };
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Handle failure&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    options.error &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(error) {
        subject.Error(error);
    };
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;try&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Load the script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        dojo.io.script.get(newOptions);
    }
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;catch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (err) {
        subject.OnError(error);
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; subject;        
}; &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This pattern applies to any callback scenario where we have one value to yield and then we want to cache it.&amp;nbsp; And now we can query Twitter with the Reactive Extensions with Dojo underneath the covers.&amp;nbsp; For example, we could query Twitter for those who are posting to FourSquare and has geolocation turned on and get the maximum user ID and then print their text.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4606fedf-c1fd-4930-8a87-48d918627170 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;dojo.addOnLoad(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; jsonpArgs &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
        url: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        callbackParamName: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;callback&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        content: { rpp: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq.com&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; } };

    Rx.Observable.FromDojoScriptGet(jsonpArgs)
        .SelectMany(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(data.results);
            })
        .Where(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.geo &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
            })
        .Max(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.from_user_id;
            })
        .Subscribe(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                alert(result.text);
            },
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(error) {
                alert(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;An error!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; error.description);
            });
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;These two patterns for wrapping events and asynchronous operations doesn’t stop at Dojo, but also applies to the other frameworks out there including YUI3, Prototype, ExtJS, jQuery and so forth.&amp;nbsp; For the AJAX calls, MooTools is different and we’ll get into that in the next post.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community.&amp;nbsp; At &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt;, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition.&amp;nbsp; By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;One important aspect of the Reactive Extensions for JavaScript is how well we play with other frameworks.&amp;nbsp; By giving out of the box extensions points, this allows for you to still use your framework of choice along with the Reactive Extensions to take care of the event-based and asynchronous behavior.&lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Sat, 29 May 2010 01:43:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-wrapping-the-dojo-api</guid><category>Event-based Programming</category><category>Functional Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Talking Reactive Extensions</title><link>https://weblogs.asp.net:443/podwysocki/talking-reactive-extensions</link><description>&lt;P&gt;There has been a lot of movement around the Reactive Extensions lately and I thought I’d round them up for you in an easy to find location.&amp;nbsp; Between the new release and various interviews, there’s a bit to cover.&lt;/P&gt;
&lt;H2&gt;New Release&lt;/H2&gt;
&lt;P&gt;Last Friday there was a release of the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for both .NET and JavaScript&lt;/A&gt; for which the release notes can be found &lt;A href="http://blogs.msdn.com/rxteam/archive/2010/05/14/release-notes.aspx" mce_href="http://blogs.msdn.com/rxteam/archive/2010/05/14/release-notes.aspx"&gt;here&lt;/A&gt;.&amp;nbsp; On the .NET side of the house, the biggest addition is the addition of IQbservable, which is short of IQueryableObservable.&amp;nbsp; Just as IObservable is the mathematical dual of IEnumerable, IQbservable is the dual of IQueryable.&amp;nbsp; &lt;A href="http://community.bartdesmet.net/blogs/bart/default.aspx" mce_href="http://community.bartdesmet.net/blogs/bart/default.aspx"&gt;Bart de Smet&lt;/A&gt;, of the Cloud Programmability Team, explains it in full detail in his Channel 9 white boarding sessions &lt;A href="http://channel9.msdn.com/shows/Going+Deep/Bart-De-Smet-Observations-on-IQbservable-The-Dual-of-IQueryable/" mce_href="http://channel9.msdn.com/shows/Going+Deep/Bart-De-Smet-Observations-on-IQbservable-The-Dual-of-IQueryable/"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;On the JavaScript side of things, there were a few more items of note.&amp;nbsp; This includes some additional bindings for &lt;A href="http://dojotoolkit.org/" mce_href="http://dojotoolkit.org/"&gt;Dojo&lt;/A&gt;, &lt;A href="http://www.extjs.com/" mce_href="http://www.extjs.com/"&gt;Ext JS&lt;/A&gt;, &lt;A href="http://jquery.com/" mce_href="http://jquery.com/"&gt;jQuery&lt;/A&gt;, &lt;A href="http://mootools.net/" mce_href="http://mootools.net/"&gt;MooTools&lt;/A&gt;, &lt;A href="http://prototypejs.org/" mce_href="http://prototypejs.org/"&gt;Prototype&lt;/A&gt; and &lt;A href="http://developer.yahoo.com/yui/3/" mce_href="http://developer.yahoo.com/yui/3/"&gt;YUI3&lt;/A&gt;.&amp;nbsp; We ship the debug versions of these integrations so that you get an idea of how we did it, but just as well, how you could add more integration points yourself.&amp;nbsp; One new integration point that I’m very excited about is the &lt;A href="http://raphaeljs.com/" mce_href="http://raphaeljs.com/"&gt;Raphael&lt;/A&gt; bindings.&amp;nbsp; If you’re not familiar with Raphael, it is a JavaScript library for simplifying interaction with vector graphics on the web.&amp;nbsp; I hope to cover more of that shortly on what it is and how we did it.&amp;nbsp; Lastly, there have been quite a few new operators that have been added in the form of Case, DoWhile, If, For, Let and While.&amp;nbsp; Each of these will be covered as well coming up.&amp;nbsp; But by no means are we even close to being done!&lt;/P&gt;
&lt;H2&gt;JSConf&lt;/H2&gt;
&lt;P&gt;A few weeks ago, I, along with &lt;A href="http://twitter.com/jeffva" mce_href="http://twitter.com/jeffva"&gt;Jeffrey Van Gogh&lt;/A&gt;, attended the preeminent JavaScript conference, &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt; here in Washington, DC.&amp;nbsp; There, we got to meet some of the best and brightest in the JavaScript community and share ideas, and in our case around event-based and asynchronous patterns in JavaScript.&amp;nbsp; What was interesting was the talk breakdown between server side and client side JavaScript seemed about a 50-50 split, with a lot of focus on &lt;A href="http://nodejs.org/" mce_href="http://nodejs.org/"&gt;Node.js&lt;/A&gt;.&amp;nbsp; Noticing this trend, Jeffrey presented in the B Track on the Reactive Extensions where he &lt;A href="http://blogs.msdn.com/jeffva/archive/2010/04/14/node-js-bindings-for-rxjs.aspx" mce_href="http://blogs.msdn.com/jeffva/archive/2010/04/14/node-js-bindings-for-rxjs.aspx"&gt;announced our bindings for Node.js&lt;/A&gt;.&amp;nbsp; &lt;A href="http://twitter.com/reybango" mce_href="http://twitter.com/reybango"&gt;Rey Bango&lt;/A&gt; caught up with us on Sunday to talk to us about the Reactive Extensions for JavaScript which you can view &lt;A href="http://blog.reybango.com/2010/04/21/jsconf-2010-video-interviews-with-top-javascript-developers/" mce_href="http://blog.reybango.com/2010/04/21/jsconf-2010-video-interviews-with-top-javascript-developers/"&gt;here&lt;/A&gt;.&amp;nbsp; Also, there was &lt;A href="http://herdingcode.com/?p=251" mce_href="http://herdingcode.com/?p=251"&gt;a great wrap-up of JSConf on Herding Code&lt;/A&gt; with the organizer, &lt;A href="http://twitter.com/voodootikigod" mce_href="http://twitter.com/voodootikigod"&gt;Chris Williams&lt;/A&gt;, Rey Bango and myself on Herding Code you might want to check out.&lt;/P&gt;
&lt;P&gt;Overall, it was a great event and I’d highly recommend attending the upcoming &lt;A href="http://jsconf.eu/2010/" mce_href="http://jsconf.eu/2010/"&gt;JSConf.EU&lt;/A&gt; if you get the chance (although I hear it did sell out).&amp;nbsp; &lt;/P&gt;
&lt;H2&gt;Interviews and More&lt;/H2&gt;
&lt;P&gt;As if you hadn’t heard enough already, we’ve had a few interviews on various podcasts where we dig into the Reactive Extensions for .NET and JavaScript.&amp;nbsp; &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.hanselminutes.com/default.aspx?ShowID=216" mce_href="http://www.hanselminutes.com/default.aspx?ShowID=216"&gt;Erik Meijer on Hanselminutes&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://herdingcode.com/?p=252" mce_href="http://herdingcode.com/?p=252"&gt;Jeffrey Van Gogh and myself on Herding Code&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.dotnetrocks.com/default.aspx?showNum=558" mce_href="http://www.dotnetrocks.com/default.aspx?showNum=558"&gt;.NET Rocks Show 558 with me on the Reactive Extensions&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;With that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download the Reactive Extensions&lt;/A&gt; and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback&lt;/A&gt;!&lt;/P&gt;</description><pubDate>Fri, 21 May 2010 21:18:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/talking-reactive-extensions</guid><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – The Final Countdown Timer</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer</link><description>&lt;P&gt;In the previous couple of posts, I’ve talked about asynchronous method chaining and creating custom schedulers so I can repeatedly hit a data source for its data, transform it and then display the results.&amp;nbsp; Instead of just setting a custom interval between the results and querying the system, so this time, I want to stick a timer where I can monitor the time between queries.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx"&gt;Refactoring a Game&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx"&gt;Async Method Chaining&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/12/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers.aspx"&gt;Custom Schedulers&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Creating Countdowns with GenerateWithTime&lt;/H2&gt;
&lt;P&gt;In previous posts, the goal was to repeatedly query a system with a given interval so that we could see what is going on in the world of Twitter.&amp;nbsp; The goal of this post is to take this idea a step further to have a timer display how long we have until the system refreshes itself, and when the timer is exhausted, we requery and reset the timer.&amp;nbsp; To do this, we’ll need to make use of the GenerateWithTime function.&amp;nbsp; This function takes five parameters:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The initial state of the timer&lt;/LI&gt;
&lt;LI&gt;The condition of the timer of whether to continue&lt;/LI&gt;
&lt;LI&gt;The result selector which takes the state and you return what you want from it&lt;/LI&gt;
&lt;LI&gt;The time selector which says how much time you want between calls&lt;/LI&gt;
&lt;LI&gt;The iterate function which takes the state and increments it&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Below is the signature of the GenerateWithTime function:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bcbde5b8-f3b5-474f-ab4e-8eddddab0080 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; initialState : State&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; condition : State -&amp;gt; bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; resultSelector : State -&amp;gt; Result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; timeSelector : State -&amp;gt; int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; iterate : State -&amp;gt; State&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
Rx.Observable.GenerateWithTime &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    initialState, 
    condition, 
    resultSelector, 
    timeSelector, 
    iterate);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have some clue on what it’s expecting, let’s go ahead and put it to some good use.&amp;nbsp; What we’re going to do is create a timer that counts down from 90 seconds while setting the countdown element to the current value at each second interval.&amp;nbsp; You’ll notice the initial state is set to 90, the condition is while the state is greater than or equal to zero, our time selector is one second and our iterate decrements the state by one. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e47ce396-1e24-4eeb-94f2-1526a75f3bab class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; countdownUI() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.GenerateWithTime(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;90&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(x) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; x &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(x) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; x; },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(x) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(x) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; x &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })
        .Do(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(x) { $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#countdown&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(x); });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Our ultimate goal is to have something happen at the front end, such as query Twitter, count down from 90 seconds and then query again.&amp;nbsp; To facilitate that need, I’ll create the updateUI function which queries Twitter and pulls the first record that matches and then displays the result in our content area. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d9ed2ab-7b88-4b3f-bfe4-deb4ca7e331b class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; updateUI() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?q=4sq.com&amp;amp;rpp=1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.getJSONAsObservable(url)
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;].text; })
        .Do(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
                $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#content&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(result);
            });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once we’ve defined both our countdownUI and updateUI functions, let’s compose them together in such a way that it fires the updateUI, counts down to zero, then rinse and repeat.&amp;nbsp; To make use of that, we must first defer the Twitter search until we’ve subscribed which allows us to repeatedly call it and have it requery.&amp;nbsp; The Defer function is a static function which takes a function which takes no arguments and you must return an observable sequence.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:fe46b723-169c-4773-974c-69e069ac7195 class=wlWriterEditableSmartContent&gt;&lt;PRE class="brush: jscript; gutter: false; first-line: 1; tab-size: 4;  toolbar: true; "&gt;// observableFactory : () -&amp;gt; Observable

Rx.Observable.Defer = function(
	observableFactory);
&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Then once we have deferred our UI update, then we need to add it on to our existing observable using Concat so that we count down to zero before repeating.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:06aa4be0-ff22-4820-b16a-ae3ae5abce5c class=wlWriterEditableSmartContent&gt;&lt;PRE class="brush: jscript; gutter: false; first-line: 1; tab-size: 4;  toolbar: true; "&gt;Rx.Observable.prototype.Concat = function (
	o1);
&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And once we Concat, we repeat it all over again to keep querying and counting down.&amp;nbsp; We can now run our observable sequence by calling Subscribe with no arguments (since we’re not doing anything with the results).&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:04b10a16-328f-4048-9917-26de552e73b4 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
    Rx.Observable.Defer(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; updateUI(); })
        .Concat(countdownUI())
        .Repeat()
        .Subscribe();
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Putting the code all together, we’re able to watch this in action where it queries Twitter for mentions of 4sq.com and displays the first result every 90 seconds.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_5A5D5A7D.png" mce_href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_5A5D5A7D.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_355F9A04.png" width=486 height=118 mce_src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_355F9A04.png"&gt;&lt;/A&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;You can find the entire code for this example &lt;A href="http://gist.github.com/399479" mce_href="http://gist.github.com/399479"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;Side Note&lt;/H2&gt;
&lt;P&gt;Just as a side note, many of these scenarios that I’ve been doing for this series apply to the Reactive Extensions for .NET as well.&amp;nbsp; For example, we could do something similar in C# that we had above.&amp;nbsp; For example, we could have our two services, each of which return nothing (Unit in this case), but do their actions as a side effect instead.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:272c68e5-5241-41e9-a4a2-d7c735c6e7de class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Unit&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; UpdateResults() 
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; SearchTwitter()
        .Select(data &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; data.results[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;])
        .ObserveOnDispatcher()
        .Do(result &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Item.Result &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result)
        .Select(_ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Unit());
}

IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Unit&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Countdown()
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable.GenerateWithTime(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;90&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
            time &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; time &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
            time &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; time,
            _ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; TimeSpan.FromSeconds(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;),
            time &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; time &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
        .ObserveOnDispatcher()
        .Do(time &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Item.Refresh &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; TimeSpan.FromSeconds(time))
        .Select(_ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Unit());
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we can listen much like before with our JavaScript example with complete parity between the two examples. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:93db90cb-8b47-4fbb-ba00-b8b0fcb1e3de class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Observable.Defer(() &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; UpdateResults)
    .Concat(Countdown())
    .Repeat()
    .Subscribe();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The difference between the two is quite small, except for the observing on the dispatcher as well as making sure that our observable sequences have the same signature of IObservable&amp;lt;Unit&amp;gt;.&amp;nbsp; In the JavaScript land, we don’t have to care since we’re dealing with a weakly typed dynamic language.&amp;nbsp; Hopefully that’ll give you a bridge between the two worlds.&amp;nbsp; In the future, I’ll make sure how to bridge the gap between the two worlds more often.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community.&amp;nbsp; At &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt;, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition.&amp;nbsp; By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.&amp;nbsp; One such example is querying a system and having a public countdown until the next refresh is going to be done.&amp;nbsp; That’s just one of the many things you can do with RxJS.&lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Mon, 17 May 2010 22:56:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-the-final-countdown-timer</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Custom Schedulers</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers</link><description>&lt;P&gt;In the &lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx"&gt;previous post&lt;/A&gt;, I talked a little bit about asynchronous method chaining and extending &lt;A href="http://jquery.com/" mce_href="http://jquery.com/"&gt;jQuery&lt;/A&gt; in a fluent style to fetch tweets from Twitter and append them to a given element.&amp;nbsp; This time I want to expand upon that post, instead of taking only one tweet, I want to take one hundred of them and then cycle through each of them at a given interval.&amp;nbsp; In order to do that, I must make use of something that I alluded to in earlier posts, and that is a custom scheduler.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx"&gt;Refactoring a Game&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/10/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining.aspx"&gt;Async Method Chaining&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Custom Schedulers&lt;/H2&gt;
&lt;P&gt;Another variation on a theme from our last post could be to take instead of just one tweet, but instead, take 100 of them and then fade each one out in turn by a specified interval.&amp;nbsp; To do this, we’ll have to make use of a custom scheduler.&amp;nbsp; To create a scheduler, we need three parameters, a schedule, a schedule with time and a now parameter.&amp;nbsp; The schedule parameter provides an implementation function to the scheduler that runs an action at the scheduler’s convenience.&amp;nbsp; The schedule with time parameter provides an implementation function to the scheduler to schedule the given action at the specified time from now.&amp;nbsp; Finally, we provide a now function which provides an implementation function to the scheduler with the timer’s notion of the current time. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f787dd12-3dcf-4e7e-8d48-3a5159d6f2ac class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; schedule : action -&amp;gt; disposable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; scheduleWithTime : action -&amp;gt; due time -&amp;gt; disposable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; now : () -&amp;gt; time&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
Rx.Scheduler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    schedule,
    scheduleWithTime,
    now);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;With this new found knowledge, we can create our own custom scheduler which takes a delay in milliseconds to create a scheduler.&amp;nbsp; We can create our Rx.Scheduler by passing in our three functions, the schedule, the schedule with time and the notion of now.&amp;nbsp; Let’s break each function parameter down one by one.&lt;/P&gt;
&lt;P&gt;The schedule function takes a given action and then makes a call to window.setTimeout with our action and our delay which allows us to evaluate the action after the given span.&amp;nbsp; We then return a Disposable which then gives us the ability to cancel the timeout with our given id from our setTimeout should we need to.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:66b7eeb1-904a-403f-a1b7-3b02d476fac8 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(action) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; id &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; window.setTimeout(action, delay);
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Disposable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { 
        window.clearTimeout(id); });
},&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The schedule with time function takes given action and a due time, and like before, it makes a call to window.setTimeout but this time with the action and the given due time.&amp;nbsp; And just as before, we create a Disposable to allow us to clean up and cancel the timeout should we need to.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb342250-ecc5-4124-b585-a4590110ba51 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(action, dueTime) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; id &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; window.setTimeout(action, dueTime);
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Disposable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { 
        window.clearTimeout(id); });
},&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Finally, the now parameter takes no arguments and we’ll simply return the current time.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ddf2aa38-2f5b-4e9a-8fc2-defbe942ac8d class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Date().getTime();
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;When we put these together we can create our custom DelayedScheduler such as the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:194a44b6-c0c8-4cb4-81c2-069f9f9a205c class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; delayedScheduler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.DelayedScheduler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(delay) {

     &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Scheduler(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(action) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; id &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; window.setTimeout(action, delay);
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Disposable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { 
                window.clearTimeout(id); });
        },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(action, dueTime) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; id &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; window.setTimeout(action, dueTime);
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Disposable.Create(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { 
                window.clearTimeout(id); });
        },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Date().getTime();
        });
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we’ve defined our custom scheduler, it’s time to put it to use.&amp;nbsp; What we’re going to do is take the array of our tweets from Twitter and put the delay between them.&amp;nbsp; To do that, we’ll make use of the Rx.Observable.FromArray method which takes not only the array we want to turn into an observable, but also a custom scheduler should we need one.&amp;nbsp; We’ll create an extension to the Array class which allows us to turn our arrays easily into observables.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f7599cfc-7104-4451-8c75-df43f0dd6f36 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Array.prototype.toObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(scheduler) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, scheduler);
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once the extension has been defined, let’s do something useful with it.&amp;nbsp; We’ll take the approach once again from the previous post to get tweets, but this time, we’ll take the maximum we can, which is 100 and then space each one out accordingly, and then rinse and repeat after we’ve exhausted our 100 tweets.&amp;nbsp; In our subscription, we’ll fade in and fade out our text as we append it to the content to give ourselves a rolling feel to it.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6295125b-595d-431a-a3af-da4ad60b9ecf class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?q=4sq.com&amp;amp;rpp=100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; customScheduler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.DelayedScheduler(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;5000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

$.ajaxAsObservable({
            url: url,
            type: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
            dataType: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;jsonp&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        })
    .SelectMany(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results.toObservable(customScheduler);
        })
    .Repeat()
    .Subscribe(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;p/&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
                .fadeTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
                .html(result.text)
                .fadeTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0.1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
                .appendTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#content&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
        });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;When we fire up our browser of choice we can see the fruits of our labor such as the following with proper spacing between the two and nice effects fading in and out.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_04D950D8.png" mce_href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_04D950D8.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_79439398.png" width=492 height=185 mce_src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_79439398.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And there you have it, using a custom scheduler to take data we already have and spread it out over time.&amp;nbsp; The idea here is to not tax the remote server, but instead query the system once, display all the results and then hit the server again once we’ve exhausted the data source.&amp;nbsp; You can find the complete example of this code &lt;A href="http://gist.github.com/399145" mce_href="http://gist.github.com/399145"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community.&amp;nbsp; At &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt;, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition.&amp;nbsp; By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.&amp;nbsp; For example, we can query a system for the data, space it out over time and then requery the system for more as we need it.&amp;nbsp; That’s just one of the many things you can do with RxJS.&lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Mon, 17 May 2010 22:53:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-custom-schedulers</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Async Method Chaining</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining</link><description>&lt;P&gt;Recently, there was a blog post by &lt;A href="http://www.dustindiaz.com/" mce_href="http://www.dustindiaz.com/"&gt;Dustin Diaz&lt;/A&gt; about &lt;A href="http://www.dustindiaz.com/async-method-queues/" mce_href="http://www.dustindiaz.com/async-method-queues/"&gt;method chaining over asynchronous operations&lt;/A&gt;.&amp;nbsp; This topic, of course is near and dear to my heart as it strikes the exact chord of what the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript (RxJS)&lt;/A&gt; is trying to solve.&amp;nbsp; The ability for us to take asynchronous operations and events and treat them as push collections, we are able to then compose functions together as if they were normal pull collections like arrays.&amp;nbsp; In this post, I’ll walk through the example that was posted on Dustin’s site and show how it can be done using RxJS.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/05/04/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game.aspx"&gt;Refactoring a Game&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Asynchronous Method Chaining&lt;/H2&gt;
&lt;P&gt;To show off some of the power of the Reactive Extensions for JavaScript, let’s take the example of creating a paragraph, populating it from a single tweet from Twitter, adding a class and finally appending it to a div element.&amp;nbsp; Just as well, we want to optionally give you a way to gracefully handle errors should the data source not be available.&amp;nbsp; And when has that ever happened to Twitter, I mean really??!!&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:78dc3015-898b-4a6b-af2b-b297d9216b9c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;p/&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .fetch(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?q=4sq.com&amp;amp;rpp=1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .addClass(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;loading&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .appendTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#content&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Or&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; errHandler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(exn) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Do something with the exception&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;}

$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;p/&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .fetch(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?q=4sq.com&amp;amp;rpp=1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, errHandler)
    .addClass(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;loading&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .appendTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#content&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This is fairly simple to do through the use of a little &lt;A href="http://jquery.com/" mce_href="http://jquery.com"&gt;jQuery&lt;/A&gt; and RxJS knowledge.&amp;nbsp; First, we’ll extend the jQuery fn with a function called fetch which takes a URL and an optional onError handler which is a function that takes an exception and returns no value.&amp;nbsp; Depending on whether the error handler was supplied or not, we’ll create an observer from the onNext which sets the tweet into the innerHTML of the jQuery selector.&amp;nbsp; We’ll then call the ajaxAsObservable extension which comes with RxJS to turn the ajax function into an observable, and subscribe with our observer.&amp;nbsp; Finally, we return the context of the selector as the result of the function which allows us to continue chaining calls.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f543fa54-8030-4e75-9d13-4e1a0ae51406 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;($) {

    $.fn.fetch &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(url, onError) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; collection &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; onNext &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { 
            $(collection).html(d.data.results[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;].text); 
        }
        
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; onError &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observer.Create(onNext, onError) : onNext;

        $.ajaxAsObservable({
            url: url,
            type: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
            dataType: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;jsonp&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        })
            .Subscribe(observer);

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
    };

})(jQuery);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;That’s a fairly simple scenario for using this, so, how about something a bit more complex like fetching the results at a given interval?&amp;nbsp; We could then create a function called fetchEvery which takes a specified interval to call the AJAX service and populate the selector by using the Interval method with our desired interval.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e893d5bd-e60c-4ab9-b947-2540ec1b4202 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$.fn.fetchEvery &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(url, interval, onError) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; collection &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; onNext &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { $(collection).html(d.data.results[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;].text); }
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; onError &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observer.Create(onNext, onError) : onNext;

    Rx.Observable.Interval(interval)
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.ajaxAsObservable({
                url: url,
                type: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
                dataType: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;jsonp&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
            }); })
        .Switch()
        .Subscribe(observer);

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we could use it much like the following where we fetch the results every 5 seconds.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ea15f3b2-71c9-4845-80ad-6c90083acc11 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;p/&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .fetchEvery(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?q=4sq.com&amp;amp;rpp=1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;5000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .addClass(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;loading&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .appendTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#content&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;But what about further transforming the data as it comes back, such as the example of censoring the swear words and create links from the text?&amp;nbsp; Well, instead of living inside jQuery and sprinkling in RxJS like we have been doing for the past couple of examples, we can then decide that instead we’ll live inside RxJS and sprinkle in jQuery.&amp;nbsp; We’ll create functions to censor the text, linkify it, handle any exceptions and finally handle the text as it comes in so that we can add it to the content.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0c280648-8150-499e-8247-d2f2d50131fe class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: white; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; handleText(text) {
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;p/&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(text).addClass(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;loading&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).appendTo(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#content&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; errHandler(exn) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; handle exception&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; censorTweet(uncensored) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; handle censoring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; linkify(tweet) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; linkify tweet&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;}

$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

    $.ajaxAsObservable({
        url: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?q=4sq.com&amp;amp;rpp=100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        type: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
        dataType: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;jsonp&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    })
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;].text; })
        .Select(censorTweet)
        .Select(linkify)
        .Subscribe(handleText, errHandler);
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This gives us the ability now to get the data from Twitter, censor it if necessary, create real links from the text, and finally put the results into our content in a fluent style while mixing in jQuery where we need to.&amp;nbsp; Just as well, I could have used any other library including Dojo, ExtJS, YUI, Prototype, Moo Tools or whatever I felt like, so I’m not particularly tied here to a certain library and I get the same win from each.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dealing with asynchronous programming has been in the forefront of many minds in the JavaScript community.&amp;nbsp; At &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf&lt;/A&gt;, there were several examples of frameworks trying to get around the idea of callbacks and instead lean more towards composition.&amp;nbsp; By utilizing the Reactive Extensions for JavaScript, we’re able to compose together asynchronous and event-based operations together and transform the results in interesting ways.&amp;nbsp; We can also live well inside the given frameworks such as jQuery and sprinkle in RxJS, or we can live in RxJS and sprinkle in the calls to the various libraries such as jQuery, Moo Tools, Prototype, and so forth.&lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Tue, 11 May 2010 04:40:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-async-method-chaining</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Refactoring a Game</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game</link><description>&lt;P&gt;We’ve covered a lot of ground so far in this series including a lot of the combinators as well as the integration into third party libraries.&amp;nbsp; Most of these examples have been from a green field mindset where we have an idea and then walk through how you would accomplish it using the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript (RxJS)&lt;/A&gt;.&amp;nbsp; This time, we’re going to take a game that’s already been written and take parts of it and migrate it to use RxJS.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/23/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin.aspx"&gt;Going “Parallel” with ForkJoin&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Refactoring Pong&lt;/H2&gt;
&lt;P&gt;During this series, I’ve been asked to take examples from other sources and show how those ideas can be expressed using RxJS.&amp;nbsp; In this series, I’m going to take an existing game from an article entitled &lt;A href="http://www.devx.com/webdev/10MinuteSolution/27134/0/page/1" mce_href="http://www.devx.com/webdev/10MinuteSolution/27134/0/page/1"&gt;“An Introduction to Game Programming with JavaScript”&lt;/A&gt; where the pong example is written in plain ol’ JavaScript and involves some events and callback scenarios.&amp;nbsp; What I will do is take this example and walk through step by step how to think differently about the application and how RxJS, with the addition of jQuery, can help.&amp;nbsp; After all, just sprinkling in a little jQuery everywhere can’t hurt, right?&amp;nbsp; Even for the simple case of &lt;A href="http://www.doxdesk.com/updates/2009.html#u20091116-jquery" mce_href="http://www.doxdesk.com/updates/2009.html#u20091116-jquery"&gt;adding two numbers together&lt;/A&gt;…&lt;/P&gt;
&lt;P&gt;Anyhow, let’s start with the beginning to understand how this application works.&amp;nbsp; We have a simple page with a paddle and a ball and a place to display the score.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:198e95f6-7171-4787-8245-ade6ca758fb0 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;body &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;onLoad&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="init()"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;h1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Rebound&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;h1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="playingArea"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="paddle"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;img &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="paddle.gif"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="ball"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;img &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="ball.gif"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="score"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            Score: 0
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;As you may notice, we have a body.onLoad defined which calls in init function.&amp;nbsp; This init function initializes the elements for the paddle, ball and score, registers the event handler for the keydown event and then starts the main loop.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d9d52d1d-9c96-459a-90ac-8cd0af39169e class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; init(){
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;instantiate HTML object instance vars&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    ball &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; document.getElementById(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;ball&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    paddle &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; document.getElementById(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;paddle&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    score &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; document.getElementById(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;score&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;register key listener with document object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    document.onkeydown &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; keyListener;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;start the game loop&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    start();
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Let’s refactor this a little bit so that we use jQuery selectors, as well as refactoring how the keydown events are handled.&amp;nbsp; You’ll notice we created a function to initialize our handlers which I’ll cover shortly.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:159bb682-84ac-4c9f-8a33-d008f1036fac class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

    ball &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#ball&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    paddle &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#paddle&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    score &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#score&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    
    initializeHandlers();
    
    start();
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Previously, we hooked our keydown event handlers on our document by using the keyListener function.&amp;nbsp; This function listened for either the left or right arrow and then moved the paddle appropriately by four pixels depending on the direction desired. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2467ea57-d945-4759-a944-68a7962d25ba class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; keyListener(e){
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;e){
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;for IE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;      e &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; window.event;
  }
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(e.keyCode&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;37&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;&amp;amp;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;){
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;keyCode 37 is left arrow&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;      paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
      paddle.style.left &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;px&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
  }
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(e.keyCode&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;39&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;&amp;amp;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;436&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;){
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;keyCode 39 is right arrow&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;      paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
      paddle.style.left &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;px&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
  }
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Instead, what we’re going to do is separate the concerns of each key, the left and right arrow, into different observables and then when we subscribe, we modify the location of the paddle in the appropriate direction.&amp;nbsp; To do this, we’ll get the keyDown observable by taking our document and calling toObservable.&amp;nbsp; Then for the left arrow handler, we’ll filter out all keys but the left arrow while we’re still in bounds, and then in the Subscribe, we move the paddle to the left by 4 pixels.&amp;nbsp; The same applies to the right arrow as well, but the difference is that we move the paddle to the right by 4 pixels.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8752be4f-415d-4fc7-9135-3e6a02970a45 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; initializeHandlers() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; keyDown &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(document).toObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;keydown&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Left arrow&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    keyDown
        .Where(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(ev) { 
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ev.keyCode&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;37&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;&amp;amp;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; 
            })
        .Subscribe(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
                paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
                paddle.css(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;left&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;px&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
            });

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Right arrow&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    keyDown
        .Where(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(ev) { 
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ev.keyCode&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;39&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;&amp;amp;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;436&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; 
            })
        .Subscribe(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
                paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
                paddle.css(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;left&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, paddleLeft &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;px&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
            });
        
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, let’s look at the overall game loop.&amp;nbsp; This sets the ball in motion so that we can react with our paddle to ensure that we hit it and continue the game until we miss and the game ends.&amp;nbsp; This function first detects if there are collisions, then renders our movement and then increases the difficulty of the game (hint: the ball moves faster).&amp;nbsp; If by the end of this, the ball is still in play, then we wait for 50 milliseconds and then start the loop all over again, else we say that the game is over.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b4d1f381-bfae-4364-8b4c-279fbaf29113 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; start() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;game loop&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    detectCollisions();
    render();
    difficulty();
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;end conditions&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(ballTop &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;470&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;){
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;still in play - keep the loop going&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        timer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; setTimeout(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;start()&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;50&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    }
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
        gameOver();
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now when it comes to porting this to the Reactive Extensions for JavaScript, we have to turn the problem on its ear a little bit.&amp;nbsp; What are we doing anyways?&amp;nbsp; At specified intervals, we’re doing a certain action which modifies our environment while our ball is in play and if it somehow isn’t, we end the game.&amp;nbsp; And just as easily as I described it above, we can write this in RxJS.&amp;nbsp; First, we start out at an interval of 50 milliseconds by calling Rx.Observable.Interval, and at each interval, we want to perform some actions, like detecting collisions and so forth that we did above.&amp;nbsp; We can accomplish that with the Do function which handles those three function calls.&amp;nbsp; Now we need to check if we’re still in bounds, so we can use the TakeWhile function to specify what is in bounds, and in this case, the top of the ball is less than 470.&amp;nbsp; And finally, once our TakeWhile no longer returns true, we’ll want to end the game, and we can do that by using the Finally function to invoke the gameOver function.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fc06b03b-e842-4015-9f71-3f92fc05676c class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; start() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;start the game loop&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;  Rx.Observable.Interval(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;50&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
      .Do(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
              detectCollisions();
              render();
              difficulty();
          })
      .TakeWhile(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ballTop &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;470&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })
      .Finally(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { gameOver(); })
      .Subscribe();
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And there you have it, we can react just the same as we did above, although we did tack on an additional 50 ms to the front end.&amp;nbsp; I’ll cover more of how we can use RxJS to break the problem into smaller building blocks and using the combinators of RxJS, put it back together.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;What we’re able to do now is to break our problems into logical pieces and by using the Reactive Extensions for JavaScript functions, we can glue them together to create composable solutions.&amp;nbsp; At some points, it can be a little difficult, just as it was for many to move from imperative looping over collections to declarative composable actions over these collections, but the reward is just as great.&lt;/P&gt;
&lt;P&gt;So with that, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Tue, 04 May 2010 23:29:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-refactoring-a-game</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Going Parallel with ForkJoin</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin</link><description>&lt;P&gt;This past week there was a new release of the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt; which includes many of the changes I’ve been talking about lately including the third-party library integration, aggregates and joins which I covered in the previous posts.&amp;nbsp; This time, I’m going to look at how we can run some observable sequence using the fork/join pattern in JavaScript, to attain some cooperative multi-tasking.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/19/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins.aspx"&gt;Join Operators&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Going Parallel with ForkJoin&lt;/H2&gt;
&lt;P&gt;One common asynchronous pattern that is often used is called fork/join where we start with a specified number of tasks and later we join together all the work.&amp;nbsp; Let’s first take a look at a simple model of what we’re trying to do.&amp;nbsp; Say we want to have the ability to load a few scripts in JavaScript?&amp;nbsp; Typically, we’d have an array of sources we want to visit and then we’d process each in turn and then return the overall result as an array.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd8cda73-9bc2-44c3-b51b-1023cc03b686 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; results &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [];
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; urls &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [url1, url2, url3];

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;for&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; task &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; tasks) {
    results.push(loadScript(tasks[task]));
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The problem with this approach is that it is a blocking scenario in that we must wait for each one to execute in turn, so the browser can do nothing else in the mean time.&amp;nbsp; Just as well, each of our operations do not depend on the other, so we could, hypothetically, run them in cooperation with each other if we had a good way of doing so.&amp;nbsp; This would easily qualify as an embarrassingly parallel solution. &lt;/P&gt;
&lt;P&gt;But how do we do it?&amp;nbsp; After all, we want to run these as non-blocking calls and we want to run them in parallel.&amp;nbsp; With the asynchronous behavior, that brings any number of issues such as error handling, cancellation checking and so forth.&amp;nbsp; Luckily, with the Reactive Extensions for JavaScript, we have that part covered with the ability to compose asynchronous operations together.&amp;nbsp; But, what about running them in parallel?&amp;nbsp; That’s where the function ForkJoin comes into play.&lt;/P&gt;
&lt;P&gt;The ForkJoin function in the Reactive Extensions for JavaScript allows us to take all observable sequences and run them in a cooperative fashion and return their first values in an array.&amp;nbsp; This function takes a variable amount of arguments, depending on how many observable sequences you have.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:59f9c2f0-32aa-4b15-b557-843b7c2d5dd9 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.ForkJoin &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    obs1,
    ...
    obsx);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Just as well, in a future version that is not out yet as of this writing will also support an array as the first argument such as:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:58a65cbf-fba4-4e74-a33b-6d363ffaa594 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.ForkJoin &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    observableArray);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now, let’s run through a quick example of using ForkJoin.&amp;nbsp; In this example, I’m going to check the flight status of a given number of flights, in case I have to coordinate a bunch of people coming into town.&amp;nbsp; To do this, I can use the Bing Instant Answer API to do that and return the answers in JSON format.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8a8b55f3-b993-40e2-93ff-e93bc0cff257 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; flightStatus(flight) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; serviceUrl &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://api.bing.net/json.aspx&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.getJSONAsObservable(
        serviceUrl,
        { appId : appId,
          Query : flight,
          Sources : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;InstantAnswer&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
          Market : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;en-us&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
          Version : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2.2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; })
        .SelectMany(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.SearchResponse.InstantAnswer.Results.toObservable();
            });

}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once we’ve defined our function to get the flight status, let’s start with a few flight numbers.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:55ce5cba-51c5-4cd3-8bbb-bacaa190689b class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; flights &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;UA123&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;UA124&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;UA125&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;];&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now, we need to take those flight numbers and then turn them into observable sequences by using the jQuery map function which takes the sequence and applies a function to each element.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:62b88114-3536-4278-9f1b-689f3d9826bc class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; flightsAsObservables &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.map(flights, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(flight) { 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; flightStatus(flight); })&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And now we can call ForkJoin to run these in parallel and return them in the order in which we sent them.&amp;nbsp; We’ll then iterate over the array of results and output the departure and arrival airport and associated times.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:793b87f7-1bc2-4fef-aafd-8c1141606813 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.ForkJoin(flightsAsObservables)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(results) {

        $.each(results, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(_,result) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; status &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.InstantAnswerSpecificData.FlightStatus;

            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#translatedText&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
                .append(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;li&amp;gt;Flight: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; status.FlightName &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; from &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; status.OriginAirport.Name &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; to &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; status.DestinationAirport.Name &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; departs at &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; status.ScheduledDeparture &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; arrives at &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; status.ScheduledArrival &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;/li&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

        });
    });
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And there you have it, we’re now able to run computations in a cooperative multi-tasking environment and return the result as an array, and in this case, getting the flight schedules for a number of flights we wish to track.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Using the Reactive Extensions for JavaScript, we’re able to run observable sequences together and get the result as an arry using a well defined pattern of fork/join in JavaScript.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Fri, 23 Apr 2010 21:17:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-going-parallel-with-forkjoin</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>[ANN] DC ALT.NET – 4/27/2010 – What has Mono done for the .NET developer lately?</title><link>https://weblogs.asp.net:443/podwysocki/ann-dc-alt-net-4-27-2010-what-has-mono-done-for-the-net-developer-lately</link><description>&lt;P&gt;As of late I haven’t posted the DC ALT.NET meetings here and instead have kept them to the &lt;A href="http://tech.groups.yahoo.com/group/dcaltnet/" mce_href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;Yahoo Groups mailing list&lt;/A&gt;.&amp;nbsp; But this month’s topic is worth sharing in that we’re going to cover everything &lt;A href="http://www.mono-project.com/Main_Page" mce_href="http://www.mono-project.com/Main_Page"&gt;Mono&lt;/A&gt; related with a member of the Mono Team, &lt;A href="http://twitter.com/jacksonh" mce_href="http://twitter.com/jacksonh"&gt;Jackson Harper&lt;/A&gt;.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;If you’re not familiar with Mono, it is an open-source, cross-platform implementation of the .NET framework based on the ECMA standards for C# and the Common Language Infrastructure. With Mono, users can run .NET applications written and compiled in Visual Studio on Windows, Linux, and Mac OS X. This session will provide an introduction to cross-platform development and deployment with Mono.&amp;nbsp; Participants will see how they can leverage their existing skills and tools to write .NET applications that will run on multiple platforms and architectures with Mono. The presentation will also include a discussion of cross-platform considerations for leveraging Mono, and demonstrate how to use MoMA, the Mono Migration Analyzer, to determine how ready an application is for cross-platform deployment.&amp;nbsp; Additional demonstrations will examine how best to leverage Visual Studio to develop and deploy to Linux and OS X, and take a peek at the current state of Moonlight 3 and 4. &lt;/P&gt;
&lt;P&gt;Hope to see you there!&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.eventbrite.com/event/661741286?ref=ebtn" target=_blank mce_href="http://www.eventbrite.com/event/661741286?ref=ebtn"&gt;&lt;IMG border=0 alt="Register for DC ALT.NET - 4/27 - What has Mono Done for .NET Developers Lately? in Alexandria, VA  on Eventbrite" src="http://www.eventbrite.com/registerbutton?eid=661741286" mce_src="http://www.eventbrite.com/registerbutton?eid=661741286"&gt;&lt;/A&gt;&lt;/P&gt;</description><pubDate>Tue, 20 Apr 2010 19:25:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/ann-dc-alt-net-4-27-2010-what-has-mono-done-for-the-net-developer-lately</guid><category>ALT.NET</category><category>Mono</category><category>User Groups</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – New Release and Joins</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins</link><description>&lt;P&gt;This past week there was a new release of the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt; which includes many of the changes I’ve been talking about lately including the third-party library integration, aggregates which I covered in the previous posts, and joins which is the subject of today’s post.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt; and &lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2.aspx"&gt;Part 2&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Joins&lt;/H2&gt;
&lt;P&gt;When we talk about joins in the Reactive Extensions for JavaScript, and for that matter in the .NET version as well, we’re not talking about the kind that we find in LINQ to Objects which matches two sequences on a given key.&amp;nbsp; Instead, what we’re talking about is to define some complex synchronization patterns between multiple event sources where we create composite events based upon combinations of other events.&amp;nbsp; In the Reactive Extensions for JavaScript, there is an implementation of &lt;A href="http://en.wikipedia.org/wiki/Join-calculus" mce_href="http://en.wikipedia.org/wiki/Join-calculus"&gt;Join calculus&lt;/A&gt; over observable sequences which allows for us to create a set of patterns that match based upon which notifications come in.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:26b0f33f-0a9d-452e-bb74-5ebcfb0d1d87 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Join &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    one to many plans);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;So, that brings us to the point, what is an execution plan exactly and what does it look like?&amp;nbsp; Well, an execution plan consists of a pattern of two functions, an And function and Then function.&amp;nbsp; Let’s first take a look at the And function, which matches when all observable sequences have a value.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d65ebee3-ad11-4bcd-b03d-c00ff4d1249f class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.And &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    other); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; The other observable to match&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can then follow this And with the Then function which matches when all observable sequences have their value available and then projects its value to another structure. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5e15e91f-2ebd-4165-8eb3-5105062567e1 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Then &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    selector); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Projects the match value&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Let’s create a simple example to show how this works.&amp;nbsp; In this example, we’ll listen for certain keys and when both of them are pressed in any given order, then we’ll bubble up and event.&amp;nbsp; We’ll listen to the keyup event and check for the C and O characters in the respective observable collections.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:89b0dae8-0aa0-41d7-bffe-3ebe86234a8a class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; cKey &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#textBox&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .toObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;keyup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(key) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; String.fromCharCode(key.keyCode) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;C&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; });
        
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; oKey &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#textBox&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .toObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;keyup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(key) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; String.fromCharCode(key.keyCode) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;O&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, we’ll create a pattern which will fire when the combination of C and O has been fired by using the Rx.Observable.Join function.&amp;nbsp; Inside the Join function, we’ll create a pattern of the cKey and the oKey together, and then project those two events together to return a string indicating it fired.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5c0bf1f8-18f8-42de-8362-a9a526c34140 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; keyJoin &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Join(
    cKey.And(oKey).Then(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(c, o) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;CO pressed!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; }));&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Then we can subscribe to the results of this join to alert when I’ve typed the letters C and O in the textbox.&amp;nbsp; Note that the two keys don’t have to be in a certain order and there can be keys in between.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:777a3f13-7e3d-4e16-8401-9d7c43d29cf6 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;keyJoin.Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) { alert(event); });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can extend this example to add another pattern in which we listen for the letters D and E in our textbox and fire a different notification.&amp;nbsp; To make that happen, we simply add another pattern to the Join method.&amp;nbsp; We can add as many Join patterns as we wish to get the right behavior our application needs.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:46cf4d54-a2ec-4f95-bca7-3f5ce9377b6b class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dKey &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#textBox&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .toObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;keyup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(key) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; String.fromCharCode(key.keyCode) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;D&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; });
        
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; eKey &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#textBox&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .toObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;keyup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(key) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; String.fromCharCode(key.keyCode) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;E&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; });
        
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; keyJoin &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Join(
    cKey.And(oKey).Then(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(c, o) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;CO pressed!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; }),
    dKey.And(eKey).Then(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d, e) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;DE pressed!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; }));        
    
keyJoin.Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) { alert(event); });    &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Let’s look at another example, this time let’s take an example where we have three pending transactions, but only really care if two of them succeed.&amp;nbsp; In this case, we’ll port one of Erik’s Microsoft Translator examples where those transactions will be going to the Microsoft Translator and then translating a given sentence into three languages, but we only care if two returns and we don’t care which.&amp;nbsp; Let’s first start off by defining our translate function using the integration with jQuery and the Reactive Extensions for JavaScript to translate some text from one language to another.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0e4313c5-da65-4a6f-bc27-7aafbbfbf036 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(from, to, text) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; appId &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someappid&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; serviceUrl &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://api.microsofttranslator.com/V2/Ajax.svc/Translate&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.getJSONAsObservable(
        serviceUrl,
        { appId : appId,
          from : from,
          to : to,
          text : text });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now we’re going to kick off the action when our translate button has been clicked. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f0b8eaeb-f5fa-473b-a760-033acc220aa5 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#translateButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .toObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Then we’re going to get the text and call the translate in the three different languages, Dutch, Spanish and French.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5515b993-4dab-4292-88d5-798017d18e8a class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; text &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#textToTranslate&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).val();

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Let's do some translating!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; from &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;en&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dutch &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(from, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;nl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, text);
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; spanish &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(from, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;es&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, text);
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; french &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(from, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;fr&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, text);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now comes the interesting part of the post.&amp;nbsp; In order for us to check for any two of our language translations to complete, we need three different execution plans, one for Dutch and Spanish, another for Dutch and French and finally one for Spanish and French.&amp;nbsp; To create those execution plans, we’ll take our first translation observable and call And, passing in the other translation observable, and then finally call the Then function which projects the first and second language.&amp;nbsp; In the Then function, we’ll project a structure which contains properties for all three languages but two get the translation and the other gets a failure message. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c51efeb9-2137-4dbd-b391-155ebd0c3cfb class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Joins&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; fail &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;FAIL!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; results &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Join(
    dutch.And(spanish).Then(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d, s)  { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { dutch : d.data, spanish : s.data, french : fail };}),
    dutch.And(french).Then(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d, f)   { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { dutch : d.data, spanish : fail, french : f.data};}),
    french.And(spanish).Then(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(f, s) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { dutch : fail, spanish : s.data, french : f.data};}));&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we created this set of patterns, we can then subscribe via the aptly named Subscribe method which passes our structure with the properties for all three languages.&amp;nbsp; We’ll take that structure and print out the value of each translation, whether it succeeded or not. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4fe4f985-14dc-450b-bbda-e90523c01192 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;results.Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#spanish&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Spanish: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.spanish);
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#french&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;French: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.french);
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#dutch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Dutch: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.dutch);
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now, let’s take our code and check it out in the browser.&amp;nbsp; Once I enter a phrase I wish to translate and click the Translate! button, you can see that both Spanish and Dutch return their value, but French does not. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_29291774.png" mce_href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_29291774.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_3DAE66F2.png" width=463 height=224 mce_src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_3DAE66F2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And then I could press the button again, and sure enough, this time Spanish fails while both Dutch and French translations return a proper value.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_2B65A030.png" mce_href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_2B65A030.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_6B2F86B5.png" width=436 height=252 mce_src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_6B2F86B5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;So, as you can see, this gives us some pretty interesting possibilities to create execution plans &lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;As you can see, with the Aggregates library addition of the Reactive Extensions for JavaScript, we have yet another tool in our belt for handling our data.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;</description><pubDate>Tue, 20 Apr 2010 03:07:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-new-release-and-joins</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Aggregation Part 2</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2</link><description>&lt;P&gt;So far we’ve come a long way in this series on the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt;, starting with some of the basics, and then going deeper with composition with such things as the Bing Maps and Twitter Mashup, Drag and Drop, asynchronous programming among others.&amp;nbsp; In the previous post, we talked about separating our concerns between the base functionality, DOM events and third-party library integration.&amp;nbsp; This time, let’s follow onto our last post which started talking about the Aggregates library of the Reactive Extensions for JavaScript&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/14/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1.aspx"&gt;Aggregates – Part 1&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Aggregation Operators&lt;/H2&gt;
&lt;P&gt;In the initial release of the Reactive Extensions for JavaScript, you may have noticed there was a lack aggregation operators.&amp;nbsp; By aggregation operators I mean those which require us to iterate over the collection with some sort of accumulated value to produce a final answer.&amp;nbsp; The Reactive Extensions for JavaScript have included a number of operators which we’ll go through one by one including:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Aggregate &lt;/LI&gt;
&lt;LI&gt;Any &lt;/LI&gt;
&lt;LI&gt;All &lt;/LI&gt;
&lt;LI&gt;Contains &lt;/LI&gt;
&lt;LI&gt;Count &lt;/LI&gt;
&lt;LI&gt;Sum &lt;/LI&gt;
&lt;LI&gt;Average &lt;/LI&gt;
&lt;LI&gt;Final &lt;/LI&gt;
&lt;LI&gt;Min/MinBy &lt;/LI&gt;
&lt;LI&gt;Max/MaxBy &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;To use these, simply add a reference to the Rx.aggregates.js library like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:caf48f10-c107-484c-9c25-657253e4ab9a class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="rx.aggregates.js"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now to cover some of the functions.&amp;nbsp; We’ll start with the Contains function, picking things up where we left off last time.&lt;/P&gt;
&lt;H2&gt;Contains&lt;/H2&gt;
&lt;P&gt;The first method we’ll cover is the Contains function, which determines whether a given observable sequence contains the item, and optionally with a comparer which allows us to compare values for equality.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cff5e99a-2d33-48ec-a77d-9ec3a02c3e93 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Contains &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    value,     &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Value to locate in the sequence&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    comparer); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Optional comparer to compare values&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;If you fail to include a comparer, the default comparer used looks like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b66cc930-795a-43f9-8b46-d2ea07d9dd88 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; defaultComparer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(a, b) { 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; a &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; b 
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In this example, I’ll have an observable collection of employees and I wish to check whether Bob is an employee.&amp;nbsp; In this case, I’ll provide a comparer which checks the name property on our object for equality.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:db97849e-637d-4fc6-aa5b-b4937afe7cc3 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Array.prototype.toObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; employees &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [{ name : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Bob&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, age : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;30&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; },
                 { name : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Billy&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, age : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;32&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; }]

employees.toObservable()
    .Contains({ name : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Bob&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; }, 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(a, b) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; a.name &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; b.name; })
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(contains) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Contains Bob?: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; contains);
    });
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This of course produces that it can in fact find an employee named Bob in the collection.&amp;nbsp; Moving on to the next function, Count.&lt;/P&gt;
&lt;H2&gt;Count&lt;/H2&gt;
&lt;P&gt;The count function in the Reactive Extensions for JavaScript simply returns the number of items in our observable collection.&amp;nbsp; The signature of this method is as follows:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:174caa1f-36f2-48ac-9c72-b67c2460ef2b class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Count &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;As an example, we can take an array of numbers, get the distinct values, count them and display the results.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:380237c7-c3e4-484a-bb4e-8a952fef231c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; factors &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;3&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;5&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;5&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ];

factors
    .toObservable()
    .DistinctUntilChanged()
    .Count()
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(count) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Distinct factors: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;count);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This then gives us the result of 3 distinct factors as shown below.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:c24b2a0f-880a-465c-b927-0d3675bd5ff2 class=wlWriterSmartContent&gt;&lt;PRE class="brush: jscript; gutter: false; first-line: 1; tab-size: 4;  toolbar: true; "&gt;Distinct factors: 3
&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next up, we’ll talk about the Sum function.&lt;/P&gt;
&lt;H2&gt;Sum&lt;/H2&gt;
&lt;P&gt;This rather self-explanatory method simply computes the sum of all items in the observable collection.&amp;nbsp; As JavaScript is a dynamic language, this method supports anything that supports the + operator, typically integers and doubles, although oddly enough it does work for strings (which is not recommended).&amp;nbsp; The signature of the function is as follows:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f4e69130-6023-4677-82d3-6a04265032ac class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;proto.Sum &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In this example, we’ll simply take some floating point numbers and sum them together.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7892e766-789f-4aa5-bda7-a197b2fb7cfd class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; numbers &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;25.78&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;5.72&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1.5&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;3.2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ];

numbers
    .toObservable()
    .Sum()
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(summed) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(summed);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And the result of our action then puts the 36.2 into the result div element.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0992b9ce-94a7-4921-b85d-c10dcbb10ecf class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;36.2&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, we’ll tackle the Average function.&lt;/P&gt;
&lt;H2&gt;Average&lt;/H2&gt;
&lt;P&gt;Just as you’d expect, we have the Average function which does as you expect, averages numbers.&amp;nbsp; This function takes no arguments and simply returns the average.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b552a8da-811a-4bed-869a-cc83bc711c04 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Average &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can take our above example and instead of summing the values, this time we’ll average them.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d8c86cad-3cd3-4700-8acd-a0444ba7f75c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; numbers &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; [ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;25.78&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;5.72&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1.5&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;3.2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ];

numbers
    .toObservable()
    .Average()
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(average) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(average);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Which then yields the result of:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:737b0474-bb8b-43bc-87ce-d8ba0382215b class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;9.05&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Moving on, let’s talk about the Final method.&lt;/P&gt;
&lt;H2&gt;Final&lt;/H2&gt;
&lt;P&gt;The aptly named Final method gives us the ability, much like the Last method in LINQ to Objects, to retrieve the final value from the observable sequence.&amp;nbsp; The signature of this method is as follows:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6c83ccc0-10ed-45de-a036-16040955f00c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Final &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Moving away from our same numbers example we had from above, instead we can turn our attention to Twitter and more in particular, FourSquare users.&amp;nbsp; In this example, I’ll search Twitter for 4sq in their tweet, take the results array and split each one into its own observable sequence and then get the last one and display the text.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1efd7e68-7bad-4de7-8697-cc90043426cc class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; term &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

$.getJSONAsObservable(url, { rpp : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q : term })
    .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results.toObservable(); })
    .Final()
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(final) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(final.text);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Let’s move on to our last two functions, Min and Max.&lt;/P&gt;
&lt;H2&gt;Min&lt;/H2&gt;
&lt;P&gt;The next function, which should also look familiar to LINQ to objects users is the Min function.&amp;nbsp; This function, which optionally takes a key selector and min comparer, computes the minimum value in the sequence.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:53dd7241-30bc-48ff-b716-0cf55c1bb91d class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.Min &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    keySelector, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Optional key selector &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    comparer);   &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Optional min comparer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;If a comparer is not provided, the default comparer uses the standard less than operator.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e507bbe6-9f06-4863-b6db-a08b32834b62 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;comparer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(current, key) { 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; key &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; current; 
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;To put this one to the test, let’s search Twitter again, this time, let’s look for the minimum user ID in this batch of tweets.&amp;nbsp; We’ll use the JSON jQuery integration with the Reactive Extensions for JavaScript, extract the results and then return the minimum by the from_user_id property.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ae4c96c4-70ab-40e8-a6db-7239861f4ba5 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; term &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

$.getJSONAsObservable(url, { rpp : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q : term })
    .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results.toObservable(); })
    .Min(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.from_user_id; })
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(min) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(min.from_user_id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;min.text);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Finally, we come to the Max function.&lt;/P&gt;
&lt;H2&gt;Max&lt;/H2&gt;
&lt;P&gt;The Max function is the inverse of the Min function and allows us to calculate the maximum value in our observable sequence.&amp;nbsp; Optionally, we can give a key selector and a max comparer.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4873c5b8-5cc0-42cd-a79f-131a8d99c441 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;proto.Max &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    keySelector, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Optional key selector&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    comparer);   &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Optional max comparer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;If a max comparer is not provided, the default should work just fine.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb8c0c12-84cb-4de4-8a7b-4d24c569a90c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;comparer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(current, key) { 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; key &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; current; 
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In our last example, let’s look for the maximum Twitter user ID that someone is responding to.&amp;nbsp; Much like above we’ll use the jQuery integration with the Reactive Extensions for JavaScript to get the JSON data, we’ll then filter by whether there is a person the user is responding to, and then get that max value of that user ID.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:dc77da97-3616-440b-933f-4d4442ec47b4 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; term &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

$.getJSONAsObservable(url, { rpp : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q : term })
    .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results.toObservable(); })
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.to_user_id &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })
    .Max(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.from_user_id; })
    .Subscribe(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(min) {
            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(min.from_user_id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;min.text); });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And there you have it, the complete set of operators handled in the aggregates library for the Reactive Extensions for JavaScript.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;As you can see, with the Aggregates library addition of the Reactive Extensions for JavaScript, we have yet another tool in our belt for handling our data.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Thu, 15 Apr 2010 02:08:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-2</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Aggregation Part 1</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1</link><description>&lt;P&gt;So far we’ve come a long way in this series on the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt;, starting with some of the basics, and then going deeper with composition with such things as the Bing Maps and Twitter Mashup, Drag and Drop, asynchronous programming among others.&amp;nbsp; In the previous post, we talked about separating our concerns between the base functionality, DOM events and third-party library integration.&amp;nbsp; This time, let’s look at another separation which comes in the form of aggregates.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/09/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns.aspx"&gt;A Separation of Concerns&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Aggregation Operators&lt;/H2&gt;
&lt;P&gt;In the initial release of the Reactive Extensions for JavaScript, you may have noticed there was a lack aggregation operators.&amp;nbsp; By aggregation operators I mean those which require us to iterate over the collection with some sort of accumulated value to produce a final answer.&amp;nbsp; The Reactive Extensions for JavaScript have included a number of operators which we’ll go through one by one including:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Aggregate &lt;/LI&gt;
&lt;LI&gt;Any &lt;/LI&gt;
&lt;LI&gt;All &lt;/LI&gt;
&lt;LI&gt;Contains &lt;/LI&gt;
&lt;LI&gt;Count &lt;/LI&gt;
&lt;LI&gt;Sum &lt;/LI&gt;
&lt;LI&gt;Average &lt;/LI&gt;
&lt;LI&gt;Final &lt;/LI&gt;
&lt;LI&gt;Min/MinBy &lt;/LI&gt;
&lt;LI&gt;Max/MaxBy &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;To use these, simply add a reference to the Rx.aggregates.js library like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:61de6fb3-1966-4a46-8020-806be2663376 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="rx.aggregates.js"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now to cover some of the functions.&amp;nbsp; Let’s start off with the Aggregate function.&lt;/P&gt;
&lt;H2&gt;Aggregate&lt;/H2&gt;
&lt;P&gt;The aggregate function gives us the ability to apply an accumulator function over our observable sequence and the seed acts as the initial accumulator value and then yields to us a single value.&amp;nbsp; For those coming from the functional programming world, one could think of this as a left fold.&amp;nbsp; For a quick refresher on what a fold is, take a look at Bart de Smet’s &lt;A href="http://community.bartdesmet.net/blogs/bart/archive/2008/08/17/folding-left-right-and-the-linq-aggregation-operator.aspx" mce_href="http://community.bartdesmet.net/blogs/bart/archive/2008/08/17/folding-left-right-and-the-linq-aggregation-operator.aspx"&gt;post on the matter&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;This function should be familiar to you who already use LINQ and the semantics are exactly the same.&amp;nbsp; Except, there is one large difference in that all of these aggregation functions don’t return just a value as they do in LINQ to objects, but instead an IObservable&amp;lt;someObject&amp;gt;, so in order for us to get at the value, we must subscribe to it.&lt;/P&gt;
&lt;P&gt;Below is the signature of the Aggregate function.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a152483d-a0a3-4433-b74a-db54d4485a6d class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Aggregate &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    seed,             &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Initial value&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    accumulator);     &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Function to be invoked on each element&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can then use this function, for example, to count the number of vowels in a given piece of text.&amp;nbsp; We have an initial seed of an object with properties for each vowel, a e i o u.&amp;nbsp; Then we have an accumulator function when then compares our letters and increments the accumulator properly by incrementing the right property should there be a match on a given vowel.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:781f89ce-0180-43e3-80dd-df8262e8e00a class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Array.prototype.toObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; countVowel(state, letter) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; letter.toLowerCase();
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;a&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.extend(state, { a : state.a &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; });
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;e&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.extend(state, { e : state.e &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; });
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;i&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.extend(state, { i : state.i &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; });
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;o&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.extend(state, { o : state.o &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; });
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;===&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;u&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.extend(state, { u : state.u &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; });

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; state;
}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; initial &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
    a : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    e : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    i : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    o : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
    u : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; };

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sentence &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Are you ready to hack some JavaScript&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

sentence.split(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;""&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).toObservable()
    .Aggregate(initial, countVowel)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(counted) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Number of E's: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; counted.e);
    });
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The result of our labor gives us that it found three E’s in the sentence.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:62f41f68-7434-44fb-96bc-03ce89cc9022 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Number of E's: 3&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We also have another version of the Aggregate function which takes no seed at all and instead just an accumulator.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0e84d6d6-418e-4b4e-804a-e9dce15544b2 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Aggregate &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    accumulator);      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Function to be invoked on each element&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In this example, we’ll take a sentence and reverse it by splitting it by whitespace and then inside of our accumulator function, we put the word in front of the rest of the sentence as we go along.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8e0ee29e-3ef7-4fe3-a681-e9147605782b class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sentence &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Are you ready to hack some JavaScript&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

sentence.split(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).toObservable()
    .Aggregate1(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(acc, word) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; word &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; acc; })
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(reversed) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#result&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(reversed);
    });
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This should then render the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0f6b03f2-c2f5-4fbb-86d0-fc303fc27d1a class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;JavaScript some hack to ready you Are &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we’re familiar with Aggregate, let’s move on to determining whether we satisfy a given condition&lt;/P&gt;
&lt;H2&gt;Any&lt;/H2&gt;
&lt;P&gt;The next LINQ operator that is in the Aggregates library is the Any function.&amp;nbsp; This function has two overloads, the first determines whether there are any items in the collection.&amp;nbsp; This function takes no arguments and looks like the following.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d73ceb54-5fac-4102-9e5f-cd6b5085d889 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Any &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can then use this to then determine if we have any items in our collection.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:737ff82d-b5d6-40eb-afb3-ebc7114c920a class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; items &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray([&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;3&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]);

items.Any().Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(any) {
    alert(any); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; alerts true&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Just as well, we could use the overload of the Any operator to determine whether any item in the observable sequence match a given predicate.&amp;nbsp; The predicate function takes in the item from list and then returns a boolean to determine whether there is a match.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:37b45667-abf9-408b-928d-4f62fe8ac2a3 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Any &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    predicate); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; predicate to determine if any satisfies&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In this example, we’ll determine whether any of the strings inside of an array have the letters “ie” in them.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:55967673-e9ac-4035-87dd-e3e5c1c78a04 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; words &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(
    [ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;belief&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;height&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;yield&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;cashier&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ]);

words
    .Any(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(word) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; word.indexOf(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;ie&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(any) {
        alert(any); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; yields true&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;H2&gt;All&lt;/H2&gt;
&lt;P&gt;The last one we’re going to cover today is the All function.&amp;nbsp; This function determines whether all elements of an observable sequence satisfy a given predicate.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6ceeddfe-1d82-4c84-aeee-c504536ee826 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;All &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(
    predicate); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; predicate to determine if all satisfy&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;To show you quickly how it works, I’ll take the example from above and rewrite it to use the All function instead of the Any and this time because not all the words have the “ie” letter combination, I should have false returned to me when I subscribe.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:991a9324-6a6d-4819-bf9b-9562d1191743 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; words &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(
    [ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;belief&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;height&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;yield&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;cashier&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ]);

words
    .All(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(word) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; word.indexOf(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;ie&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(any) {
        alert(any); &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; yields false&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;&amp;nbsp;&amp;nbsp; 
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;I think that’s enough for now, and we have a bit to cover including:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Contains&lt;/LI&gt;
&lt;LI&gt;Count&lt;/LI&gt;
&lt;LI&gt;Sum&lt;/LI&gt;
&lt;LI&gt;Average&lt;/LI&gt;
&lt;LI&gt;Final&lt;/LI&gt;
&lt;LI&gt;Max&lt;/LI&gt;
&lt;LI&gt;Min&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;As you can see, with the Aggregates library addition of the Reactive Extensions for JavaScript, we have yet another tool in our belt for handling our data.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Wed, 14 Apr 2010 05:49:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-aggregation-part-1</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – A Separation of Concerns</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns</link><description>&lt;P&gt;So far we’ve come a long way in this series, starting with some of the basics, and then going deeper with composition with such things as the Bing Maps and Twitter Mashup, Drag and Drop, asynchronous programming among others.&amp;nbsp; In the previous post, we covered a bit about jQuery AJAX integration into the Reactive Extensions for JavaScript.&amp;nbsp; This time, we’re going to step back just a little bit to talk about de-cluttering the libraries and separating our concerns.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/04/02/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax.aspx"&gt;jQuery AJAX integration&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;A Clear Separation of Concerns&lt;/H2&gt;
&lt;P&gt;In an earlier post, we talked a bit about jQuery event integration using bind/unbind as part of the first release of the Reactive Extensions for JavaScript.&amp;nbsp; Since that time, we’ve also added the Live Events and AJAX among other features.&amp;nbsp; Just as well, we’ve looked at other libraries such as Dojo, Prototype, YUI3, Moo Tools, ExtJS among others and sought integration points as well.&amp;nbsp; The question then comes up, how do we arrange the libraries in an easy manner so that we have the core functionality in one library, and then the integration points with both the DOM and third-party libraries integrations?&lt;/P&gt;
&lt;P&gt;What the team has decided to do is split among just those lines to have a separation between the core Rx functionality of Observers, Observables, and the various combinators, and then split out anything DOM or third-party library related into their own respective packages.&amp;nbsp; What you can expect are the following then in the next release of the Reactive Extensions for JavaScript, in no particular order:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;DOM Events (IE and DOM) and XmlHttpRequest&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://jquery.com/" mce_href="http://jQuery.com"&gt;jQuery&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://dojotoolkit.org/dojo" mce_href="http://dojotoolkit.org/dojo"&gt;Dojo&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.extjs.com/" mce_href="http://www.extjs.com/"&gt;ExtJS&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://prototypejs.org/" mce_href="http://prototypejs.org/"&gt;Prototype&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://developer.yahoo.com/yui/3/" mce_href="http://developer.yahoo.com/yui/3/"&gt;YUI3&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.mootools.net/" mce_href="http://www.mootools.net/"&gt;Moo Tools&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;In addition to the integration points, the library will also ship with samples on how you can integrate the Reactive Extensions for JavaScript with your library of choice.&amp;nbsp; For example, here is a quick demo of using Dojo and the Reactive Extensions together to bind and unbind events to buttons.&amp;nbsp; The intent of this example is to have two buttons that control whether a third button can in fact listen to events.&amp;nbsp; The first button hooks the events and changes the text of the button, whereas the second button unhooks the listener from the third button.&amp;nbsp; To do this, we’ll need a MutableDisposable which allows us to replace the disposable at any time with a new one.&amp;nbsp; In this case, we’ll replace a blank disposable with one that comes from the subscription to the third button’s click event.&amp;nbsp; Then in the second button handler, we’ll dispose our MutableDisposable and then initialize it again to a new one.&amp;nbsp; There are plenty of built-in Disposable instances that we’ll cover in the near future.&amp;nbsp; Below is the full implementation using Dojo and RxJS together.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c66fc833-915a-4a04-9aa1-2d4f801dab31 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; 
            type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="..\rx.js"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="..\rx.dojo.js"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;

        dojo.addOnLoad(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;() {

            &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; liveEvent &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; Rx.MutableDisposable();
            Rx.Observable.FromDojoEvent(dojo.byId(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;bind&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;),&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;)
                .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; () {
                    dojo.byId(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;theone&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;).value &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;can click...&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;;

                    liveEvent.Replace(
                        Rx.Observable.FromDojoEvent(dojo.byId(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;theone&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;),&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;)
                        .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;() {
                            alert(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;hello&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;);  }));
                    });

            Rx.Observable.FromDojoEvent(dojo.byId(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;unbind&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;), &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;)
                .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; () {
                        dojo.byId(&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;theone&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;).value &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;Does nothing...&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;;
                        liveEvent.Dispose();
                        liveEvent &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; Rx.MutableDisposable();
                    });
        });
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;button &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="theone"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Does nothing...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;button&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;button &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="bind"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Bind Click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;button&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;button &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="unbind"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Unbind Click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;button&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;By providing not only the libraries as well as the samples, the team is able to show you how you can hook up RxJS into your own libraries as well and not be limited to what has already been provided out of the box.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;By separating the concerns of the core functionality of RxJS and DOM and third party library integration, the team has given fine grained control on how you can integrate it into your libraries.&amp;nbsp; And if there are other libraries that you think should be integrated out of the box, do give feedback!&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Fri, 09 Apr 2010 05:02:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-a-separation-of-concerns</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Extending jQuery AJAX</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax</link><description>&lt;P&gt;So far we’ve come a long way in this series, starting with some of the basics, and then going deeper with composition with such things as the Bing Maps and Twitter Mashup, Drag and Drop, asynchronous programming among others.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;One of the nicest aspects so far is the ease of integration into jQuery.&amp;nbsp; For the first release, we have Observable.FromJQueryEvent, but we didn’t cover such things as Live Events, which are coming soon as well as other such calls that we’ll get into in today’s post.&amp;nbsp; But adding all of this could tend to clutter up the main Reactive Extensions for JavaScript core functionality, so instead, we’ll break out the functionality into the core library and then have extensions in other files to cover DOM events, jQuery integration and so on.&amp;nbsp; We’ll go into more details about what those other libraries are in subsequent posts.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/21/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration.aspx"&gt;jQuery Live Event integration&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Extending jQuery AJAX Calls&lt;/H2&gt;
&lt;P&gt;In the previous post, we covered not only the jQuery event integration with the bind and unbind, but also support for integration with live events.&amp;nbsp; Outside of events and live events, there are other ways we could integrate the Reactive Extensions for JavaScript and jQuery, including all functions which take some form of a callback. &lt;/P&gt;
&lt;P&gt;For example, we could look at wrapping the jQuery AJAX API to take advantage of RxJS and that’s precisely what we’ve done is to include the AJAX wrappers in an upcoming Reactive Extensions for JavaScript release.&amp;nbsp; To give you an idea of what we’ve done, let’s first look at the jQuery function getJSON, which allows us to load JSON encoded data from a given URL using an HTTP GET request.&amp;nbsp; This function takes in a URL, an optional map or string of data to be sent with the request, and finally a callback with the data and the text status should it succeed.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c877106e-952f-42ac-9058-acd450c558c8 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;jQuery.getJSON( 
    url, 
    [ data ], 
    [ callback(data, textStatus) ] )&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We can use this function to, for example, to call the Twitter search API to look for users of FourSquare, iterate through the results and put each result in an unordered list.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e151c916-6053-4b7a-b250-ccb1e9276749 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; term &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

$.getJSON(url, { rpp : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q : term }, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) {

    $.each(d.results, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(_, result) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#twitterList&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
            .append(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;li&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.created_at &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
                             result.from_user &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
                             result.text &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;/li&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    });
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;When we’re using the getJSON function though, what we’re really doing is calling the underlying ajax function which takes a settings map which contains our URL, the data and a success continuation.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b568590f-5672-48f6-aec6-2193894e5b10 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$.ajax({
  url: url,
  dataType: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,
  data: data,
  success: success
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This is great for simple examples, but what about error handling?&amp;nbsp; Unfortunately, as written, it does not handle such scenarios.&amp;nbsp; Instead, we could, through the use of the Reactive Extensions for JavaScript, rewrite the AJAX functionality so that we get composable behavior with these callback scenarios.&amp;nbsp; To do this, we could wrap not only the success function, but the error as well and handle each appropriately.&amp;nbsp; We’ll concern ourselves with two functions on our settings maps, the success and the error.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The success function is a function which is called upon the successful completion of the AJAX operation.&amp;nbsp; This function gets passed three arguments, the data, the text status and the XMLHttpRequest object and looks much like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:097420ec-f0b4-49e9-8959-f749934e2259 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;success(
    data, 
    textStatus, 
    XMLHttpRequest) &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;On the other side of the story, we have the error function which is called when our AJAX call fails.&amp;nbsp; This function gets passed three arguments as well, the XMLHttpRequest object, the text describing the error, and optionally the error object itself.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8c812f34-27d4-4c72-8719-c541646d121d class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;error(
    XMLHttpRequest, 
    textStatus, 
    errorThrown) &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have a basic understanding of the two functions we’re interested in as part of our settings object, let’s look how we rewrote the ajax function as an observable, in this case calling it ajaxAsObservable.&amp;nbsp; We’ll take the take a settings map much like the original ajax function does, copy out the values into a new map and add the success and error handlers.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e538071d-4ab0-49a5-9c0d-3c2a67f06677 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$.ajaxAsObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(settings)
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; internalSettings &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {};
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;for&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; setting &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; settings) {
        internalSettings[setting] &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; settings[setting];
    }
   
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; subject &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.AsyncSubject();
    internalSettings.success &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data, textStatus, xmlHttpRequest) {
        subject.OnNext({ 
            data: data, 
            textStatus: textStatus, 
            xmlHttpRequest: xmlHttpRequest });
       subject.OnCompleted();
    };
   
    internalSettings.error &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(xmlHttpReuqest, textStatus, errorThrown) {
        subject.OnError({ 
            xmlHttpRequest: xmlHttpRequest, 
            textStatus: textStatus, 
            errorThrown: errorThrown });
    };
   
    $.ajax(internalSettings);
   
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; subject;
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see, it’s a fairly common approach for us to use the AsyncSubject to wrap a lot of these APIs.&amp;nbsp; We also defined a shorthand function just as jQuery did with getJSON aptly named getJSONAsObservable.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:01a320bd-a870-4917-a900-56f213c11ca3 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$.getJSONAsObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(url, data)
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $.ajaxAsObservable({ 
        url: url, 
        dataType: &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, 
        data: data });
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now to show this in action, we could create an observable which searches Twitter for FourSquare users and only those with geolocation enabled, display the tweeted text as well as their latitude and longitude.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:268d75d2-0738-4ea5-aab3-6b2648fc9860 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Array.prototype.toObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromArray(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; term &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;4sq&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

$.getJSONAsObservable(url, { rpp : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, q : term })
    .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(d) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; d.data.results.toObservable(); })
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.geo &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; lat &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.geo.coordinates[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;];
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; lon &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.geo.coordinates[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;];

        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#twitterList&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).append(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;li&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            result.created_at &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            result.from_user &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; : &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            result.text &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;br /&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;at &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; lat &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; - &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; lon &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;/li&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;As you may have noticed, not once did we actually use naming of Rx.Observable naming, and instead we’re focusing more on integrating naturally with jQuery including the naming as well as extending the jQuery object itself.&amp;nbsp; Your thoughts and feedback on these designs are always appreciated.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Combining jQuery and the Reactive Extensions for JavaScript gives us even more options to deal with not just events but AJAX callbacks as well.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Fri, 02 Apr 2010 23:17:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-extending-jquery-ajax</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Time Flies Like An Arrow in F# and the Reactive Extensions for .NET</title><link>https://weblogs.asp.net:443/podwysocki/time-flies-like-an-arrow-in-f-and-the-reactive-extensions-for-net</link><description>&lt;P&gt;In the past week, I had the pleasure of speaking on Reactive Programming in F# with &lt;A href="http://lorgonblog.spaces.live.com/Blog/" mce_href="http://lorgonblog.spaces.live.com/Blog/"&gt;Brian McNamara&lt;/A&gt; at a conference out in Seattle.&amp;nbsp; The point of this talk was to cover the what and why of using F# in reactive programming on both the client and the server and showed quite a few examples.&amp;nbsp; &lt;A href="http://lorgonblog.spaces.live.com/Blog/cns!701679AD17B6D310!1842.entry" mce_href="http://lorgonblog.spaces.live.com/Blog/cns!701679AD17B6D310!1842.entry"&gt;One of the samples Brian&lt;/A&gt; alluded to, the “Time Flies Like An Arrow” example, which is an example of having a stream of text, each character delayed behind the previous, follow the mouse around the screen.&amp;nbsp; Brian’s version used a combination of both the asynchronous workflows which are a standard part of F#, as well as first class events/observables.&amp;nbsp; For my version, I’m going to strictly use F# first class events and the integration with the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for .NET&lt;/A&gt; to show you how it can be done.&lt;/P&gt;
&lt;H2&gt;Implementing Using F#&lt;/H2&gt;
&lt;P&gt;Let’s take a look at what would be required for us to make the Time Flies Like an Arrow example work in F# using first class events and the Reactive Extensions for .NET on top of WPF.&amp;nbsp; I’ll write it in a pseudo-code to give you an idea of how it should be done.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a173bb93-f25c-4ed1-b755-2b8fd90e5201 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;text &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;F# Reacts to First Class Events!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

for character in text

  textbox &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; TextBox with text &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; character
  Add textbox to canvas
  
  MouseMove
  - Get Position relative to canvas
  - Delay by character position * &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
  - Observe on the dispatcher thread
  - Set the textbox top to the y position of the mouse
  - Set the textbox left to x position of the mouse
    plus character position * &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;10&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Luckily, using F#, our code shouldn’t be much more complicated than that.&amp;nbsp; With this pseudo-code in mind, let’s get started.&amp;nbsp; First, we’ll need an additional two combinators that the F# Observable module does not include, which is the ability to delay and observe on the dispatcher thread.&amp;nbsp; These combinators are simple pass-throughs to the Reactive Extensions which allow us to have a nice pipelining for creating our events.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:87a48055-fd5b-424b-961b-b7b5d1ecd723 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;module&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;let&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; delay (span : TimeSpan) (observable : IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;T&amp;gt;) =&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    observable.Delay(span)

  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;let&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observeOnDispatcher (observable : IObservable&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;T&amp;gt;) =&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    observable.ObserveOnDispatcher()&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, we’ll need to be able to get the position of our mouse in relation to a given UIElement, and this case, it will be the main canvas.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3f764941-b049-4c3d-8005-666900d600c6 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;let&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getPosition (element : #UIElement) (args : MouseEventArgs) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;let&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; point &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; args.GetPosition(element)
  (point.X, point.Y)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have some of the helpers defined, we can define our WPF Window.&amp;nbsp; In this code, I’ll create a canvas and add it as the content to our Window.&amp;nbsp; Then I’ll implement the pseudo-code from above into F# to iterate through the characters, create a textbox for each and then set the mouse move to move our text box when there is a reaction.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a8829206-4ad1-43de-96d0-28f1e440496e class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; TimeFliesWindow() as this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
  inherit Window()

  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;do&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; this.Title &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Time files like an arrow&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;let&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; canvas &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Canvas(Width&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;800.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, Height&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;400.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, Background &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Brushes.White) 
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;do&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; this.Content &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; canvas

  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;do&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;F# can react to first class events!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Seq.iteri(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;fun&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; i c &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;-&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;  
          &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;let&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; s &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; TextBlock(Width&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;20.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, 
                            Height&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;30.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, 
                            FontSize&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;20.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, 
                            Text&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; c, 
                            Foreground&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Brushes.Black, 
                            Background&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Brushes.White)
          canvas.Children.Add(s) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ignore

          this.MouseMove
          &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable.map(getPosition canvas)
          &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable.delay (TimeSpan.FromMilliseconds(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;float&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; i &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;*&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;100.0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;))
          &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable.observeOnDispatcher
          &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Observable.subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;fun&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (x, y) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;-&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
                 Canvas.SetTop(s, y) 
                 Canvas.SetLeft(s, x &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;float&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ( i &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;*&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800080"&gt;10&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)))
          &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;|&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ignore)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And there you have it, a rather simple example of using F# First Class Events which shows off the composable nature of events and what interesting things we can do with them.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_722AE9DA.png" mce_href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_722AE9DA.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; WIDTH: 536px; DISPLAY: inline; HEIGHT: 379px; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_63803DF5.png" width=536 height=379 mce_src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_63803DF5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Some point in the near future, I’ll come back to explain asynchronous workflows on the client to give further examples on where F# fits.&amp;nbsp; F# is a great language for writing non-blocking I/O computations.&amp;nbsp; Using the rich F# First Class events, which are also IObservable&amp;lt;T&amp;gt; instances, we can use both what F# has to offer as well as the integration with the Reactive Extensions for .NET to create even richer experiences.&amp;nbsp; &lt;/P&gt;</description><pubDate>Mon, 29 Mar 2010 02:28:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/time-flies-like-an-arrow-in-f-and-the-reactive-extensions-for-net</guid><category>Event-based Programming</category><category>F#</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – jQuery Live Event Integration</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration</link><description>&lt;P&gt;So far we’ve come a long way in this series, starting with some of the basics, and then going deeper with composition with such things as the Bing Maps and Twitter Mashup, Drag and Drop, asynchronous programming among others.&amp;nbsp; One of the nicest aspects so far is the ease of integration into jQuery.&amp;nbsp; For the first release, we have Observable.FromJQueryEvent, but we didn’t quite cover live events and the integration points that we can extend.&amp;nbsp; Let’s cover that today and what is coming in a future release of the Reactive Extensions for JavaScript.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/19/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop.aspx"&gt;Drag and Drop&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;jQuery Live Event Integration&lt;/H2&gt;
&lt;P&gt;In an earlier post, we covered the existing jQuery event integration with the bind and unbind scenario.&amp;nbsp; This is the primary way in jQuery to attach an event handler to existing matched elements such as the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:055666c3-e9ae-42fb-8276-85ea3672a765 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).bind(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).attr(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we could disable the binding such as the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:58bc4f05-e578-4e4a-a6f3-b171799aaee6 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).unbind(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we added a nice abstraction over this to enroll it as an observable via the Rx.Observable.FromJQueryEvent method and the extension to the jQuery fn itself of a ToObservable method.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:293fa489-dc7c-4dc7-b567-0e645348933b class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; FromJQueryEvent&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.FromJQueryEvent($(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;), &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;))
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).attr(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    });    


&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Or extending jQuery object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#someButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).attr(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;But what about if we want to attach a handler to an event for all elements which match the selector both now and in the future?&amp;nbsp; For example, if we add elements after we have called bind on a matching selector, those will not be bound to the handler, and instead would have to rebind.&amp;nbsp; In order for us to get the behavior of attaching to all matching selectors both now and in the future, we’ll use the live function which does exactly that.&amp;nbsp; In this case, we’ll add a a click event handler to all present and future items that have the formButton class which disables the element.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c1380835-0431-4ee2-8e91-0be1809a925a class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.formButton&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).live(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
     $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).attr(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;disabled&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
});

$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .append(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;input type="button" class="formbutton" value="Click me!"/&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we’ve covered how to live bind handlers to events, what about unbinding live events?&amp;nbsp; To do that, we’ll use the die function which removes all event handlers that we’ve attached using the live function.&amp;nbsp; This example, taking from the documentation, shows a button bound to click which then live binds to the click handler of the main button.&amp;nbsp; We also bind the click handler of the unbind button which then unbinds the live handler from the main button.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:05ff11f3-8d94-414d-b921-cef51107d39e class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; aClick() {
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).show().fadeOut(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;slow&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
}

$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#bind&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).click(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; () {
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#theone&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).live(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, aClick)
        .text(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Can Click!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
});

$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#unbind&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).click(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; () {
    $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#theone&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).die(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, aClick)
        .text(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Does nothing...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;So, how do we capture live events in the Reactive Extensions for JavaScript.&amp;nbsp; The answer is much like before when we integrated bind and unbind to the Rx.Observable.FromJQueryEvent function.&amp;nbsp; We’ll take the FromJQuery event and simply replace the calls to bind and unbind with live and die.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:43c05374-30d0-4fb4-9c30-d1eff850894b class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable.FromJQueryLiveEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(jQueryObject, eventType, eventData) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.Create (&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(observer) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; handler &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(eventObject) {
            observer.OnNext(eventObject);
        };
        jQueryObject.live(eventType, eventData, handler);
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {
            jQueryObject.die(eventType, handler);
        };
    });
};&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And we’ll also update the jQuery plugin to be able extend the jQuery object to have a ToLiveObservable method which will look like the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:83f033f8-db10-422f-9099-0e74773c4f0d class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;jQuery.fn.ToLiveObservable &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(eventType, eventData) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.FromJQueryLiveEvent(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, eventType, eventData);
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now I can rewrite the above sample which uses live and die to use the Reactive Extensions for JavaScript instead.&amp;nbsp; Instead of calling die explicitly to detach our handlers, we can call Dispose() on our subscription which underneath the covers calls the die function.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9eebcd77-c5ab-409a-8cca-f0e8ead91001 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; liveEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#bind&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; () {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#theone&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).text(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;can click...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

        liveEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#theone&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
            .ToLiveObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
            .Subscribe(aClick);
        });

$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#unbind&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; () {
            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#theone&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).text(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Does nothing...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (liveEvent &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
                liveEvent.Dispose();
        });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In addition to this example, we can show off the live aspect of this to show that in fact our observable is live.&amp;nbsp; This example will enlist the live click event on all paragraphs, timestamp our values, and then add a new paragraph with the associated timestamp.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e4406827-99d8-4bce-a565-050b5d528a54 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;p&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .ToLiveObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Timestamp()
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
        $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;p&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).after(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;p&amp;gt;Item created &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; event.Timestamp &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;/p&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We’re using the Timestamp method to append a timestamp to our value to keep track of time.&amp;nbsp; &lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Combining jQuery and the Reactive Extensions for JavaScript gives us even more options to deal with events.&amp;nbsp; In addition to the standard bind and unbind, we now have the ability to connect and disconnect live events as well.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Mon, 22 Mar 2010 03:16:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-jquery-live-event-integration</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Drag and Drop</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop</link><description>&lt;P&gt;We’ve covered a bit of ground in this series on the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt;, covering the basics, jQuery integration, blocking to asynchronous and further composition.&amp;nbsp; One way I’ve found to really get your hands dirty using this library is to take examples from other libraries and translate them into using our library of choice.&amp;nbsp; In this case, I’m taking examples written in the &lt;A href="http://flapjax-lang.org/" mce_href="http://flapjax-lang.org"&gt;Flapjax language&lt;/A&gt;, and translating them into the Reactive Extensions for JavaScript and jQuery.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx" mce_href="https://aspblogs.blob.core.windows.net/media/podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx"&gt;Bing Maps and Twitter Mashup&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Drag and Drop Example&lt;/H2&gt;
&lt;P&gt;Some of the comments made during my initial introduction to the Reactive Extensions for JavaScript mentioned the Flapjax language and whether I had heard of it.&amp;nbsp; I had indeed, having translated some of the samples from the language to the Reactive Extensions for JavaScript with some assistance with jQuery.&amp;nbsp; Today, we’ll step through a simple drag and drop scenario.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;For those unfamiliar with Flapjax, it’s a programming language built on top of JavaScript which deals with event-driven reactive design, much like RxJS.&amp;nbsp; By reading the APIs, you can very much see a Haskell influence in not only the design, but the naming and even down to the notation they use to describe the functions.&amp;nbsp; Not to mention of course that the actual compiler for Flapjax is written in Haskell.&amp;nbsp; Interestingly enough, Erik Meijer hosted an &lt;A href="http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Web-Programming-with-Flapjax/" mce_href="http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Web-Programming-with-Flapjax/"&gt;Expert-to-Expert on Channel9&lt;/A&gt; with one of the Flapjax creators, &lt;A href="http://www.cs.brown.edu/~sk/" mce_href="http://www.cs.brown.edu/~sk/"&gt;Shriram Krishnamurthi&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Before we move onto the actual drag and drop scenario, let’s look at the most basic example, tracking the mouse movement.&amp;nbsp; In Flapjax, you could write some inline code which gets compiled to native JavaScript such as the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f968fac0-8e86-4f99-822a-002dc27f291d class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;p&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;The mouse coordinates are 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;&amp;amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {! mouseLeftB(document) !}, {! mouseTopB(document) !} &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;&amp;amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;p&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Then, when compiled with Flapjax, will produce real JavaScript like the following which allows us to track the mouse movement:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1e9a8ac7-5b49-4e9f-93bc-640cf9f61dee class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;p&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
      The mouse coordinates are 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;&amp;amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;lang&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;=""&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; 
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; fxcode0()
        {
          compilerInsertDomB(mouseLeftB(compilerUnbehavior(document)),
                             &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;flapjaxsuid0&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;);
        }
        fxcode.push(fxcode0)
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;lt;!--&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;--&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="flapjaxsuid0"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
           
         &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
      , 
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;lang&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;=""&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; 
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; fxcode1()
        {
          compilerInsertDomB(mouseTopB(compilerUnbehavior(document)),
                             &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;flapjaxsuid1&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;);
        }
        fxcode.push(fxcode1)
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;&amp;lt;!--&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;--&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="flapjaxsuid1"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
           
         &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
      &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;&amp;amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;p&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Not exactly the prettiest, yet quite effective on what it’s trying to do.&amp;nbsp; What I’d rather do, however, is live in native JavaScript to make this happen instead of some of these more interesting abstractions.&amp;nbsp; In some sense, I like tools that stay true to the language and focus on libraries such as jQuery, Moo Tools, etc, although I do find CoffeeScript oddly fascinating.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To back up my argument for a second with an example in native JavaScript, we could do similar using the Reactive Extensions for JavaScript and a little bit of jQuery.&amp;nbsp; In this case, we’ll take the document’s mousemove event and in our Subscribe method call, we set the coords &amp;lt;span&amp;gt; element with our pageX and pageY values from the mousemove event.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5fa6b88d-cd4e-4168-bd37-ac3a6d915e44 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseMove &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(document)
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousemove&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
        .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#coords&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).html(event.pageX &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; event.pageY);
        });

});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we get to our actual HTML which displays our initial values of 0, 0 inside a span before our mouse starts moving.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0ac25de6-f163-4295-ab3b-11b7c1868d50 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;The mouse coordinates are &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;&amp;amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="coords"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0, 0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;&amp;amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We could of course have tackled this in any number of ways with any number of frameworks, but it is interesting how they differ in tackling the same problem.&amp;nbsp; Let’s take their example of dragging an object around the screen.&amp;nbsp; You can check their code sample here.&lt;/P&gt;
&lt;P&gt;Let’s double up our approach and create two &amp;lt;div&amp;gt; elements that we want to drag around the screen.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6c28c202-99ea-4090-ac2d-2b768b3098b6 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="dragTarget1"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;
     style&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="background-color: #000000; border: 1px solid #666666;
            color: #ffffff; padding: 10px; position: absolute;
            font-family: sans-serif; cursor: move"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
Drag Me!
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="dragTarget2"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;
     style&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="background-color: #0000FF; border: 1px solid #666666;
            color: #ffffff; padding: 10px; position: absolute;
            font-family: sans-serif; cursor: move; left: 150px"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
Drag Me Too!
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now, let’s do something interesting with it.&amp;nbsp; We’ll create a function called drag which takes a jQuery element and we’ll return the different between our image offset and the position of the mouse.&amp;nbsp; Let’s first define the function and get our mouse up and mouse move events bound.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e28b7149-3af5-4a8c-8bbd-c527cf4efb13 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; drag(dragTarget) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseUp &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mouseup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseMove &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousemove&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, on our mouse down, we’ll need to calculate the difference between the mouse position and the offset of our drag target.&amp;nbsp; We will return an object with a left and top properties calculated with the difference between the mouse position and the element’s offset.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5efa7ba8-f8db-415d-be2b-52786e20d6ff class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseDown &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousedown&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { left : event.clientX &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget.offset().left,
                     top  : event.clientY &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget.offset().top }; });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Finally, we need to take our mouse down and bind it to our mouse down with the SelectMany method passing in our image offset.&amp;nbsp; With our mouse move, we need to prevent the default action of the event from happening, which in this case is the text from being selected.&amp;nbsp; We’ll then project the position of the mouse move and create a delta between that and the image offset coming from the mouse down along with returning our drag target for ease of access.&amp;nbsp; And finally, we’ll do all of this until the mouse up event.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:88f5b641-292c-4692-b3d0-7982274f93f0 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseDown
        .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(imageOffset) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseMove
                .Do(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) { event.preventDefault(); })
                .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(pos) {
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { target : dragTarget,
                             left   : pos.clientX &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; imageOffset.left,
                             top    : pos.clientY &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; imageOffset.top }; })
                .TakeUntil(mouseUp); });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Below is the drag function in its entirety.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4bd2de5e-210d-4354-b641-132c22678598 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; drag(dragTarget) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseUp &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mouseup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseMove &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousemove&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseDown &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget
        .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;mousedown&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { left : event.clientX &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget.offset().left,
                     top  : event.clientY &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dragTarget.offset().top }; });

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseDown
        .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(imageOffset) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; mouseMove
                .Do(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) { event.preventDefault(); })
                .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(pos) {
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { target : dragTarget,
                             left   : pos.clientX &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; imageOffset.left,
                             top    : pos.clientY &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; imageOffset.top }; })
                .TakeUntil(mouseUp); });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now to hook up the two &amp;lt;div&amp;gt; elements to the drag function and then set the top and left appropriately.&amp;nbsp; To do this, we’ll create an Observer which takes the mouse position and sets the CSS property top and left accordingly.&amp;nbsp; We’ll then call Subscribe on our two elements using the Observer.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:83ba6ed6-a8bc-4a0a-9553-0a93efbfe143 class=wlWriterEditableSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; observer &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observer.Create(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(pos) {
            pos.target.css(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;left&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, pos.left);
            pos.target.css(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;top&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,  pos.top);
        });

    drag($(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#dragTarget1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)).Subscribe(observer);
    drag($(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#dragTarget2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)).Subscribe(observer);
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;You can find the source in its entirety &lt;A href="http://gist.github.com/337272" mce_href="http://gist.github.com/337272"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Taking lessons from other frameworks gives us some good practice on how to do things with our current library and with the Reactive Extensions for JavaScript and some jQuery, we’re able to do some pretty interesting things such as this simple drag and drop example.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Fri, 19 Mar 2010 17:35:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-drag-and-drop</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>The Reactive Extensions for JavaScript Released</title><link>https://weblogs.asp.net:443/podwysocki/the-reactive-extensions-for-javascript-released</link><description>&lt;P&gt;The long awaited day has come as the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for JavaScript&lt;/A&gt; have been released on DevLabs in conjunction with &lt;A href="http://live.visitmix.com/MIX10/Sessions/FTL01" mce_href="http://live.visitmix.com/MIX10/Sessions/FTL01"&gt;the talk given by Erik Meijer&lt;/A&gt; at Mix 2010.&amp;nbsp; Jeff Van Gogh, one of the principal developers on this project has more details and a detailed look at the &lt;A href="http://blogs.msdn.com/jeffva/archive/2010/03/17/reactive-extensions-for-javascript-the-time-flies-like-an-arrow-sample.aspx" mce_href="http://blogs.msdn.com/jeffva/archive/2010/03/17/reactive-extensions-for-javascript-the-time-flies-like-an-arrow-sample.aspx"&gt;sample application of “Time Flies Like an Arrow”.&lt;/A&gt;&amp;nbsp; I’d like to also give a detailed explanation of another sample application, the Bing Maps and Twitter mashup.&lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/16/introduction-to-the-reactive-extensions-for-javascript-composing-deeper.aspx"&gt;Composing Deeper&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Mashing Bing Maps and Twitter&lt;/H2&gt;
&lt;P&gt;One of the demos I’ve created is a mashup of Bing Maps and Twitter so that if the tweet has geolocation information attached that I’d display it on a map in near real time.&amp;nbsp; What that means is that at a specified interval, we query the Twitter search API for something such as FourSquare, which has a higher probability of having some geolocation information attached and then display those results.&amp;nbsp; So, how could we do this?&amp;nbsp; Let’s walk through the example step by step.&lt;/P&gt;
&lt;P&gt;First, we need to focus on the &lt;A href="http://msdn.microsoft.com/en-us/library/bb429619.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb429619.aspx"&gt;Bing Maps AJAX API&lt;/A&gt; which gives us the two main features we’re looking for, displaying a map and putting pushpins on it with some detailed information.&amp;nbsp; In order to make use of it, we’ll need to link the Bing Maps AJAX library such as the following:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:62b33ac9-906a-4c40-b010-b92a4528a346 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; 
  src&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, we’ll need a place to put our map, so, let’s create a simple &amp;lt;div&amp;gt; to host it.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:29f6d2dd-6a50-4e69-a472-fb2f29c30fae class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="veMap"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt; 
         style&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="position:relative; width: 1000px; height: 600px;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And then we’ll need our global map object that we can manipulate.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9e37cf26-dbfa-48d7-9b3b-ad72e609dcdb class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="text/javascript"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;

    &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; map &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #000000"&gt;;
    
    &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000"&gt; More code goes here&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once we’ve defined that, we need a way to show the pushpin on the page.&amp;nbsp; For this I’ll use the tweet ID for the overall pushpin ID, and I’ll take the date, latitude, longitude, their user icon URL, the user name and the text.&amp;nbsp; From there, I’ll create a pushpin in that exact spot with the associated data.&amp;nbsp; I have a try/catch block here where if I’ve seen that tweet before, the Bing Maps API doesn’t allow me to have multiple pushpins with the same ID, so I just ignore the failure.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bc095d17-6c8d-4133-8d22-62da3a9085ff class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; addPushPin(
    id, 
    date, 
    latitude, 
    longitude, 
    imageUrl, 
    text, 
    details) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;try&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; pinPoint &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; VELatLong(
            latitude,
            longitude, 
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, 
            VEAltitudeMode.RelativeToGround);
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detailText &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; date &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;-&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; details
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; pin &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; VEPushpin(id, pinPoint, imageUrl, text, detailText);
        map.AddPushpin(pin);
    } &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;catch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(err) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Seen it, don't worry&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    }
}
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Moving onto the Twitter aspect of this, we’ll need a way to query Twitter using the &lt;A href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search" mce_href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search"&gt;Search API&lt;/A&gt;.&amp;nbsp; In this case, we’ll once again use the XmlHttpRequest method with our URL which contains our text and a total request of 100 records per page.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:703546ad-d6e5-4d12-8a03-bb6c450ab5c9 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; searchTwitter(text) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://search.twitter.com/search.json?rpp=100&amp;amp;q=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; encodeURIComponent(text);

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.XmlHttpRequest(url);
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once the page loads, we need to load the map, so in my document’s ready function, I initialize the VEMap with the ID of my &amp;lt;div&amp;gt;, load the map and set my zoom level to a nice globe shot.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:62d40e3e-5943-4437-90e9-34aac80818fa class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(document).ready(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() {

    map &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; VEMap(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;veMap&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
    map.LoadMap();
    map.SetZoomLevel(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now we get to the interesting part.&amp;nbsp; How do we reload the data every so often without running into the Twitter API limit?&amp;nbsp; We can use the Interval method which we can set a due time before it invokes the action again.&amp;nbsp; In this case, our action is going to be searching for &lt;A href="http://foursquare.com/" mce_href="http://foursquare.com/"&gt;FourSquare&lt;/A&gt; which returns an IObservable&amp;lt;IObservable&amp;lt;JSONData&amp;gt;&amp;gt; in C# parlance which we need to flatten and take the first one, so we’ll need the Switch method to do that.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:34d98a35-444e-46af-93da-5be77f906a5d class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;Rx.Observable
    .Interval(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;10000&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;() { 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; searchTwitter(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;foursquare&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;); })
    .Switch()&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Realizing that we’re dealing with JSON data, we need to safely parse it and in this case, I’m using the &lt;A href="http://www.json.org/js.html" mce_href="http://www.json.org/js.html"&gt;JSON2 library&lt;/A&gt; to do that.&amp;nbsp; We’ll then take the JSON array and split it apart and turn each record into an observable with the SelectMany method.&amp;nbsp; And since we’re only interested in those with geolocation, I check if they have any with my Where method.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f01b7fae-fa0c-41fe-8329-94cf91d4eeee class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;    .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; JSON.parse(result.responseText); })
        .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; data.results.toObservable(); })
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; data.geo &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;; })&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Finally, we can subscribe to the resulting observable.&amp;nbsp; Our subscribing action is going to be adding a push pin with the JSON data as well as handle any potential errors.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6a32aebb-6c04-4e7d-9cdd-58115c249b3a class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;    .Subscribe(
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(data) {

                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; lat &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; data.geo.coordinates[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;];
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; lon &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; data.geo.coordinates[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;];
                
                addPushPin(
                    data.id, 
                    data.created_at, 
                    lat, 
                    lon, 
                    data.profile_image_url, 
                    data.from_user, 
                    data.text);
        },
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(error) {
            alert(error);
        });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;The code in its entirety can be found &lt;A href="http://gist.github.com/335925" mce_href="http://gist.github.com/335925"&gt;here&lt;/A&gt;.&amp;nbsp; And below is the result of our code where we see the icons of the Twitter users who mention FourSquare after we leave it running for just a few short minutes…&lt;/P&gt;
&lt;P&gt;&lt;A href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/foursquare_5F00_1D6674E0.png" mce_href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/foursquare_5F00_1D6674E0.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=foursquare border=0 alt=foursquare src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/foursquare_5F00_thumb_5F00_1FA2FD9C.png" width=460 height=276 mce_src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/foursquare_5F00_thumb_5F00_1FA2FD9C.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We could take this example further to expire the pushpins over time, but I think overall, it’s a great example of asynchronous programming in JavaScript and how you can integrate it into your APIs.&lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Through the use of the Reactive Extensions for JavaScript, we’re able to mash two APIs together through AJAX and refresh them at a certain interval, keeping a near real-time feel to it.&amp;nbsp; That’s just one of the many things we can do with it that I’ll hopefully cover more in the near future.&amp;nbsp; So, &lt;A href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;download it&lt;/A&gt;, and &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/rx/threads"&gt;give the team feedback!&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Thu, 18 Mar 2010 03:39:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/the-reactive-extensions-for-javascript-released</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item><item><title>Introduction to the Reactive Extensions for JavaScript – Composing deeper</title><link>https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-composing-deeper</link><description>&lt;P&gt;We’ve covered a bit of ground already in this series on the Reactive Extensions for JavaScript (RxJS) from the basic information, creating observers and observables, jQuery integration, composing asynchronous methods with callbacks and in the last post, turned blocking calls into asynchronous calls.&amp;nbsp; Now that we have some more fundamental building blocks, let’s see what else we can do with it.&amp;nbsp; Before moving to &lt;A href="http://flapjax-lang.org/" mce_href="http://flapjax-lang.org/"&gt;FlapJax&lt;/A&gt; examples, I want to revisit the &lt;A href="http://www.microsofttranslator.com/Default.aspx" mce_href="http://www.microsofttranslator.com/Default.aspx"&gt;Microsoft Translator&lt;/A&gt; to take a piece of text and translate into all languages except the currently detected.&lt;/P&gt;
&lt;P&gt;Also, if you’re at &lt;A href="http://live.visitmix.com/" mce_href="http://live.visitmix.com/"&gt;Mix&lt;/A&gt; this year, you’ll notice that Erik Meijer is giving a talk on the &lt;A href="http://live.visitmix.com/MIX10/Sessions/FTL01" mce_href="http://live.visitmix.com/MIX10/Sessions/FTL01"&gt;Reactive Extensions for JavaScript&lt;/A&gt;. I highly recommend that you go to this session, especially if you’ve been interested in this series.&amp;nbsp; Prepare for some pretty slick demos to show off the functionality of the library. &lt;/P&gt;
&lt;P&gt;Before we get started, let’s get caught up to where we are today:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/23/introduction-to-the-reactive-extensions-for-javascript-creating-observables.aspx"&gt;Creating Observables&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/27/introduction-to-the-reactive-extensions-for-javascript-creating-observers.aspx"&gt;Creating Observers&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/05/introduction-to-the-reactive-extensions-for-javascript-jquery-integration.aspx"&gt;jQuery Integration&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/11/introduction-to-the-reactive-extensions-for-javascript-composing-callbacks.aspx"&gt;Composing Callbacks&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/12/introduction-to-the-reactive-extensions-for-javascript-from-blocking-to-async.aspx"&gt;From Blocking to Async&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup.aspx"&gt;Wikipedia Lookup&lt;/A&gt;&amp;nbsp; &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Composing and Filtering&lt;/H2&gt;
&lt;P&gt;As I stated earlier, the goal of this post was to take our piece of text, detect the language and then translate it to all other languages other than the detected.&amp;nbsp; We’ll make use of the detect, getLanguages and translate methods as we’ve talked about earlier, but with some slight changes.&amp;nbsp; Since the time I wrote these posts, the Microsoft Translator has released a new API for us to call which gets away from the JavaScript library approach and to the URL based approach instead.&amp;nbsp; You can read more about &lt;A href="http://msdn.microsoft.com/en-us/library/ff512423.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ff512423.aspx"&gt;the APIs&lt;/A&gt; in their documentation.&amp;nbsp; Let’s get started taking our old code and porting it to the new APIs.&amp;nbsp; First, I’ll need the base URL and an AppID that you can apply for.&amp;nbsp; And next, I’ll need to handle making an XmlHttpRequest, getting the value and then evaluating the response.&amp;nbsp; Yes, I realize that eval might be used for malicious purposes, but in this case, this is simple text being returned.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2b213d4f-314a-4e5b-927e-444b0543f4c1 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;http://api.microsofttranslator.com/V2/Ajax.svc/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; appId &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;someappid&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getResponse(url) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Rx.Observable.XmlHttpRequest(url)
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; eval(result.responseText);
        });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now we can turn our attention once again to the getLanguages function which gives us all of the language codes that are currently supported by the Translator.&amp;nbsp; To do this, we build up a URL for our request, send it to the getResponse function, and then project each language array of our observable sequence to an observable sequence, in this case our resulting array turned into an observable, and flattens the resulting observable sequences into one observable sequence.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:55d47ff5-d7c0-4bfe-bab4-e33ef479837f class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getLanguages() {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; serviceUrl &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;GetLanguagesForTranslate?appId=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;  appId;

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getResponse(serviceUrl)
        .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(result) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; result.toObservable(); });
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Our detect follows the same pattern of building up a URL and gets the response from the XmlHttpRequest as an observable sequence. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3a2ff311-4b5d-4b4b-985d-ff88d0810489 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detect(text) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; serviceUrl &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Detect?AppId=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; appId &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;text=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; encodeURIComponent(text);
        
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getResponse(serviceUrl);
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And translate follows the very same pattern as well. &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0be5a51b-f2e8-4b7b-978f-f5419afabd37 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(text, from, to) {

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; serviceUrl &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; url &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Translate?appId=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;  appId &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;from=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; from &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;to=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; to &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;amp;text=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; encodeURIComponent(text);

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getResponse(serviceUrl);
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;We’ll start first at creating a subscription for the translation button to listen for clicks:&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3621c25b-41c6-4b25-b90b-9d22dfdc3914 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#translateCommand&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .ToObservable(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;click&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    .Subscribe(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(event) {
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; More stuff here&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;    
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Inside of the Subscribe method, we need to clear out the last results as well as get the value of the text to be translated.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6ef5ac2d-6c5b-4d7f-b4ea-63b2b1efb631 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;$(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#translatedText&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).empty();
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; textTotranslate &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; $(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;#translateText&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;).val();&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Next, we need to add more code to do the actual translation.&amp;nbsp; What we’re going to do is first detect the text and before we get to combining this with another function.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9e848562-36d3-4af6-96a8-4496f74b4c49 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translator &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detect(textTotranslate)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;I’m going to need to take the detected language and then translate it into all the other languages.&amp;nbsp; In order to do that, I need to use the SelectMany method which lets you project our detected text to another observable sequence, which in this case will be the languages.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b73f8ce-5cc6-49e5-93f3-bc127b7bd347 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;.SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(detectedLanguage) {
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Code goes here&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Once we get to the languages, we want to filter out the language that equals our detected language, and then once again, project our language to the translate method observable sequence.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:be1d3d1e-6ddc-405d-ad36-eab40c154c21 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getLanguages()
    .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(language) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; language &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detectedLanguage; })&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;I’ll take the result of the translated text and then project to return an object with a translated property with our translated text, and the associated language property.&amp;nbsp; &lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b4300e9c-4d8d-411a-90f1-9d74b0bb9641 class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #000000"&gt;.SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(language) {
     &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(translatedText, detectedLanguage, language)
        .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(translatedText) {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { translated : translatedText, language : language };
 });&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;All together, the code to take the piece of text, detect its language, and translate it into all other supported languages looks like the following.&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7eb90161-b277-4388-8e56-f9a4ed209a8c class=wlWriterSmartContent&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; OVERFLOW: auto"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translator &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detect(textTotranslate)
    .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(detectedLanguage) {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; getLanguages()
            .Where(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(language) { &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; language &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; detectedLanguage; })
            .SelectMany(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(language) {
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; translate(translatedText, detectedLanguage, language)
                    .Select(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(translatedText) {
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; { translated : translatedText, language : language };
                });
        });
});&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Then we could subscribe to this observable sequence and for example, take a sentence “Do you know what time it is?” and translate it into 29 other languages.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_4AA99198.png" mce_href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_4AA99198.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; WIDTH: 364px; DISPLAY: inline; HEIGHT: 438px; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_1B0B730C.png" width=364 height=438 mce_src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_1B0B730C.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;What’s really interesting about this example is the true asynchronous behavior.&amp;nbsp; If I press the button again with the same sentence, the order of their return is not always the same each and every time.&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;H2&gt;Conclusion&lt;/H2&gt;
&lt;P&gt;Through the use of the Reactive Extensions for JavaScript, we’ve been able to compose together asynchronous behavior of detecting a piece of text’s language, and translating it into 29 different languages all at once.&amp;nbsp; This could apply to any number of scenarios where we need to compose together one asynchronous call after another, but only when certain things happen.&amp;nbsp; And better yet, to use code in a fluent API style, you don’t have this logic scattered throughout all of your functions.&lt;/P&gt;
&lt;P&gt;This of course is only scratching the surface of what capabilities this library has and there is much more yet left to cover.&amp;nbsp; The question you’re probably asking now is where can I get it?&amp;nbsp; Well, for that you’ll have to stay tuned to Erik Meijer and his &lt;A href="http://live.visitmix.com/MIX10/Sessions/FTL01" mce_href="http://live.visitmix.com/MIX10/Sessions/FTL01"&gt;Reactive Extensions for JavaScript&lt;/A&gt; session at Mix on Wednesday.&lt;/P&gt;
&lt;P&gt;What can I say?&amp;nbsp; I love JavaScript and very much looking forward to the upcoming &lt;A href="http://jsconf.us/2010" mce_href="http://jsconf.us/2010"&gt;JSConf 2010&lt;/A&gt; here in Washington, DC where the Reactive Extensions for JavaScript will be shown in its full glory with &lt;A href="http://twitter.com/jvgogh" mce_href="http://twitter.com/jvgogh"&gt;Jeffrey Van Gogh&lt;/A&gt; (who you can now follow on Twitter).&amp;nbsp; For too many times, we’ve looked for the abstractions over the natural language of the web (HTML, CSS and JavaScript) and created monstrosities instead of embracing the web for what it is.&amp;nbsp; With libraries such as jQuery and indeed the Reactive Extensions for JavaScript gives us better tools for dealing with the troubled child that is DOM manipulation and especially events.&lt;/P&gt;</description><pubDate>Wed, 17 Mar 2010 14:07:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/podwysocki/introduction-to-the-reactive-extensions-for-javascript-composing-deeper</guid><category>Event-based Programming</category><category>JavaScript</category><category>JSConf</category><category>Reactive Framework</category></item></channel></rss>