From: <cos...@us...> - 2012-01-29 18:56:52
|
Revision: 6022 http://jaffa.svn.sourceforge.net/jaffa/?rev=6022&view=rev Author: cosbor11 Date: 2012-01-29 18:56:46 +0000 (Sun, 29 Jan 2012) Log Message: ----------- Added multi-key support for the JSonReader. The primarykey field(s) are deduced by using the meta and recordType. Modified Paths: -------------- trunk/JaffaRIA/source/html/js/extjs/ext-overrides.js Modified: trunk/JaffaRIA/source/html/js/extjs/ext-overrides.js =================================================================== --- trunk/JaffaRIA/source/html/js/extjs/ext-overrides.js 2012-01-28 23:07:03 UTC (rev 6021) +++ trunk/JaffaRIA/source/html/js/extjs/ext-overrides.js 2012-01-29 18:56:46 UTC (rev 6022) @@ -13,6 +13,7 @@ return String(s==null?'':s).toUpperCase(); }; + /** Override createAccessor which is used to generate extraction functions. * If it creates an extraction function that has nested values (ie extract part.description) * in its original state, if part is null, an NullPointerException will be thrown when the @@ -25,22 +26,97 @@ * scope the variable and prevent it being initialized each time this function is invoked * */ + Ext.override(Ext.data.JsonReader, { - createAccessor: function () { + buildExtractors : function() { //overrode buildExtractors to handle the createAccessor for getId to support multi-key records + if(this.ef){ + return; + } + var s = this.meta, Record = this.recordType, + f = Record.prototype.fields, fi = f.items, fl = f.length; + + if(s.totalProperty) { + this.getTotal = this.createAccessor(s.totalProperty); + } + if(s.successProperty) { + this.getSuccess = this.createAccessor(s.successProperty); + } + if (s.messageProperty) { + this.getMessage = this.createAccessor(s.messageProperty); + } + this.getRoot = s.root ? this.createAccessor(s.root) : function(p){return p;}; + + s.idProperty = this.determineIdProperty(s, Record); + + if(s.idProperty){ + var g = this.createAccessor(s.idProperty); //single key + if(Ext.isArray(s.idProperty)){ //composite-key + g = function(recJson){ + if(recJson) { + var keyVal = []; + for(var i = 0; i < s.idProperty.length; i++){ + keyVal.push(recJson[s.idProperty[i]]); + } + if(keyVal.length > 0){ + return keyVal; + } else{ + return null; + } + } else { + return null; + } + }; + } + this.getId = function(rec){ + var r = g(rec); + return (r === undefined || r === '') ? null : r; + }; + } else { + this.getId = function(){return null;}; + } + var ef = []; + for(var i = 0; i < fl; i++){ + f = fi[i]; + var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name; + ef.push(this.createAccessor(map)); + } + this.ef = ef; + }, + + determineIdProperty: function(meta, recordType){ //Order of precedence: meta.id || meta.idFields || meta.idProperty || metaClass.key ||recordType.metaClass, 'id' + var idProperty = null; + if(meta){ + idProperty = meta.id; + if(meta.idFields && meta.idFields.length && meta.idFields.length > 0){ + meta.idProperty = meta.idFields; + } else if(meta.idProperty && meta.idProperty !== 'id'){ //*Note: The JsonReader's constructor set the idProperty to 'id' if not defined, we want to override this + idProperty = meta.idProperty; + } + if(!idProperty && meta.metaClass){ + if(typeof meta.metaClass === 'object' && meta.metaClass.key){ + idProperty = meta.metaClass.key; + } else if(typeof meta.metaClass === 'string' && ClassMetaData && ClassMetaData[meta.metaClass] && ClassMetaData[meta.metaClass].key){ + idProperty = ClassMetaData[meta.metaClass].key; + } + } + } + if(!idProperty && recordType){ + if(recordType.defaultMetaClass && ClassMetaData[recordType.defaultMetaClass] && ClassMetaData[recordType.defaultMetaClass].key){ + idProperty = ClassMetaData[recordType.defaultMetaClass].key; + } + } + return idProperty || 'id'; + }, + createAccessor: function(){ var re = /[\[\.]/; - return function (expr) { + return function(expr){ try { - if (Ext.isEmpty(expr)) + if (Ext.isEmpty(expr)){ return Ext.emptyFn; - else if (Ext.isFunction(expr)) + } else if (Ext.isFunction(expr)){ return expr; - else if (Ext.isArray(expr)) { - // modified to support composite key, in which case, expr should be an array of key field names + } else if (Ext.isArray(expr)) { return function(obj) { - // TODO - FIXME - 7/27/11 bug 19782. consider using (obj, returnArray) to allow return - // id as an array of key values. This is useful for line 313, PanelController.js - // inside of getModifiedPanelStore(). At that point, recId returned by reader is - // expected to be an array if the key fields are not all null. var fs = expr; if (!obj) return null; var o = obj[fs[0]]; @@ -58,19 +134,17 @@ if (o.length==fs.length) o = null; return o; }; - } else + } else { return(re.test(expr)) ? new Function("obj", "try{return obj."+expr+"} catch(e){return null}") : function(obj){return obj==null?null:obj[expr];}; + } } catch(e){ return Ext.emptyFn; } }; }(), - getId: function(obj) { - // in readRecord(), getId() will be overwritten according to meta.id value. - // make it here just to avoid getId() not found error when called. - // It should return null before any record has been read in. + getId: function(obj){ return null; } }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |