Created
May 31, 2010 20:33
-
-
Save robotarmy/420252 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
Ledger.prototype.collection = function(callback) { | |
var client = new DStore('experiment',new Server("127.0.0.1", 27017, {},{strict:true})); | |
client.open(function() { | |
client.createCollection('ledger', function(err, collection) { | |
client.collection('ledger', function(err, collection) { | |
callback.apply(this,[err,collection,client]); | |
}); | |
}); | |
}); | |
}; |
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
Ledger.prototype.service = function() { | |
var self = this; | |
var http = require('http'); | |
var sys = require('sys'); | |
self.server = http.createServer(function(request,response) { | |
var req = request; | |
var resp = response; | |
self.find_all(function(err,items){ | |
sys.puts(err); | |
var callback = undefined; | |
var query = url.parse(request.url,true).query | |
if (query != undefined) | |
callback = query['callback']; | |
if (callback != undefined){ | |
var body = ";"+callback+"("+JSON.stringify(items)+");"; | |
resp.writeHead(200, { | |
'Content-Length': body.length, | |
'Content-Type': 'application/json;charset=utf-8' | |
}); | |
resp.write(body,'utf8') | |
resp.end(); | |
} | |
}); | |
}); | |
self.server.addListener('close',function(errno) { | |
sys.puts('close ' + errno); | |
}); | |
self.server.listen(7001); | |
}; |
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
Ledger.prototype.find_all = function(callback) { | |
this.collection(function(err, collection) { | |
collection.find(function(err, cursor) { | |
cursor.toArray(function(err, docs) { | |
callback.apply(this,[err,docs]); | |
}); | |
}); | |
}); | |
}; | |
# Same problems with this write of it | |
# | |
Ledger.prototype.find_all = function(callback) { | |
this.collection(function(err, collection, client) { | |
raise_if(err); | |
collection.find(function(err, cursor) { | |
raise_if(err); | |
docs = [] | |
cursor.each(function(err, doc) { | |
raise_if(err); | |
docs[docs.length] = doc | |
}); | |
callback.apply(this,[err,docs]); | |
client.close(); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you so much Christian for your thought and efforts.