Changeset 12322

Show
Ignore:
Timestamp:
02/08/08 15:27:28 (11 months ago)
Author:
jaredj
Message:

Updated to the DojoData? model. fixes #5724 \!strict

Location:
dojox/trunk/grid
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • dojox/trunk/grid/_data/model.js

    r12215 r12322  
    403403                this.count = 1; 
    404404                this._rowIdentities = {}; 
     405                this._currentlyProcessing = []; 
    405406                if(args){ 
    406407                        dojo.mixin(this, args); 
    407408                } 
    408409                if(this.store){ 
    409                         // NOTE: we assume Read and Identity APIs for all stores! 
    410410                        var f = this.store.getFeatures(); 
    411411                        this._canNotify = f['dojo.data.api.Notification']; 
     
    414414                        if(this._canNotify){ 
    415415                                dojo.connect(this.store, "onSet", this, "_storeDatumChange"); 
     416                                dojo.connect(this.store, "onDelete", this, "_storeDatumDelete"); 
     417                                dojo.connect(this.store, "onNew", this, "_storeDatumNew"); 
     418                        } 
     419                        if(this._canWrite) { 
     420                                dojo.connect(this.store, "revert", this, "refresh"); 
    416421                        } 
    417422                } 
     
    422427        query: { name: "*" }, // default, stupid query 
    423428        store: null, 
     429        _currentlyProcessing: null, 
    424430        _canNotify: false, 
    425431        _canWrite: false, 
     
    474480                // gets us the row object (and row index) of an item 
    475481        }, 
     482        _createRow: function(item){ 
     483                var row = {};  
     484                row.__dojo_data_item = item; 
     485                dojo.forEach(this.fields.values, function(a){ 
     486                        value = this.store.getValue(item, a.name); 
     487                        row[a.name] = (value === undefined || value === null)?"":value; 
     488                }, this); 
     489                return row; 
     490        }, 
    476491        processRows: function(items, request){ 
    477492                // console.debug(arguments); 
     
    479494                this._setupFields(items[0]); 
    480495                dojo.forEach(items, function(item, idx){ 
    481                         var row = {};  
    482                         row.__dojo_data_item = item; 
    483                         dojo.forEach(this.fields.values, function(a){ 
    484                                 value = this.store.getValue(item, a.name); 
    485                                 row[a.name] = (value === undefined || value === null)?"":value; 
    486                         }, this); 
    487                          
    488                         // FIXME: where else do we need to keep this in sync? 
    489                         //Handle stores that implement identity and try to handle those that do not. 
    490                         if (this._canIdentify) { 
    491                                 this._rowIdentities[this.store.getIdentity(item)] = {rowId: request.start+idx}; 
    492                         }else{ 
    493                                 var identity = dojo.toJson(request.query) + ":start:" + request.start + ":idx:" + idx + ":sort:" + dojo.toJson(request.sort); 
    494                                 this._rowIdentities[identity] = {rowId: request.start+idx, item: item}; 
    495                         } 
     496                        var row = this._createRow(item); 
     497                        this._setRowId(item, request.start, idx); 
    496498                        this.setRow(row, request.start+idx); 
    497499                }, this); 
     
    569571                } 
    570572        }, 
    571         _storeDatumChange: function(item, attr, oldVal, newVal){ 
    572                 // the store has changed some data under us, need to update the display 
     573        _setRowId: function(item, offset, idx){ 
     574                // FIXME: where else do we need to keep this in sync? 
     575                //Handle stores that implement identity and try to handle those that do not. 
     576                if (this._canIdentify) { 
     577                        this._rowIdentities[this.store.getIdentity(item)] = {rowId: offset+idx, item: item}; 
     578                }else{ 
     579                        var identity = dojo.toJson(this.query) + ":start:" + offset + ":idx:" + idx + ":sort:" + dojo.toJson(this.sortFields); 
     580                        this._rowIdentities[identity] = {rowId: offset+idx, item: item}; 
     581                } 
     582        }, 
     583        _getRowId: function(item, isNotItem){ 
     584                //      summary: 
     585                //              Function determine the row index for a particular item 
     586                //      item: 
     587                //              The store item to examine to determine row index. 
     588                //      isNotItem: 
     589                //              Boolean flag to indicate if the item passed is a store item or not. 
    573590                var rowId = null; 
    574591                //Handle identity and nonidentity capable stores. 
    575                 if(this._canIdentify){ 
     592                if(this._canIdentify && !isNotItem){ 
    576593                        rowId = this._rowIdentities[this.store.getIdentity(item)].rowId; 
    577594                }else{ 
     
    582599                        for(id in this._rowIdentities){ 
    583600                                if(this._rowIdentities[id].item === item){ 
    584                                         rowId = id; 
     601                                        rowId = this._rowIdentities[id].rowId; 
    585602                                        break; 
    586603                                } 
    587604                        } 
    588605                } 
     606                return rowId; 
     607        }, 
     608        _storeDatumChange: function(item, attr, oldVal, newVal){ 
     609                // the store has changed some data under us, need to update the display 
     610                var rowId = this._getRowId(item); 
    589611                var row = this.getRow(rowId); 
    590612                if(row){ 
     
    593615                        this.notify("DatumChange", [ newVal, rowId, colId ]); 
    594616                } 
     617        }, 
     618        _storeDatumDelete: function(item){ 
     619                if(dojo.indexOf(this._currentlyProcessing, item) != -1) 
     620                        return; 
     621                // the store has deleted some item under us, need to remove that item from 
     622                // the view if possible.  It may be the deleted item isn't even in the grid. 
     623                var rowId = this._getRowId(item, true); 
     624                if(rowId != null){ 
     625                        this._removeItems([rowId]); 
     626                } 
     627        }, 
     628        _storeDatumNew: function(item){ 
     629                if(this._disableNew) 
     630                        return; 
     631                // the store has added some item under us, need to add it to the view. 
     632                this._insertItem(item, this.data.length); 
     633        }, 
     634        insert: function(item, index){ 
     635                // Push the given item back to the store 
     636                this._disableNew = true; 
     637                var i = this.store.newItem(item); 
     638                this._disableNew = false; 
     639                this._insertItem(i, index); 
     640        }, 
     641        _insertItem: function(storeItem, index){ 
     642                var row = this._createRow(storeItem); 
     643                this._setRowId(storeItem, 0, index); 
     644                dojox.grid.data.Dynamic.prototype.insert.apply(this, [row, index]); 
    595645        }, 
    596646        datumChange: function(value, rowIdx, colIdx){ 
     
    618668                this.notify("Change", arguments); 
    619669        }, 
     670        remove: function(inRowIndexes){ 
     671                //      summary: 
     672                //              Function to remove a set of items from the store based on the row index. 
     673                //      inRowIndexes: 
     674                //              An array of row indexes from the grid to remove from the store. 
     675                /* Call delete on the store */  
     676                for(var i=inRowIndexes.length-1; i>=0; i--){ 
     677                        // Need to find the item, then remove each from the data store 
     678                        var item = this.data[inRowIndexes[i]].__dojo_data_item; 
     679                        this._currentlyProcessing.push(item); 
     680                        this.store.deleteItem(item); 
     681                } 
     682                /* Remove from internal data structure and the view */ 
     683                this._removeItems(arguments); 
     684                this._currentlyProcessing = []; 
     685        }, 
     686        _removeItems: function(inRowIndexes /*array*/){ 
     687                //      summary: 
     688                //              Function to remove a set of items from the store based on the row index. 
     689                //      inRowIndexes: 
     690                //              An array of row indexes from the grid to remove from the store. 
     691                dojox.grid.data.Dynamic.prototype.remove.apply(this, arguments); 
     692                // Rebuild _rowIdentities 
     693                this._rowIdentities = {}; 
     694                for (var i = 0; i < this.data.length; i++){ 
     695                        this._setRowId(this.data[i].__dojo_data_item, 0, i); 
     696                } 
     697        }, 
    620698        canSort: function(){ 
    621699                // Q: Return true and re-issue the queries? 
     
    632710        }, 
    633711        refresh: function(){ 
     712                //      summary: 
     713                //              Function to cause the model to re-query the store and rebuild the current viewport. 
    634714                this.clearData(true); 
    635715                this.requestRows();