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);
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);
}
}
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);
$(document).ready(function(){
responses.bindTo($(".box"), 'html');
});