Skip to content

Instantly share code, notes, and snippets.

@wolfflow
Created February 8, 2013 14:22
Show Gist options
  • Save wolfflow/4739298 to your computer and use it in GitHub Desktop.
Save wolfflow/4739298 to your computer and use it in GitHub Desktop.
Bacon.UI mini)
(function () {
Bacon.UI = {
ajax: function (params) {
return Bacon.fromPromise($.ajax(params))
},
get: function (url, data, dataType) {
return this.ajax({ url: url, dataType: dataType, data: data })
},
getJSON: function (url, data) {
return this.ajax({ url: url, dataType: "json", data: data })
},
post: function (url, data, dataType) {
this.ajax({ url: url, dataType: dataType, data: data, type: "POST" })
},
hash: function (defaultValue) {
if (defaultValue === undefined) defaultValue = ""
function getHash() {
return !!document.location.hash ? document.location.hash : defaultValue
}
return $(window).asEventStream("hashchange").map(getHash).toProperty(getHash()).skipDuplicates()
}
}
Bacon.Observable.prototype.awaiting = function (response) {
return this.map(true).merge(response.map(false)).toProperty(false).skipDuplicates()
}
Bacon.EventStream.prototype.ajax = function () {
return this.flatMapLatest(Bacon.UI.ajax)
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment