Created
August 24, 2017 12:49
-
-
Save contrerasmarc/29788a1434a18814222a7b04d5d1ac87 to your computer and use it in GitHub Desktop.
Code Mi_Tabla3 (Table + CouchDB) 24-08-2017
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: 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, | |
name: 'Mi Store for People' | |
}).from('MiTabla3.PeopleDataSource') | |
// TODO: Add global constants or singleton objects needed by your app here. | |
}); |
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: MiTabla3 - | |
// Copyright: Michelin | |
// ========================================================================== | |
/*globals MiTabla3 */ | |
// This is the function that will start your app running. The default | |
// implementation will load any fixtures you have created then instantiate | |
// your controllers and fire up the state chart. | |
// | |
// As you develop your application you will probably want to override this. | |
// See comments for some pointers on what to do next. | |
// | |
MiTabla3.main = function main() { | |
// Step 1: Set the statechart to be the default responder | |
// The default code here will set the statechart as default responder for your application. | |
// It means that any action which is not given a specific target will be sent to the | |
// statechart. For most applications, this is the most convenient. | |
var statechart = MiTabla3.statechart; | |
SC.RootResponder.responder.set('defaultResponder', statechart); | |
// Step 2. If you are using controllers which need to be setup with data before | |
// your app comes alive (for example controllers holding menu options), do it here. | |
// var content = MiTabla3.store.find(MiTabla3.Group); | |
// MiTabla3.groupsController.set('content', content); | |
// or | |
// MiTabla3.menuListController.set('content', MiTabla3.menuOptions); | |
// var query = MiTabla3.PEOPLE_QUERY; | |
// var people = MiTabla3.store.find(query); | |
// var people = MiTabla3.store.find(MiTabla3.PeopleModel); | |
// MiTabla3.peopleTableController.set('content', people); | |
// var phone = MiTabla3.store.find(MiTabla3.PhoneModel); | |
// MiTabla3.phoneTableController.set('content', phone); | |
// Step 3. Initialize the statechart and make your app come alive. | |
statechart.initStatechart(); | |
}; | |
function main() { MiTabla3.main(); } |
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: 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. | |
prefix: '' | |
}); | |
MiTabla3.set('connection', connection); | |
// var query = MiTabla3.PEOPLE_QUERY; | |
var query = SC.Query.create({ | |
// isEditable: YES, | |
// destroyOnRemoval: YES, | |
recordType: MiTabla3.PeopleModel, | |
orderBy:'name', | |
//conditions: "type = 'person' ", | |
database: 'mi_tabla', | |
// viewName: 'people_app/allPeople', | |
viewName: '' | |
}); | |
// console.log(query.recordType); | |
var people = MiTabla3.store.find(query); | |
// console.log(people.store.name); | |
// Controller get the data | |
MiTabla3.peopleTableController.set('content', people); | |
}, | |
exitState: function() { | |
MiTabla3.getPath('mainPage.mainPane').remove(); | |
} | |
}); |
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: MiTabla3 - | |
// Copyright: Michelin | |
// ========================================================================== | |
/*globals MiTabla3 */ | |
/** @class | |
(Document Your Data Source Here) | |
@extends SC.DataSource | |
*/ | |
// 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: '' | |
// | |
// }); | |
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'); | |
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 | |
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); | |
} | |
} | |
}); |
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: MiTabla3 - | |
// Copyright: Michelin | |
// ========================================================================== | |
/*globals MiTabla3 */ | |
/** @class | |
(Document your Model here) | |
@extends SC.Record | |
@version 0.1 | |
*/ | |
MiTabla3.PeopleModel = SC.Record.extend( | |
/** @scope MiTabla3.PeopleModel.prototype */ | |
{ | |
database: "mi_tabla", | |
primaryKey: "_id", | |
index: SC.Record.attr(Number), | |
// primaryKey: "index", | |
type: SC.Record.attr(String, { | |
defaultValue: 'person' | |
}), | |
name: SC.Record.attr(String), | |
country: SC.Record.attr(String), | |
phone: SC.Record.attr(String), | |
date: SC.Record.attr(String), | |
filler: SC.Record.attr(String) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment