Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created September 10, 2009 10:10
Show Gist options
  • Save thomaslang/184456 to your computer and use it in GitHub Desktop.
Save thomaslang/184456 to your computer and use it in GitHub Desktop.
SC.MyCouchdbDataSource = SC.DataSource.extend( {
root: "/couchdb/",
database: "",
design: "",
databasePath: function(){
return this.get('root') + this.get('database');
}.property('database'),
designPath: function(){
return this.get('databasePath') + "/_design/" + this.get('design');
}.property('databasePath', 'design'),
cancelStoreKeys: {},
fetchRequest: SC.Request.getUrl("").set('isJSON', YES),
// CUSTOM LOADING //
loadAllDocs: function(store) {
var r = this.fetchRequest;
var url = this.get('designPath')+"/_view/all_docs";
r.set('address', url);
r.header("Content-Type", "application/json");
r.notify(this, this.loadAllDocsDidComplete, {store: store}).send();
return
},
loadAllDocsDidComplete: function(r, params) {
var response = r.response();
var recordTypes = {};
var store = params.store;
var docs, doc, l, i, dataHash, recordType, primaryKey, type, storeKey, id;
if(response.kindOf ? response.kindOf(SC.Error) : false) {
this.requestDidError(r);
} else {
docs = response.rows;
l = docs.length;
for(i=0; i<l; i++) {
doc = docs[i];
type = doc.value.type;
recordType = recordTypes[type];
if (!recordType) recordType = recordTypes[type] = SC.objectForPropertyPath(type);
dataHash = doc.value;
id = dataHash._id ;
storeKey = recordType.storeKeyFor(id);
store.pushRetrieve(recordType, id, dataHash, storeKey);
}
BB.startApp();
}
},
...... snip ......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment