Resumé

RxJs along with JsRender

Cristian Merighi () 0.00

Quick article that reviews the twitter reader sample, using RxJs (Reactive Extensions for Javascript) to improve code elegance and readability.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

In perpetuating the hackneyed example of the Twitter reader, I want to (re)propose it baked with some relative new stuff coming from the world of js frameworks: Rx-Js!

The programming nouvelle vague coerces to be declarative and fluent in coding, Rx is meant to fave this way of writing code.

Here's how the code extracted from my last article appears after a bit of restyling using Observables and Subscribers:

Rx-Js + Rx bindings for jQuery
  1. function loadTweets() {
  2.     // Rx-jQuery ajax (fluent version)
  3.     var query = $.ajaxAsObservable({
  4.         // be sure to specify dataType jsonp
  5.         dataType: "jsonp",
  6.         data: { include_entities: true, include_rts: true, screen_name: 'cmerighi', count: 10 },
  7.         url: 'https://api.twitter.com/1/statuses/user_timeline.json'
  8.     }).selectMany(function (json) {
  9.         // sweep out as data get received...
  10.         $('#tweets').empty();
  11.         //
  12.         return Rx.Observable.fromArray(json.data);
  13.     }).select(function (tweet) {
  14.         return {
  15.             img: tweet.user.profile_image_url_https,
  16.             text: renderTweet(tweet),
  17.             date: timeago(tweet.created_at)
  18.         };
  19.     }).subscribe(function (tweet) {
  20.         // append the new item (JsRender template)
  21.         $('#tweets').append($.render.twit(tweet));
  22.         },
  23.         function (error) {
  24.             // handle the error
  25.         });
  26. }

Presenting the updated version of the usual DEMO...
I end this article by suggesting NuGet as the optimal RxJs repository:

NuGet

NuGet

Take care. Bye.

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds