Ignore:
Timestamp:
06/18/12 12:51:58 (13 years ago)
Author:
hendrikvanantwerpen
Message:

[Client] Changed store and pages to use CouchDB.
[Client] Disabled login for now, have to figure out some more details about CouchDB.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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){
     1define(['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){
    33   
    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 || {};
    846
    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);
    14160            }
    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/'});
    68164
    69165    });
Note: See TracChangeset for help on using the changeset viewer.