Changeset 343
- Timestamp:
- 06/18/12 12:51:58 (13 years ago)
- Location:
- Dev/branches/rest-dojo-ui/client
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/branches/rest-dojo-ui/client/db/db.js
r328 r343 93 93 req('PUT','/rft') 94 94 95 log("Setting _security on database 'rft'");95 /*log("Setting _security on database 'rft'"); 96 96 req('PUT','/rft/_security', securityDoc); 97 97 … … 103 103 log("Creating user 'rft_admin' with password 'Welkom01'"); 104 104 req('PUT','/_users/org.couchdb.user:rft_admin', rft_adminDoc); 105 }); 105 });*/ 106 106 }; 107 107 -
Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json
r328 r343 4 4 "validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) { if (oldDoc && oldDoc.published) { throw({forbidden: 'Published documents cannot be modified.'}); } }", 5 5 "views": { 6 "by_type": { 7 "map": "function(doc) { emit(doc.type, doc); }" 8 }, 6 9 "unpublished": { 7 10 "map": "function(doc) { if ( doc.type == 'Survey' && !doc.published ) { emit(doc._id, doc); } }" -
Dev/branches/rest-dojo-ui/client/rft/api.js
r311 r343 5 5 this.post = function(path,args) { 6 6 var dfd = xhr.post({ 7 url: "../server/api .php/"+path,7 url: "../server/api/"+path, 8 8 handleAs: "json", 9 9 headers: { -
Dev/branches/rest-dojo-ui/client/rft/auth.js
r274 r343 7 7 function post(path,args) { 8 8 return xhr.post({ 9 url: "../server/api .php"+path,9 url: "../server/api"+path, 10 10 handleAs: "json", 11 11 headers: { … … 18 18 self.login = function(username,password) { 19 19 var d = new Deferred(); 20 post("/login",20 /*post("/login", 21 21 { 22 22 email:username, … … 28 28 },function(){ 29 29 d.reject(); 30 }); 30 });*/ 31 d.resolve(); 31 32 return d.promise; 32 33 }; … … 34 35 self.restore = function() { 35 36 var d = new Deferred(); 36 post("/login",{})37 /*post("/login",{}) 37 38 .then(function(data) { 38 39 currentUser = data; … … 40 41 },function(){ 41 42 d.reject(); 42 }); 43 });*/ 44 d.resolve(); 43 45 return d.promise; 44 46 … … 47 49 self.register = function(username,password) { 48 50 var d = new Deferred(); 49 post("/register",51 /*post("/register", 50 52 { 51 53 email:username, … … 57 59 },function(){ 58 60 d.reject(); 59 }); 61 });*/ 62 d.resolve(); 60 63 return d.promise; 61 64 }; -
Dev/branches/rest-dojo-ui/client/rft/pages/questions.js
r316 r343 8 8 }, 9 9 onVisit: function() { 10 this._store = store.getStore('Question');11 10 this._list = new AccordionList({ 12 11 actions: { 13 12 'Edit': lang.hitch(this,'_editQuestion') 14 13 }, 15 idProperty: this._store.idProperty,14 idProperty: store.idProperty, 16 15 categoryProperty: 'category', 17 16 titleProperty: 'title' … … 21 20 }, 22 21 _refresh: function() { 23 Deferred.when( this._store.query())22 Deferred.when(store.query('_design/default/_view/by_type',{key: 'Question'})) 24 23 .then(lang.hitch(this,function(items){ 25 24 this._list.setItems(items); … … 27 26 }, 28 27 onNewQuestion: function() { 29 Deferred.when( this._store.add({}))28 Deferred.when(store.add({type:'Question'})) 30 29 .then(lang.hitch(this,function(question){ 31 30 this._editQuestion(question); … … 39 38 onSaveQuestion: function(evt) { 40 39 var value = this.questionWidget.get('value'); 41 Deferred.when( this._store.put(value))40 Deferred.when(store.put(value)) 42 41 .then(lang.hitch(this,function(){ 43 42 this.questionDialog.hide(); -
Dev/branches/rest-dojo-ui/client/rft/pages/survey.js
r316 r343 5 5 return declare('rft.pages.survey',[_Page],{ 6 6 object: null, 7 postCreate: function() {8 this.inherited(arguments);9 this._surveyStore = store.getStore('Survey');10 this._questionStore = store.getStore('Question');11 },12 7 onVisit: function() { 13 8 if ( this.pageArgs.uid ) { 14 Deferred.when( this._surveyStore.get(this.pageArgs.uid))9 Deferred.when(store.get(this.pageArgs.uid)) 15 10 .then(lang.hitch(this,function(obj){ 16 11 this.object = obj; … … 21 16 this.creator.innerHTML = (obj && obj.email) || 'unknown'; 22 17 })); 23 Deferred.when( this._questionStore.query())18 Deferred.when(store.query('_design/default/_view/by_type',{key:'Question'})) 24 19 .then(lang.hitch(this,function(items){ 25 20 this._questionList = new AccordionList({ 26 store: this._surveyStore,27 21 actions: { 28 22 'Add': lang.hitch(this,'_addQuestion') 29 23 }, 30 idProperty: this._questionStore.idProperty,31 24 categoryProperty: 'category', 32 25 titleProperty: 'title' … … 44 37 onSave: function(evt) { 45 38 lang.mixin(this.object,this.form.get('value')); 46 Deferred.when( this._store.put(this.object) )39 Deferred.when( store.put(this.object) ) 47 40 .then(lang.hitch(this,function(obj){ 48 41 this.object = obj; … … 53 46 })); 54 47 event.stop(evt); 48 evt.stopPropagation(); 55 49 return false; 56 50 }, -
Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js
r303 r343 3 3 return declare('rft.pages.surveys',[_Page],{ 4 4 selectedObject: null, 5 postCreate: function() {6 this._store = store.getStore('Survey');7 },8 5 onVisit: function() { 9 this.grid.setStore(ObjectStore({objectStore: this._store})); 6 this.grid.setStore( 7 ObjectStore({objectStore: store}), 8 "_design/default/_view/by_type",{key:'Survey'}); 10 9 11 10 this.grid.on('rowclick',lang.hitch(this,function(evt){ … … 16 15 this.grid.on('rowdblclick',lang.hitch(this,function(evt){ 17 16 var obj = evt.grid.getItem(evt.rowIndex); 18 content.goTo('/survey',{uid: obj.getUid()});17 content.goTo('/survey',{uid:store.getIdentity(obj)}); 19 18 })); 20 19 21 20 this.btnNew.on('click',lang.hitch(this,function(){ 22 Deferred.when( this._store.add({'creator':auth.getUser()}) )21 Deferred.when( store.add({type:'Survey',creator:auth.getUser()}) ) 23 22 .then(function(obj) { 24 content.goTo('/survey',{uid: obj.getUid()});23 content.goTo('/survey',{uid:store.getIdentity(obj)}); 25 24 }); 26 25 })); … … 28 27 this.btnEdit.on('click',lang.hitch(this,function(){ 29 28 if ( this.selectedObject ) { 30 content.goTo('/survey',{uid: this.selectedObject.getUid()});29 content.goTo('/survey',{uid:store.getIdentity(this.selectedObject)}); 31 30 } 32 31 -
Dev/branches/rest-dojo-ui/client/rft/store.js
r311 r343 1 define(['dojo/_base/ lang','dojo/_base/array','dojo/_base/Deferred','dojo/store/JsonRest','./api'],2 function( lang,array,Deferred,JsonRest,api){1 define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/Deferred','dojo/_base/xhr','dojo/json','dojo/store/util/QueryResults'], 2 function(declare,lang,array,Deferred,xhr,JSON,QueryResults){ 3 3 4 var OBJ_TYPE = '__objectType'; 5 var OBJ_UID = '__objectUid'; 6 var REF_TYPE = '__referenceType'; 7 var REF_UID = '__referenceUid'; 4 var CouchStore = declare("rft.store", null, { 5 /** dojo.Store implementation for CouchDB 6 * 7 * See for details on the REST API, the wiki 8 * at http://wiki.apache.org/couchdb/HTTP_Document_API. 9 */ 10 target: "", 11 accepts: "application/json", 12 idProperty: "_id", 13 revProperty: "_rev", 14 _responseIdProperty: "id", 15 _responseRevProperty: "rev", 16 constructor: function(options){ 17 declare.safeMixin(this, options); 18 }, 19 getIdentity: function(object){ 20 return object[this.idProperty]; 21 }, 22 getRevision: function(object){ 23 return object[this.revProperty]; 24 }, 25 get: function(id){ 26 var dfd = new Deferred(); 27 xhr("GET", { 28 url: this.target + id, 29 handleAs: "json", 30 headers: { 31 Accept: this.accepts 32 } 33 }).then(function(result){ 34 if ( result.error ) { 35 dfd.reject(result.reason); 36 } else { 37 dfd.resolve(result); 38 } 39 }, function(err){ 40 dfd.reject(err); 41 }); 42 return dfd.promise; 43 }, 44 put: function(object, options){ 45 options = options || {}; 8 46 9 var RftObject = function(values) { 10 var self = this; 11 lang.mixin(this,values); 12 self.getUid = function() { 13 return self[OBJ_UID]; 47 var dfd = new Deferred(); 48 var id = options.id ? options.id : this.getIdentity(object); 49 var hasId = typeof id != "undefined"; 50 xhr(hasId ? "PUT" : "POST", { 51 url: hasId ? this.target + id : this.target, 52 postData: JSON.stringify(object), 53 handleAs: "json", 54 headers:{ 55 "Content-Type": "application/json", 56 Accept: this.accepts 57 } 58 }).then(function(result){ 59 if ( result.error ) { 60 dfd.reject(result.reason); 61 } else { 62 object[this.idProperty] = result[this._responseIdProperty]; 63 object[this.revProperty] = result[this._responseRevProperty]; 64 dfd.resolve(object); 65 } 66 }, function(err){ 67 dfd.reject(err); 68 }); 69 return dfd.promise; 70 }, 71 add: function(object, options){ 72 return this.put(object,options); 73 }, 74 remove: function(id,rev){ 75 var dfd = new Deferred(); 76 xhr("DELETE",{ 77 url: this.target + id, 78 headers: { 79 'If-Match': rev 80 } 81 }).then(function(result){ 82 if ( result.error ) { 83 dfd.reject(result.reason); 84 } else { 85 dfd.resolve(); 86 } 87 },function(err){ 88 dfd.reject(err); 89 }); 90 return dfd.promise; 91 }, 92 query: function(query, options){ 93 options = options || {}; 94 95 var dfd = new Deferred(); 96 var queryOpts = {}; 97 if ( query === undefined ) { 98 query = '_all_docs'; 99 queryOpts.include_docs = true; 100 } 101 102 if (!lang.isString(query)) { 103 console.warn("Query must be a view name"); 104 } 105 106 // Standard options 107 if (options.start >= 0) { 108 queryOpts.skip = options.start; 109 } 110 if (options.count >= 0) { 111 queryOpts.limit = options.count; 112 } 113 if (options.sort) { 114 if (options.sort[0]) { 115 if (options.sort[0].attribute && options.sort[0].attribute !== "key") { 116 console.warn("Can only sort on key"); 117 } 118 if (options.sort[0].descending) { 119 queryOpts.descending = true; 120 } 121 } 122 if (options.sort.length > 1) { 123 console.warn("multiple sort fields not supported"); 124 } 125 } 126 127 // Custom options 128 if (options.key) { 129 queryOpts.key = options.key; 130 } else if (options.keys) { 131 queryOpts.keys = options.keys; 132 } else if (options.startkey || options.endkey) { 133 queryOpts.startkey = options.startkey; 134 queryOpts.endkey = options.endkey; 135 } 136 137 for (var qp in queryOpts) { 138 queryOpts[qp] = JSON.stringify(queryOpts[qp]); 139 } 140 query += '?' + xhr.objectToQuery(queryOpts); 141 142 xhr("GET", { 143 url: this.target + query, 144 handleAs: "json", 145 headers: { 146 Accept: this.accepts 147 } 148 }).then(function(result){ 149 if (result.error) { 150 dfd.reject(result.reason); 151 } else { 152 dfd.resolve( 153 array.map(result.rows, 154 function(result){ return result.value; })); 155 } 156 },function(err){ 157 dfd.reject(err); 158 }); 159 return QueryResults(dfd.promise); 14 160 } 15 }; 16 17 var rftStoreCtor = function(objectType) { 18 var jsonStore = new JsonRest({ 19 target:"../server/api.php/data/"+objectType+"/", 20 idProperty: OBJ_UID 21 }); 22 var errHandler = api.defaultErrorHandler; 23 return lang.delegate(jsonStore,{ 24 query: function(query, directives){ 25 return Deferred.when( jsonStore.query(query,directives), function(results){ 26 return array.map(results,function(result){ 27 return new RftObject(typeof result == "object" ? result : object); 28 }); 29 },errHandler); 30 }, 31 get: function(id, directives){ 32 return Deferred.when( jsonStore.get(id,directives), function(result){ 33 return new RftObject(typeof result == "object" ? result : object); 34 },errHandler); 35 }, 36 add: function(object, directives){ 37 return Deferred.when( jsonStore.add(object,directives), function(result){ 38 return new RftObject(typeof result == "object" ? result : object); 39 },errHandler); 40 }, 41 put: function(object, directives){ 42 return Deferred.when( jsonStore.put(object,directives), function(result) { 43 return new RftObject(typeof result == "object" ? result : object); 44 },errHandler); 45 }, 46 remove: function(id, directives){ 47 return jsonStore.remove(id,directives); 48 } 49 }); 50 }; 51 52 var storeCache = {}; 53 54 return { 55 getStore: function(objectType) { 56 if ( !storeCache[objectType] ) { 57 storeCache[objectType] = new rftStoreCtor(objectType); 58 } 59 return storeCache[objectType]; 60 }, 61 dereference: function(object) { 62 if ( object[REF_TYPE] ) { 63 return this.getStore(object[REF_TYPE]).get(object[REF_UID]); 64 } 65 return object; 66 } 67 }; 161 }); 162 163 return new CouchStore({target: '/couchdb/rft/'}); 68 164 69 165 }); -
Dev/branches/rest-dojo-ui/client/rft/ui/QuestionWidget.js
r305 r343 9 9 name: '', 10 10 value: null, 11 _ type: null,11 _scale: null, 12 12 _widgetCache: null, 13 13 constructor: function() { … … 18 18 postCreate: function() { 19 19 this._resetValue = this.value; 20 this. typeSelector.set('disabled', this.mode == 'edit');20 this.scaleSelector.set('disabled', this.mode == 'edit'); 21 21 }, 22 22 _setValueAttr: function(value) { 23 23 this.value = value; 24 this._onTypeChange(value. type || 'string');24 this._onTypeChange(value.scale || 'string'); 25 25 this.ourForm.set('value',value); 26 26 }, … … 30 30 return this.value; 31 31 }, 32 _onTypeChange: function( type) {33 if ( this._ type == type ) return;34 this._ type = type;35 domConstruct.empty(this. typeDetails);36 var widget = this._getTypeWidget( type);37 widget && widget.placeAt(this. typeDetails,'only');32 _onTypeChange: function(scale) { 33 if ( this._scale == scale ) return; 34 this._scale = scale; 35 domConstruct.empty(this.scaleDetails); 36 var widget = this._getTypeWidget(scale); 37 widget && widget.placeAt(this.scaleDetails,'only'); 38 38 }, 39 _getTypeWidget: function( type) {40 var widget = this._widgetCache[ type];39 _getTypeWidget: function(scale) { 40 var widget = this._widgetCache[scale]; 41 41 if (!widget) { 42 switch( type) {42 switch(scale) { 43 43 case 'string': 44 44 widget = new TextBox({ … … 59 59 name: 'answers', 60 60 mode: this.mode, 61 allowMultiple: type == 'multipleChoice'61 allowMultiple: scale == 'multipleChoice' 62 62 }); 63 63 break; 64 64 } 65 this._widgetCache[ type] = widget;65 this._widgetCache[scale] = widget; 66 66 } 67 67 return widget; -
Dev/branches/rest-dojo-ui/client/rft/ui/templates/QuestionWidget.html
r303 r343 12 12 </fieldset> 13 13 <fieldset> 14 <select data-dojo-type="dijit.form.Select" data-dojo-attach-point=" typeSelector" data-dojo-attach-event="onChange:_onTypeChange" name="type">14 <select data-dojo-type="dijit.form.Select" data-dojo-attach-point="scaleSelector" data-dojo-attach-event="onChange:_onTypeChange" name="scale"> 15 15 <option value="string" selected>String</option> 16 16 <option value="text">Text</option> … … 18 18 <option value="multipleChoice">Multiple choice</option> 19 19 </select> 20 <div data-dojo-attach-point=" typeDetails"></div>20 <div data-dojo-attach-point="scaleDetails"></div> 21 21 </fieldset> 22 22 </form>
Note: See TracChangeset
for help on using the changeset viewer.