Created
July 20, 2009 19:20
-
-
Save craigw/150585 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| AddressBook = { | |
| // Change this to point to your own CouchDB instance. | |
| uri: "http://craig-01.vm.xeriom.net:5984/addressbook/", | |
| _request: function(method, uri) { | |
| var req = new XMLHttpRequest(); | |
| req.open(method, uri, false); | |
| req.send(); | |
| return req; | |
| }, | |
| // Fetch all address book cards. | |
| allCards: function() { | |
| var req = this._request("GET", this.uri + "_all_docs"); | |
| var result = JSON.parse(req.responseText); | |
| if (req.status != 200) | |
| throw result; | |
| var allDocs = []; | |
| for(var offset in result.rows) { | |
| var id = result.rows[offset]["id"]; | |
| var doc = this.openCard(id); | |
| allDocs[allDocs.length] = doc; | |
| } | |
| return allDocs; | |
| }, | |
| // Fetch an individual address book card. | |
| openCard: function(id) { | |
| var req = this._request("GET", this.uri + id); | |
| if (req.status == 404) | |
| return null; | |
| var result = JSON.parse(req.responseText); | |
| if (req.status != 200) | |
| throw result; | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment