Index: Dev/branches/rest-dojo-ui/client/db/db.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/db.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/db/db.js	(revision 343)
@@ -93,5 +93,5 @@
             req('PUT','/rft')
             
-            log("Setting _security on database 'rft'");
+            /*log("Setting _security on database 'rft'");
             req('PUT','/rft/_security', securityDoc);
 
@@ -103,5 +103,5 @@
                 log("Creating user 'rft_admin' with password 'Welkom01'");
                 req('PUT','/_users/org.couchdb.user:rft_admin', rft_adminDoc);
-            });
+            });*/
         };
 
Index: Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json	(revision 343)
@@ -4,4 +4,7 @@
     "validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) { if (oldDoc && oldDoc.published) { throw({forbidden: 'Published documents cannot be modified.'}); } }",
     "views": {
+        "by_type": {
+            "map": "function(doc) { emit(doc.type, doc); }"
+        },
         "unpublished": {
             "map": "function(doc) { if ( doc.type == 'Survey' && !doc.published ) { emit(doc._id, doc); } }"
Index: Dev/branches/rest-dojo-ui/client/rft/api.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/api.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/api.js	(revision 343)
@@ -5,5 +5,5 @@
             this.post = function(path,args) {
                 var dfd = xhr.post({
-                    url: "../server/api.php/"+path,
+                    url: "../server/api/"+path,
                     handleAs: "json",
                     headers: {
Index: Dev/branches/rest-dojo-ui/client/rft/auth.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/auth.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/auth.js	(revision 343)
@@ -7,5 +7,5 @@
             function post(path,args) {
                 return xhr.post({
-                    url: "../server/api.php"+path,
+                    url: "../server/api"+path,
                     handleAs: "json",
                     headers: {
@@ -18,5 +18,5 @@
             self.login = function(username,password) {
                 var d = new Deferred();
-                post("/login",
+                /*post("/login",
                 {
                     email:username,
@@ -28,5 +28,6 @@
                 },function(){
                     d.reject(); 
-                });
+                });*/
+                d.resolve();
                 return d.promise;
             };
@@ -34,5 +35,5 @@
             self.restore = function() {
                 var d = new Deferred();
-                post("/login",{})
+                /*post("/login",{})
                 .then(function(data) {
                     currentUser = data;
@@ -40,5 +41,6 @@
                 },function(){
                     d.reject(); 
-                });
+                });*/
+                d.resolve();
                 return d.promise;
             
@@ -47,5 +49,5 @@
             self.register = function(username,password) {
                 var d = new Deferred();
-                post("/register",
+                /*post("/register",
                 {
                     email:username,
@@ -57,5 +59,6 @@
                 },function(){
                     d.reject(); 
-                });
+                });*/
+                d.resolve();
                 return d.promise;
             };
Index: Dev/branches/rest-dojo-ui/client/rft/pages/questions.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/pages/questions.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/pages/questions.js	(revision 343)
@@ -8,10 +8,9 @@
             },
             onVisit: function() {
-                this._store = store.getStore('Question');
                 this._list = new AccordionList({
                     actions: {
                         'Edit': lang.hitch(this,'_editQuestion')
                     },
-                    idProperty: this._store.idProperty,
+                    idProperty: store.idProperty,
                     categoryProperty: 'category',
                     titleProperty: 'title'
@@ -21,5 +20,5 @@
             },
             _refresh: function() {
-                Deferred.when(this._store.query())
+                Deferred.when(store.query('_design/default/_view/by_type',{key: 'Question'}))
                 .then(lang.hitch(this,function(items){
                     this._list.setItems(items);
@@ -27,5 +26,5 @@
             },
             onNewQuestion: function() {
-                Deferred.when( this._store.add({}) )
+                Deferred.when(store.add({type:'Question'}))
                 .then(lang.hitch(this,function(question){
                     this._editQuestion(question);
@@ -39,5 +38,5 @@
             onSaveQuestion: function(evt) {
                 var value = this.questionWidget.get('value');
-                Deferred.when( this._store.put(value) )
+                Deferred.when(store.put(value))
                 .then(lang.hitch(this,function(){
                     this.questionDialog.hide();
Index: Dev/branches/rest-dojo-ui/client/rft/pages/survey.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/pages/survey.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/pages/survey.js	(revision 343)
@@ -5,12 +5,7 @@
         return declare('rft.pages.survey',[_Page],{
             object: null,
-            postCreate: function() {
-                this.inherited(arguments);
-                this._surveyStore = store.getStore('Survey');
-                this._questionStore = store.getStore('Question');
-            },
             onVisit: function() {
                 if ( this.pageArgs.uid ) {
-                    Deferred.when(this._surveyStore.get(this.pageArgs.uid))
+                    Deferred.when(store.get(this.pageArgs.uid))
                     .then(lang.hitch(this,function(obj){
                         this.object = obj;
@@ -21,12 +16,10 @@
                         this.creator.innerHTML = (obj && obj.email) || 'unknown';
                     }));
-                    Deferred.when(this._questionStore.query())
+                    Deferred.when(store.query('_design/default/_view/by_type',{key:'Question'}))
                     .then(lang.hitch(this,function(items){
                         this._questionList = new AccordionList({
-                            store: this._surveyStore,
                             actions: {
                                 'Add': lang.hitch(this,'_addQuestion')
                             },
-                            idProperty: this._questionStore.idProperty,
                             categoryProperty: 'category',
                             titleProperty: 'title'
@@ -44,5 +37,5 @@
             onSave: function(evt) {
                 lang.mixin(this.object,this.form.get('value'));
-                Deferred.when( this._store.put(this.object) )
+                Deferred.when( store.put(this.object) )
                 .then(lang.hitch(this,function(obj){
                     this.object = obj;
@@ -53,4 +46,5 @@
                 }));
                 event.stop(evt);
+                evt.stopPropagation();
                 return false;
             },
Index: Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js	(revision 343)
@@ -3,9 +3,8 @@
         return declare('rft.pages.surveys',[_Page],{
             selectedObject: null,
-            postCreate: function() {
-                this._store = store.getStore('Survey');
-            },
             onVisit: function() {
-                this.grid.setStore(ObjectStore({objectStore: this._store}));
+                this.grid.setStore(
+                    ObjectStore({objectStore: store}),
+                    "_design/default/_view/by_type",{key:'Survey'});
                 
                 this.grid.on('rowclick',lang.hitch(this,function(evt){
@@ -16,11 +15,11 @@
                 this.grid.on('rowdblclick',lang.hitch(this,function(evt){
                     var obj = evt.grid.getItem(evt.rowIndex);
-                    content.goTo('/survey',{uid:obj.getUid()});
+                    content.goTo('/survey',{uid:store.getIdentity(obj)});
                 }));
                                 
                 this.btnNew.on('click',lang.hitch(this,function(){
-                    Deferred.when( this._store.add({'creator':auth.getUser()}) )
+                    Deferred.when( store.add({type:'Survey',creator:auth.getUser()}) )
                     .then(function(obj) {
-                        content.goTo('/survey',{uid:obj.getUid()});
+                        content.goTo('/survey',{uid:store.getIdentity(obj)});
                     });
                 }));
@@ -28,5 +27,5 @@
                 this.btnEdit.on('click',lang.hitch(this,function(){
                     if ( this.selectedObject ) {
-                        content.goTo('/survey',{uid:this.selectedObject.getUid()});
+                        content.goTo('/survey',{uid:store.getIdentity(this.selectedObject)});
                     }
                     
Index: Dev/branches/rest-dojo-ui/client/rft/store.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/store.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/store.js	(revision 343)
@@ -1,69 +1,165 @@
-define(['dojo/_base/lang','dojo/_base/array','dojo/_base/Deferred','dojo/store/JsonRest','./api'],
-    function(lang,array,Deferred,JsonRest,api){
+define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/Deferred','dojo/_base/xhr','dojo/json','dojo/store/util/QueryResults'],
+    function(declare,lang,array,Deferred,xhr,JSON,QueryResults){
     
-        var OBJ_TYPE = '__objectType';
-        var OBJ_UID = '__objectUid';
-        var REF_TYPE = '__referenceType';
-        var REF_UID = '__referenceUid';
+        var CouchStore = declare("rft.store", null, {
+            /** dojo.Store implementation for CouchDB
+             *
+             * See for details on the REST API, the wiki
+             * at http://wiki.apache.org/couchdb/HTTP_Document_API.
+             */
+	        target: "",
+	        accepts: "application/json", 
+            idProperty: "_id",
+            revProperty: "_rev",
+            _responseIdProperty: "id",
+            _responseRevProperty: "rev",
+	        constructor: function(options){
+		        declare.safeMixin(this, options);
+            },
+	        getIdentity: function(object){
+		        return object[this.idProperty];
+            },
+	        getRevision: function(object){
+		        return object[this.revProperty];
+            },
+	        get: function(id){
+                var dfd = new Deferred();
+		        xhr("GET", {
+			        url: this.target + id,
+			        handleAs: "json",
+			        headers: {
+		                Accept: this.accepts
+                    }
+		        }).then(function(result){
+                    if ( result.error ) {
+                        dfd.reject(result.reason);
+                    } else {
+                        dfd.resolve(result);
+                    }
+                }, function(err){
+                    dfd.reject(err);
+                });
+                return dfd.promise;
+            },
+	        put: function(object, options){
+		        options = options || {};
 
-        var RftObject = function(values) {
-            var self = this;
-            lang.mixin(this,values);
-            self.getUid = function() {
-                return self[OBJ_UID];
+                var dfd = new Deferred();
+		        var id = options.id ? options.id : this.getIdentity(object);
+		        var hasId = typeof id != "undefined";
+		        xhr(hasId ? "PUT" : "POST", {
+				    url: hasId ? this.target + id : this.target,
+				    postData: JSON.stringify(object),
+				    handleAs: "json",
+				    headers:{
+					    "Content-Type": "application/json",
+					    Accept: this.accepts
+				    }
+			    }).then(function(result){
+                    if ( result.error ) {
+                        dfd.reject(result.reason);
+                    } else {
+                        object[this.idProperty] = result[this._responseIdProperty];
+                        object[this.revProperty] = result[this._responseRevProperty];
+                        dfd.resolve(object);
+                    }
+                }, function(err){
+                    dfd.reject(err);
+                });
+                return dfd.promise;
+            },
+	        add: function(object, options){
+                return this.put(object,options);
+            },
+	        remove: function(id,rev){
+                var dfd = new Deferred();
+		        xhr("DELETE",{
+			        url: this.target + id,
+				    headers: {
+                        'If-Match': rev
+                    }
+		        }).then(function(result){
+                    if ( result.error ) {
+                        dfd.reject(result.reason);
+                    } else {
+                        dfd.resolve();
+                    }
+                },function(err){
+                    dfd.reject(err);
+                });
+                return dfd.promise;
+            },
+            query: function(query, options){
+		        options = options || {};
+
+                var dfd = new Deferred();
+                var queryOpts = {};
+                if ( query === undefined ) {
+                    query = '_all_docs';
+                    queryOpts.include_docs = true;
+                }
+
+                if (!lang.isString(query)) {
+                    console.warn("Query must be a view name");
+                }
+
+                // Standard options
+                if (options.start >= 0) {
+                    queryOpts.skip = options.start;
+                }
+                if (options.count >= 0) {
+                    queryOpts.limit = options.count;
+                }
+                if (options.sort) {
+                    if (options.sort[0]) {
+                        if (options.sort[0].attribute && options.sort[0].attribute !== "key") {
+                            console.warn("Can only sort on key");
+                        }
+                        if (options.sort[0].descending) {
+                            queryOpts.descending = true;
+                        }
+                    }
+                    if (options.sort.length > 1) {
+                        console.warn("multiple sort fields not supported");
+                    }
+                }
+
+                // Custom options
+                if (options.key) {
+                    queryOpts.key = options.key;
+                } else if (options.keys) {
+                    queryOpts.keys = options.keys;
+                } else if (options.startkey || options.endkey) {
+                    queryOpts.startkey = options.startkey;
+                    queryOpts.endkey = options.endkey;
+                }
+
+                for (var qp in queryOpts) {
+                    queryOpts[qp] = JSON.stringify(queryOpts[qp]);
+                }
+			    query += '?' + xhr.objectToQuery(queryOpts);
+
+		        xhr("GET", {
+			        url: this.target + query,
+			        handleAs: "json",
+			        headers: {
+                        Accept: this.accepts
+                    }
+		        }).then(function(result){
+                    if (result.error) {
+                        dfd.reject(result.reason);
+                    } else  {
+                        dfd.resolve(
+                            array.map(result.rows,
+                                      function(result){ return result.value; }));
+                    }
+                },function(err){
+                    dfd.reject(err);
+                });
+		        return QueryResults(dfd.promise);
             }
-        };
-    
-        var rftStoreCtor = function(objectType) {
-            var jsonStore = new JsonRest({
-                target:"../server/api.php/data/"+objectType+"/",
-                idProperty: OBJ_UID
-            });
-            var errHandler = api.defaultErrorHandler;
-            return lang.delegate(jsonStore,{
-                query: function(query, directives){
-                    return Deferred.when( jsonStore.query(query,directives), function(results){
-                        return array.map(results,function(result){
-                            return new RftObject(typeof result == "object" ? result : object);
-                        });
-                    },errHandler);
-                },
-                get: function(id, directives){
-                    return Deferred.when( jsonStore.get(id,directives), function(result){
-                        return new RftObject(typeof result == "object" ? result : object);
-                    },errHandler);
-                },
-                add: function(object, directives){
-                    return Deferred.when( jsonStore.add(object,directives), function(result){
-                        return new RftObject(typeof result == "object" ? result : object);
-                    },errHandler);
-                },
-                put: function(object, directives){
-                    return Deferred.when( jsonStore.put(object,directives), function(result) {
-                        return new RftObject(typeof result == "object" ? result : object);
-                    },errHandler);
-                },
-                remove: function(id, directives){
-                    return jsonStore.remove(id,directives);
-                }
-            });
-        };
-    
-        var storeCache = {};
-    
-        return {
-            getStore: function(objectType) {
-                if ( !storeCache[objectType] ) {
-                    storeCache[objectType] = new rftStoreCtor(objectType);
-                }
-                return storeCache[objectType];
-            },
-            dereference: function(object) {
-                if ( object[REF_TYPE] ) {
-                    return this.getStore(object[REF_TYPE]).get(object[REF_UID]);
-                }
-                return object;
-            }
-        };
+        });
+
+        return new CouchStore({target: '/couchdb/rft/'});
 
     });
Index: Dev/branches/rest-dojo-ui/client/rft/ui/QuestionWidget.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/ui/QuestionWidget.js	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/ui/QuestionWidget.js	(revision 343)
@@ -9,5 +9,5 @@
             name: '',
             value: null,
-            _type: null,
+            _scale: null,
             _widgetCache: null,
             constructor: function() {
@@ -18,9 +18,9 @@
             postCreate: function() {
                 this._resetValue = this.value;
-                this.typeSelector.set('disabled', this.mode == 'edit');
+                this.scaleSelector.set('disabled', this.mode == 'edit');
             },
             _setValueAttr: function(value) {
                 this.value = value;
-                this._onTypeChange(value.type || 'string');
+                this._onTypeChange(value.scale || 'string');
                 this.ourForm.set('value',value);
             },
@@ -30,15 +30,15 @@
                 return this.value;
             },
-            _onTypeChange: function(type) {
-                if ( this._type == type ) return;
-                this._type = type;
-                domConstruct.empty(this.typeDetails);
-                var widget = this._getTypeWidget(type);
-                widget && widget.placeAt(this.typeDetails,'only');
+            _onTypeChange: function(scale) {
+                if ( this._scale == scale ) return;
+                this._scale = scale;
+                domConstruct.empty(this.scaleDetails);
+                var widget = this._getTypeWidget(scale);
+                widget && widget.placeAt(this.scaleDetails,'only');
             },
-            _getTypeWidget: function(type) {
-                var widget = this._widgetCache[type];
+            _getTypeWidget: function(scale) {
+                var widget = this._widgetCache[scale];
                 if (!widget) {
-                    switch(type) {
+                    switch(scale) {
                         case 'string':
                             widget = new TextBox({
@@ -59,9 +59,9 @@
                                 name: 'answers',
                                 mode: this.mode,
-                                allowMultiple: type == 'multipleChoice'
+                                allowMultiple: scale == 'multipleChoice'
                             });
                             break;
                     }
-                    this._widgetCache[type] = widget;
+                    this._widgetCache[scale] = widget;
                 }
                 return widget;
Index: Dev/branches/rest-dojo-ui/client/rft/ui/templates/QuestionWidget.html
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/ui/templates/QuestionWidget.html	(revision 342)
+++ Dev/branches/rest-dojo-ui/client/rft/ui/templates/QuestionWidget.html	(revision 343)
@@ -12,5 +12,5 @@
         </fieldset>
         <fieldset>
-            <select data-dojo-type="dijit.form.Select" data-dojo-attach-point="typeSelector" data-dojo-attach-event="onChange:_onTypeChange" name="type">
+            <select data-dojo-type="dijit.form.Select" data-dojo-attach-point="scaleSelector" data-dojo-attach-event="onChange:_onTypeChange" name="scale">
                 <option value="string" selected>String</option>
                 <option value="text">Text</option>
@@ -18,5 +18,5 @@
                 <option value="multipleChoice">Multiple choice</option>
             </select>
-            <div data-dojo-attach-point="typeDetails"></div>
+            <div data-dojo-attach-point="scaleDetails"></div>
         </fieldset>
     </form>
