HTTP Ajax Module

Implementation

Module

At first we need http module which we can use in our application.

var http = {}; //module
//bus
http.gets = Warden.Stream(function(trigger){
  this.get = function(url){
    $.get(url).always(trigger);
  }
}, http);

Helper functions and const data

These functions are commonly used, and Warden provides easy way to use them.

var errorMessage = "<p class='error'> Error: {{status}}: {{statusText}}</p>";
// or Warden.Utils.is.str
function isString(res){
  return typeof res == 'string';
}
// or Warden.Utils.not
function not(predicate){
  return function(x){
    return !predicate(x);
  }
}
      

Streams

It's not the best way to filetr success response from failed, but currently we sure that if response is string than it's correct HTML markup, otherwise it's Error object

var successes = http.gets.filter(isString);
var errors = http.gets.filter(not(isString)).interpolate(errorMessage);
var responses = successes.merge(errors);

Side effects

$(document).ready(function(){
  responses.bindTo($(".box"), 'html');
});