Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created August 10, 2009 19:14
Show Gist options
  • Save thomaslang/165360 to your computer and use it in GitHub Desktop.
Save thomaslang/165360 to your computer and use it in GitHub Desktop.
SC.MyCouchdbDataSource = SC.DataSource.extend( {
database: "",
design: "",
cancelStoreKeys: {},
fetchRequest: SC.Request.getUrl("").set('isJSON', YES),
// STANDARD DATA SOURCE METHODS //
fetch: function(store, fetchKey, params) {
var ret = [];
var action = {};
var r = this.fetchRequest;
var database = this.get('database') ;
var design = this.get('design') ;
var view, url;
var isQuery = (SC.instanceOf(fetchKey, SC.Query)) ? YES : NO ;
//if(isQuery) return fetchKey;
if(isQuery) {
fetchKey = fetchKey.get('recordType');
}
if(SC.typeOf(fetchKey)===SC.T_STRING) {
fetchKey = SC.objectForPropertyPath(fetchKey);
}
view = fetchKey.prototype.get("couchView") ;
url = database+"/_design/"+design+"/_view/"+view;
r.set('address', url);
r.header("Content-Type", "application/json");
//console.log(r);
r.notify(this, this.fetchDidComplete,{
store: store,
fetchKey: fetchKey ,
storeKeyArray: ret }
).send();
return ret;
},
fetchDidComplete: function(r, params) {
var hashes= [], dataHash, storeKeys= [], store, fetchKey, ret, primaryKey,
response, results, lenresults, idx;
response = r.response();
if(response.kindOf ? response.kindOf(SC.Error) : false) {
this.requestDidError(r);
} else {
fetchKey = params.fetchKey;
primaryKey = fetchKey ? fetchKey.prototype.primaryKey : 'guid';
results = response.rows;
lenresults = results.length;
for(idx=0;idx<lenresults;idx++) {
dataHash = results[idx].value;
dataHash[primaryKey] = dataHash._id ;
hashes.push(dataHash);
}
storeKeys = params.store.loadRecords(fetchKey, hashes);
params.storeKeyArray.replace(0,0,storeKeys);
}
return YES;
},
// --- snip ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment