Created
July 20, 2017 17:58
-
-
Save mauritslamers/70d691545389af144e99b8f6233368ba 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
CoreMeetme.DataSource = SC.DataSource.extend({ | |
// we assume there is a connection already before we are asked to retrieve anything | |
fetch: function (store, query) { | |
// queries are either allDocs calls or views | |
// queries carry the database name on which they should be executed | |
var db = query.get('database'); | |
if (!db && query.get('isRemote')) throw new Error("CoreMeetme: no database on query???"); | |
if (!db && query.get('isLocal')) return true; //don't handle local queries | |
var viewName = query.get('viewName'); | |
if (viewName) { // view | |
// something is off with the view callback....TODO | |
CoreMeetme.connection.database(db).view(viewName, this, this._viewDidFetch, store, query); | |
return true; | |
} | |
else { // alldocs | |
CoreMeetme.connection.database(db).all({ include_docs: true }, this, this._allDocsDidFetch, store, query); | |
return true; | |
} | |
// | |
//return NO ; // do not handle anything! | |
}, | |
_allDocsDidFetch: function (err, result, store, query) { | |
debugger; | |
if (!err) { | |
var sks = store.loadRecords(query.get('recordType'), result.rows.getEach('doc')); | |
store.dataSourceDidFetchQuery(query, sks); | |
} | |
else { | |
store.dataSourceDidErrorQuery(query, err); | |
// and how do you actually catch this in the statechart I don't know.. | |
} | |
if (query.target && query.method) { | |
if (SC.typeOf(query.method) === SC.T_STRING) { | |
query.target[query.method](err); | |
} | |
else { | |
query.method.call(query.target, err); | |
} | |
} | |
}, | |
_viewDidFetch: function (err, result, store, query) { | |
if (err) { | |
if (err === Couch.ERROR_NOAUTH) { | |
// if we are not authenticated to get this, we also should not try to install | |
} | |
} | |
else { | |
var docs = result.rows.getEach('value'); | |
// var students = docs.filter(function (d) { return d.roles.indexOf('student') > -1; }); | |
// var teachers = docs.filter(function (d) { return d.roles.indexOf('teacher') > -1; }); | |
var keys = store.loadRecords(query.recordType, docs); | |
store.dataSourceDidFetchQuery(query, keys); | |
} | |
}, | |
// we keep the simple implementation for now | |
// _buffer: null, | |
// updateRecords: function () { | |
// SC.debug("updateRecords in CoreMeetme datasource"); | |
// SC.Logger.debugWithoutFmt(arguments); | |
// sc_super(); | |
// }, | |
updateRecord: function (store, storeKey, params) { | |
var recHash = store.readDataHash(storeKey); | |
var database = params.database; | |
CoreMeetme.database(database).save(recHash, this, this._updateRecordDidRespond, store, storeKey, recHash); | |
}, | |
_updateRecordDidRespond: function (err, res, store, storeKey, recHash) { | |
if (!err) { | |
recHash._rev = res.rev; | |
store.dataSourceDidComplete(storeKey, recHash); | |
} | |
else { | |
store.dataSourceDidError(storeKey, err); | |
} | |
}, | |
createRecord: function (store, storeKey, params) { | |
var recHash = store.readDataHash(storeKey); | |
var database = params.database; | |
if (!database) throw new Error("Undefined database in datasource"); | |
if (!recHash._id) { // if an id wasn't already given, we give it | |
recHash._id = CoreMeetme.connection.uuid(); | |
} | |
CoreMeetme.database(database).save(recHash, this, this._createRecordDidRespond, store, storeKey, recHash); | |
}, | |
_createRecordDidRespond: function (err, res, store, storeKey, recHash) { | |
if (!err) { | |
// there is something more that should happen, as in that we'd need a primary key? | |
// or perhaps not, as we can get one before | |
recHash._rev = res.rev; | |
store.dataSourceDidComplete(storeKey, recHash, recHash._id); | |
} | |
else { | |
store.dataSourceDidError(storeKey, err); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment