Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created August 11, 2009 20:40
Show Gist options
  • Save thomaslang/166107 to your computer and use it in GitHub Desktop.
Save thomaslang/166107 to your computer and use it in GitHub Desktop.
BB.main = function main() {
var usersQuery = SC.Query.create({recordType: BB.User});
BB.set('usersQuery', usersQuery);
var users = BB.store.findAll(BB.get('usersQuery'));
BB.set('users', users);
var conceptsQuery = SC.Query.create({
recordType: BB.Concept,
conditions: "type = 'BB.Concept'"});
BB.set('conceptsQuery', conceptsQuery);
var concepts = BB.store.findAll(BB.get('conceptsQuery'));
BB.set('concepts', concepts);
var descriptionsQuery = SC.Query.create({
recordType: BB.Description,
conditions: "type = 'BB.Description'"});
BB.set('descriptionsQuery', descriptionsQuery);
var descriptions = BB.store.findAll(BB.get('descriptionsQuery'));
BB.set('descriptions', descriptions);
BB.store.dataSource.loadAllDocs(BB.store);
//SC.RunLoop.begin();
BB.usersController.set('content', users);
//SC.RunLoop.end();
} ;
/////////////////////////////////////
datasource code:
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 null;
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();
else {return ret;}
},
///////////////////////////
code that creates the records:
_addConcept: function() {
var concept, description;
concept = this.store.createRecord(BB.Concept,
{_id: "@id%@".fmt(SC.Store.generateStoreKey())}
);
concept.set('user', BB.userController);
concept.set('date', new Date());
this.store.commitRecord(BB.Concept, concept.get('_id'));
concept.addObserver('status', this, '_addDescription');
//this._addDescription(concept);
return YES;
},
_addDescription: function(concept) {
var store = concept.get('store');
var description;
console.log('_addDescription called');
console.log(concept.get('status'));
if(concept.get('status') == SC.Record.READY_CLEAN) {
console.log('status is READY_CLEAN');
// remove observer
concept.removeObserver('status', this, '_addDescription');
// create description record
description = store.createRecord(BB.Description,{
content: BB.getPath('addController.description'),
_id: "@id%@".fmt(SC.Store.generateStoreKey())
});
description.set('concept', concept);
description.set('user', BB.userController);
description.set('date', new Date());
// commit description record
store.commitRecord(BB.Description, description.get('_id'));
}
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment