Location: A review of cardiac cellular electrophysiology models @ 98909b01e6b2 / dojo-presentation / js / dojo / dojox / data / CouchDBRestStore.js

Author:
David Nickerson <nickerso@users.sourceforge.net>
Date:
2009-07-07 17:11:57+12:00
Desc:
update for modified HH graphs in tutorial description
Permanent Source URI:
https://models.physiomeproject.org/workspace/a1/rawfile/98909b01e6b21653a5e1cd28865dd259c586d490/dojo-presentation/js/dojo/dojox/data/CouchDBRestStore.js

dojo.provide("dojox.data.CouchDBRestStore");
dojo.require("dojox.data.JsonRestStore");

// A CouchDBRestStore is an extension of JsonRestStore to handle CouchDB's idiosyncrasies, special features,
// and deviations from standard HTTP Rest.
// NOTE: CouchDB is not designed to be run on a public facing network. There is no access control
// on database documents, and you should NOT rely on client side control to implement security.


dojo.declare("dojox.data.CouchDBRestStore",
	dojox.data.JsonRestStore,
	{
		save: function(kwArgs) {
			var actions = this.inherited(arguments); // do the default save and then update for version numbers
			var prefix = this.service.servicePath;
			for(var i = 0; i < actions.length; i++){
				// need to update the item's version number after it has been committed
				(function(item,dfd){
					dfd.addCallback(function(result){
						if(result){
							item.__assignedId = prefix + result.id; // update the object with the results of the post
							item._rev = result.rev;
						}
						return result;
					});
				})(actions[i].content,actions[i].deferred);
			}
		},
		fetch: function(args){
			// summary:
			// 		This only differs from JsonRestStore in that it, will put the query string the query part of the URL and it handles start and count
			if(!args.useIndexCache){
				args.query = '_all_docs?' + (args.query || '');
			}
			if(args.start){
				args.query = (args.query ? (args.query + '&') : '') + 'skip=' + args.start;
				delete args.start;
			}
			if(args.count){
				args.query = (args.query ? (args.query + '&') : '') + 'count=' + args.count;
				delete args.count;
			}
			return this.inherited(arguments);
		},
		_processResults: function(results){
			var rows = results.rows;
			if(rows){
				var prefix = this.service.servicePath;
				var self = this;
				for(var i = 0; i < rows.length;i++){
					rows[i] = {
						__id: prefix + rows[i].id, 
						_id: rows[i].id,
						_loadObject: function(callback){
							self.fetchItemByIdentity({
								identity: this._id,
								onItem: callback
							});
							delete this._loadObject;
						}
					};
				}
				return {totalCount:results.total_rows, items:results.rows};
			}else{
				return {items:results};
			}
						
		}
	}
);

// create a set of stores
dojox.data.CouchDBRestStore.getStores = function(couchServerUrl){
	var dfd = dojo.xhrGet({
		url: couchServerUrl+"_all_dbs",
		handleAs: "json",
		sync: true
	});
	var stores = {};
	dfd.addBoth(function(dbs){
		for(var i = 0; i < dbs.length; i++){
			stores[dbs[i]] = new dojox.data.CouchDBRestStore({target:couchServerUrl + dbs[i],idAttribute:"_id"});
		}
		return stores;
	});
	return stores;
};