Skip to content

Instantly share code, notes, and snippets.

@contrerasmarc

contrerasmarc/core.js

Last active Jul 26, 2017
Embed
What would you like to do?
Almost ok using the Couch framework (sproutcore-couchdb) framework
// ==========================================================================
// Project: MiTabla3 -
// Copyright: Michelin
// ==========================================================================
/*globals MiTabla3 */
/** @namespace
My cool new app. Describe your application.
@extends SC.Object
*/
MiTabla3 = SC.Application.create(
/** @scope MiTabla3.prototype */ {
NAMESPACE: 'MiTabla3',
VERSION: '0.1.0',
// This is your application store. You will use this store to access all
// of your model data. You can also set a data source on this store to
// connect to a backend server. The default setup below connects the store
// to any fixtures you define.
// Usando los Fixtures
// store: SC.Store.create().from(SC.Record.fixtures)
connection: null,
store: SC.Store.create({
commitRecordsAutomatically: YES
}).from('MiTabla3.PeopleDataSource')
// TODO: Add global constants or singleton objects needed by your app here.
});
// ==========================================================================
// Project: MiTabla3 -
// Copyright: Michelin
// ==========================================================================
/*globals MiTabla3 */
MiTabla3.PEOPLE_QUERY = SC.Query.local(MiTabla3.PeopleModel, {
// isEditable: YES,
// destroyOnRemoval: YES,
orderBy:'name',
conditions: "type = 'person' ",
database: 'mi_tabla',
// viewName: 'people_app/allPeople',
viewName: ''
});
/** @class
(Document Your Data Source Here)
@extends SC.DataSource
*/
MiTabla3.PeopleDataSource = SC.DataSource.extend(
/** @scope MiTabla3.PeopleDataSource.prototype */ {
// we assume there is a connection already before we are asked to retrieve anything
init: function(){
},
fetch: function (store, query) {
var db = query.get('database');
// queries are either allDocs calls or views
// queries carry the database name on which they should be executed
if (!db && query.get('isRemote')) throw new Error("No database on query???");
if (!db && query.get('isLocal')) return true; //don't handle local queries
var viewName = query.get('viewName');
// console.log("queryGetViewName:", query.get('viewName'));
if (viewName) { // view
// something is off with the view callback....
MiTabla3.connection.database(db).view(viewName, this, this._viewDidFetch, store, query);
return true;
}
else { // alldocs
console.log(store, query);
MiTabla3.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;
MiTabla3.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 = MiTabla3.connection.uuid();
}
MiTabla3.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);
}
}
});
// ==========================================================================
// Project: MiTabla3
// Copyright: @2015 My Company, Inc.
// ==========================================================================
/*globals MiTabla3 */
/** @class
(Document Your State Here)
@extends SC.State
@version 0.1
*/
MiTabla3.ReadyState = SC.State.extend({
enterState: function() {
MiTabla3.getPath('mainPage.mainPane').append();
// ..........................................................
// Connecting to data (CouchDB)
//
var connection = Couch.Connection.create({
prefix: 'http://localhost:5984' // in case your couch is not available at the server root.
});
MiTabla3.set('connection', connection);
var query = MiTabla3.PEOPLE_QUERY;
var people = MiTabla3.store.find(query);
console.log(people);
// Controller get the data
MiTabla3.peopleTableController.set('content', people);
},
exitState: function() {
MiTabla3.getPath('mainPage.mainPane').remove();
}
});
@contrerasmarc

This comment has been minimized.

Copy link
Owner Author

@contrerasmarc contrerasmarc commented Jul 26, 2017

Almost ready to success, using Couch framework from Maurits Lamers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment