Created
May 11, 2010 22:19
-
-
Save tdegrunt/397956 to your computer and use it in GitHub Desktop.
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
// ========================================================================== | |
// Project: Project.MongodbDataSource | |
// Copyright: ©2010 My Company, Inc. | |
// ========================================================================== | |
/*globals Reader */ | |
/** @class | |
(Document Your Data Source Here) | |
@extends SC.DataSource | |
*/ | |
Project.MongodbDataSource = SC.DataSource.extend( | |
/** @scope Project.MongodbDataSource.prototype */ { | |
// .......................................................... | |
// GENERICS | |
// | |
dbName: function() { | |
return "projectdb"; | |
}.property().cacheable(), | |
/* | |
* Retrieves the collection name for the recordType. | |
* We assume the collection name is the model, lowercased. | |
*/ | |
collectionNameFor: function (recordType) { | |
return recordType.toString().split('.').pop().toLowerCase(); | |
}, | |
/* | |
* Builds the URL to query to, for different parameters: | |
* query | |
* store, storeKey | |
*/ | |
urlFor: function () { | |
var query, store, storeKey, result; | |
if (arguments.length == 1 && arguments[0].kindOf(SC.Query)) { | |
query = arguments[0]; | |
var myQuery = ""; | |
if (query.conditions) { | |
var myConditions = "{"+query.conditions.replace(/ AND /,",").replace(/([a-zA-Z]*) \= (\'|\")?([0-1a-zA-Z]*)(\'|\")?/g,'"$1": $2$3$4')+"}"; | |
myQuery = "?query="+escape(myConditions); | |
} | |
result = '/'+this.get('dbName')+'/'+this.collectionNameFor(query.recordType)+myQuery; | |
} else if (arguments.length == 2 && arguments[0].kindOf(SC.Store)) { | |
store = arguments[0]; | |
storeKey = arguments[1]; | |
result = "/"+this.get('dbName')+"/"+this.collectionNameFor(store.recordTypeFor(storeKey))+"/"+store.idFor(storeKey); | |
} else { | |
result = null; | |
} | |
return result; | |
}, | |
// .......................................................... | |
// QUERY SUPPORT | |
// | |
fetch: function (store, query) { | |
if (this.collectionNameFor(query.recordType)!=null) { | |
SC.Request.getUrl(this.urlFor(query)).json() | |
.notify(this, 'didFetchDocuments', store, query) | |
.send(); | |
return YES; | |
} | |
return NO ; // return YES if you handled the query | |
}, | |
didFetchDocuments: function (response, store, query) { | |
if (SC.ok(response)) { | |
store.loadRecords(query.recordType, response.get('body')); | |
store.dataSourceDidFetchQuery(query); | |
} else { | |
store.dataSourceDidErrorQuery(query, response); | |
} | |
}, | |
// .......................................................... | |
// RECORD SUPPORT | |
// | |
retrieveRecord: function (store, storeKey) { | |
if (this.collectionNameFor(store.recordTypeFor(storeKey))) { | |
SC.Request.getUrl(this.urlFor(store, storeKey)).json() | |
.notify(this, 'didRetrieveDocument', store, storeKey) | |
.send(); | |
return YES; | |
} else { | |
return NO; | |
} | |
}, | |
didRetrieveDocument: function (response, store, storeKey) { | |
if (SC.ok(response)) { | |
var dataHash = response.get('body'); | |
store.dataSourceDidComplete(storeKey, dataHash); | |
} else { | |
store.dataSourceDidError(storeKey, response); | |
} | |
}, | |
createRecord: function (store, storeKey) { | |
if (this.collectionNameFor(store.recordTypeFor(storeKey))) { | |
SC.Request.postUrl(this.urlFor(store, storeKey)).json() | |
.notify(this, 'didCreateDocument', store, storeKey) | |
.send(store.readDataHash(storeKey)); | |
return YES; | |
} else { | |
return NO; | |
} | |
}, | |
didCreateDocument: function (response, store, storeKey) { | |
if (SC.ok(response)) { | |
store.dataSourceDidComplete(storeKey, null, response.header('Location').split('/').pop()); | |
} else { | |
store.dataSourceDidError(storeKey, response); | |
} | |
}, | |
updateRecord: function (store, storeKey) { | |
if (this.collectionNameFor(store.recordTypeFor(storeKey))) { | |
SC.Request.putUrl(this.urlFor(store, storeKey)).json() | |
.notify(this, 'didUpdateDocument', store, storeKey) | |
.send(store.readDataHash(storeKey)); | |
return YES; | |
} else { | |
return NO; | |
} | |
}, | |
didUpdateDocument: function (response, store, storeKey) { | |
if (SC.ok(response)) { | |
store.dataSourceDidComplete(storeKey); | |
} else { | |
store.dataSourceDidError(storeKey); | |
} | |
}, | |
destroyRecord: function (store, storeKey) { | |
if (this.collectionNameFor(store.recordTypeFor(storeKey))) { | |
SC.Request.deleteUrl(this.urlFor(store, storeKey)).json() | |
.notify(this, 'didDestroyDocument', store, storeKey) | |
.send(); | |
return YES; | |
} else { | |
return NO; | |
} | |
}, | |
didDestroyDocument: function (response, store, storeKey) { | |
if (SC.ok(response)) { | |
store.dataSourceDidDestroy(storeKey); | |
} else { | |
store.dataSourceDidError(response); | |
} | |
} | |
}) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment