Created
February 8, 2013 14:22
-
-
Save wolfflow/4739298 to your computer and use it in GitHub Desktop.
Bacon.UI mini)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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