Changeset 363


Ignore:
Timestamp:
07/17/12 11:52:08 (13 years ago)
Author:
hendrikvanantwerpen
Message:

Fixed incompatibility of store with defined interface
dojo.store.Store. Added custom forPairs function to query results and
included documentation about query options.

Location:
Dev/branches/rest-dojo-ui/client/rft
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui/client/rft/pages/survey.js

    r362 r363  
    4646                }));
    4747                store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:1})
    48                 .forEach(lang.hitch(this,function(value,key){
     48                .forPairs(lang.hitch(this,function(value,key){
    4949                    this._createCategoryTab(key[0],value);
    5050                }));
     
    6868                    categoryMap._filled = true;
    6969                    store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]})
    70                     .forEach(lang.hitch(this,function(value,key){
     70                    .forPairs(lang.hitch(this,function(value,key){
    7171                        this._createTopicSelector(key[1],category,value);
    7272                    }));
     
    9494                if (!topicMap._filled) {
    9595                    store.query('_design/default/_view/questions', {reduce:false,include_docs:true,key:[category,topic]})
    96                     .forEach(lang.hitch(this,function(value,key){
     96                    .forEach(lang.hitch(this,function(value){
    9797                        topicMap._widget.addItem(value);
    9898                    }));
  • Dev/branches/rest-dojo-ui/client/rft/store.js

    r362 r363  
    22    function(declare,lang,array,Deferred,xhr,JSON,QueryResults){
    33   
    4         var CouchStore = declare("rft.store", null, {
     4        var CouchStore = declare(null, {
    55            /** dojo.Store implementation for CouchDB
    66             *
     
    4343            },
    4444                put: function(object, options){
     45                 // summary:
     46                 //     put an object in CouchDB
     47                 // object: Object
     48                 //     The object to put
     49                 // options: Object
     50                 //     Options object as
     51                 //         id: String
     52                 //
    4553                        options = options || {};
    4654
     
    9199            },
    92100            query: function(query, options){
     101                // summary:
     102                //    query a couchdb view
     103                // query: String
     104                //    name of a couchdb view you want to query, relative to the current database
     105                // options: Object
     106                //     options object as
     107                //        start: Number
     108                //            Start results at this item
     109                //        count: Number
     110                //            Number of items to return
     111                //        sort: [{attribute:'key',descending:true|false}]
     112                //            CouchDB only support sorting by key, so only 'key'
     113                //            is allowed as attribute value. Multiple sort items
     114                //            are ignored.
     115                //        key: String|Array|Object
     116                //            Return only values with this key.
     117                //            Excludes start/endkey usage.
     118                //        startkey: String|Array|Object
     119                //            Return values starting from this key.
     120                //        endkey: String|Array|Object
     121                //            Return values with key lower than this key.
     122                //        include_docs: true|false
     123                //            Return the full documents instead of the view
     124                //            values.
     125                //        reduce: true|false
     126                //            Execute reduce on the view or not. Default depends
     127                //            on if a reduce function is defined on the view.
     128                //        group: true|false
     129                //            Should values be grouped per key or not? Default
     130                //            is false.
     131                //        group_level: Number
     132                //            When group = true and the key is an array,
     133                //            determines which elements starting from the first
     134                //            are used for grouping. Default is 0.
     135                //        get_keys: true|false
     136                //            Instead of returning the values or documents,
     137                //            return the array of keys as the result.
     138                //            This does not affect the forPairs function.
    93139                        options = options || {};
    94140
     
    161207                        dfd.reject(result.reason);
    162208                    } else  {
    163                         dfd.resolve(
    164                             array.map(result.rows,
    165                                       function(result){
    166                                           return [result.key, options.include_docs === true ? result.doc : result.value];
    167                                       }));
     209                        var results;
     210                        var values = array.map(result.rows,function(result){
     211                            return options.include_docs === true ? result.doc : result.value;
     212                        });
     213                        var keys = array.map(result.rows,function(result){
     214                            return result.key;
     215                        });
     216                        if (options.get_keys === true) {
     217                            results = keys;
     218                            results.values = values;
     219                        } else {
     220                            results = values;
     221                            results.keys = keys;
     222                        }
     223                        dfd.resolve(results);
    168224                    }
    169225                },function(err){
     
    175231
    176232        function CouchResults(results) {
    177             if (!results) {
    178                 return results;
     233            results = QueryResults(results);
     234            results.forPairs = function(callback,thisObject) {
     235                callback = lang.hitch(thisObject,callback);
     236                return Deferred.when(results,function(results) {
     237                    var values = results.values || results;
     238                    var keys = results.keys || results;
     239                    return array.forEach(values, function(value,index) {
     240                        callback(value,keys[index],index);
     241                    });
     242                });
    179243            }
    180 
    181                 if(results.then){
    182                         results = lang.delegate(results);
    183                 }
    184 
    185             if (!results.forEach) {
    186                 results.forEach = function(callback) {
    187                     return Deferred.when(results, function(results) {
    188                         array.forEach(results, function(result) {
    189                             callback(result[1],result[0]);
    190                         });
    191                     });
    192                 }
    193             }
    194 
    195244            return results;
    196245        }
Note: See TracChangeset for help on using the changeset viewer.