📄 jsonpathstore.js
字号:
cleanMeta: function(data, options){ // summary // Recurses through 'data' and removes an // meta information that has been attached. This // function will also removes any id's that were autogenerated // from objects. It will not touch id's that were not generated data = data || this._data; if (data[this.metaLabel]){ if(data[this.metaLabel]["autoId"]){ delete data[this.idAttribute]; } delete data[this.metaLabel]; } if (dojo.isArray(data)){ for(var i=0; i<data.length;i++){ if(dojo.isObject(data[i]) || dojo.isArray(data[i]) ){ this.cleanMeta(data[i]); } } } else if (dojo.isObject(data)){ for (var i in data){ this.cleanMeta(data[i]); } } }, fetch: function(args){ //console.log("fetch() ", args); // summary // // fetch takes either a string argument or a keywordArgs // object containing the parameters for the search. // If passed a string, fetch will interpret this string // as the query to be performed and will do so in // SYNC_MODE returning the results immediately. // If an object is supplied as 'args', its options will be // parsed and then contained query executed. // // query: /* string or object */ // Defaults to "$..*". jsonPath query to be performed // on data store. **note that since some widgets // expect this to be an object, an object in the form // of {query: '$[*'], queryOptions: "someOptions"} is // acceptable // // mode: dojox.data.SYNC_MODE || dojox.data.ASYNC_MODE // Override the stores default mode. // // queryOptions: /* object */ // Options passed on to the underlying jsonPath query // system. // // start: /* int */ // Starting item in result set // // count: /* int */ // Maximum number of items to return // // sort: /* function */ // Not Implemented yet // // The following only apply to ASYNC requests (the default) // // onBegin: /* function */ // called before any results are returned. Parameters // will be the count and the original fetch request // // onItem: /*function*/ // called for each returned item. Parameters will be // the item and the fetch request // // onComplete: /* function */ // called on completion of the request. Parameters will // be the complete result set and the request // // onError: /* function */ // colled in the event of an error // we're not started yet, add this request to a queue and wait till we do if (!this._data){ this._fetchQueue.push(args); return args; } if(dojo.isString(args)){ query = args; args={query: query, mode: dojox.data.SYNC_MODE}; } var query; if (!args || !args.query){ if (!args){ var args={}; } if (!args.query){ args.query="$..*"; query=args.query; } } if (dojo.isObject(args.query)){ if (args.query.query){ query = args.query.query; }else{ query = args.query = "$..*"; } if (args.query.queryOptions){ args.queryOptions=args.query.queryOptions } }else{ query=args.query; } if (!args.mode) {args.mode = this.mode;} if (!args.queryOptions) {args.queryOptions={};} args.queryOptions.resultType='BOTH'; var results = dojox.jsonPath.query(this._data, query, args.queryOptions); var tmp=[]; var count=0; for (var i=0; i<results.length; i++){ if(args.start && i<args.start){continue;} if (args.count && (count >= args.count)) { continue; } var item = results[i]["value"]; var path = results[i]["path"]; if (!dojo.isObject(item)){continue;} if(this.metaRegex.exec(path)){continue;} //this automatically records the objects path this._updateMeta(item,{path: results[i].path}); //if autoIdentity and no id, generate one and add it to the item if(this.autoIdentity && !item[this.idAttribute]){ var newId = this.autoIdPrefix + this._autoId++; item[this.idAttribute]=newId; item[this.metaLabel]["autoId"]=true; } //add item to the item index if appropriate if(item[this.idAttribute]){this.index[item[this.idAttribute]]=item} count++; tmp.push(item); } results = tmp; var scope = args.scope || dojo.global; if ("sort" in args){ console.log("TODO::add support for sorting in the fetch"); } if (args.mode==dojox.data.SYNC_MODE){ return results; }; if (args.onBegin){ args["onBegin"].call(scope, results.length, args); } if (args.onItem){ for (var i=0; i<results.length;i++){ args["onItem"].call(scope, results[i], args); } } if (args.onComplete){ args["onComplete"].call(scope, results, args); } return args; }, dump: function(options){ // summary: // // exports the store data set. Takes an options // object with a number of parameters // // data: /* object */ // Defaults to the root of the store. // The data to be exported. // // clone: /* boolean */ // clone the data set before returning it // or modifying it for export // // cleanMeta: /* boolean */ // clean the meta data off of the data. Note // that this will happen to the actual // store data if !clone. If you want // to continue using the store after // this operation, it is probably better to export // it as a clone if you want it cleaned. // // suppressExportMeta: /* boolean */ // By default, when data is exported from the store // some information, such as as a timestamp, is // added to the root of exported data. This // prevents that from happening. It is mainly used // for making tests easier. // // type: "raw" || "json" // Defaults to 'json'. 'json' will convert the data into // json before returning it. 'raw' will just return a // reference to the object var options = options || {}; var d=options.data || this._data; if (!options.suppressExportMeta && options.clone){ data = dojo.clone(d); if (data[this.metaLabel]){ data[this.metaLabel]["clone"]=true; } }else{ var data=d; } if (!options.suppressExportMeta && data[this.metaLabel]){ data[this.metaLabel]["last_export"]=new Date().toString() } if(options.cleanMeta){ this.cleanMeta(data); } //console.log("Exporting: ", options, dojo.toJson(data)); switch(options.type){ case "raw": return data; case "json": default: return dojo.toJson(data); } }, getFeatures: function(){ // summary: // return the store feature set return { "dojo.data.api.Read": true, "dojo.data.api.Identity": true, "dojo.data.api.Write": true, "dojo.data.api.Notification": true } }, getLabel: function(item){ // summary // returns the label for an item. The label // is created by setting the store's labelAttribute // property with either an attribute name or an array // of attribute names. Developers can also // provide the store with a createLabel function which // will do the actaul work of creating the label. If not // the default will just concatenate any of the identified // attributes together. item = this._correctReference(item); var label=""; if (dojo.isFunction(this.createLabel)){ return this.createLabel(item); } if (this.labelAttribute){ if (dojo.isArray(this.labelAttribute)) { for(var i=0; i<this.labelAttribute.length; i++){ if (i>0) { label+=" ";} label += item[this.labelAttribute[i]]; } return label; }else{ return item[this.labelAttribute]; } } return item.toString(); }, getLabelAttributes: function(item){ // summary: // returns an array of attributes that are used to create the label of an item item = this._correctReference(item); return dojo.isArray(this.labelAttribute) ? this.labelAttribute : [this.labelAttribute]; }, sort: function(a,b){ console.log("TODO::implement default sort algo"); }, //Identity API Support getIdentity: function(item){ // summary // returns the identity of an item or throws // a not found error. if (this.isItem(item)){ return item[this.idAttribute]; } throw new Error("Id not found for item"); }, getIdentityAttributes: function(item){ // summary: // returns the attributes which are used to make up the // identity of an item. Basically returns this.idAttribute return [this.idAttribute]; }, fetchItemByIdentity: function(args){ // summary: // fetch an item by its identity. This store also provides // a much more finger friendly alias, 'byId' which does the // same thing as this function. If provided a string // this call will be treated as a SYNC request and will // return the identified item immediatly. Alternatively it // takes a object as a set of keywordArgs: // // identity: /* string */ // the id of the item you want to retrieve // // mode: dojox.data.SYNC_MODE || dojox.data.ASYNC_MODE // overrides the default store fetch mode // // onItem: /* function */ // Result call back. Passed the fetched item. // // onError: /* function */ // error callback. var id; if (dojo.isString(args)){ id = args; args = {identity: id, mode: dojox.data.SYNC_MODE} }else{ if (args){ id = args["identity"]; } if (!args.mode){args.mode = this.mode} } if (this.index && (this.index[id] || this.index["identity"])){ if (args.mode==dojox.data.SYNC_MODE){ return this.index[id]; } if (args.onItem){ args["onItem"].call(args.scope || dojo.global, this.index[id], args); } return args; }else{ if (args.mode==dojox.data.SYNC_MODE){ return false; } } if(args.onError){ args["onItem"].call(args.scope || dojo.global, new Error("Item Not Found: " + id), args); } return args; }, //Write API Support newItem: function(data, options){ // summary: // adds a new item to the store at the specified point. // Takes two parameters, data, and options. // // data: /* object */ // The data to be added in as an item. This could be a // new javascript object, or it could be an item that // already exists in the store. If it already exists in the // store, then this will be added as a reference. // // options: /* object */ // // item: /* item */ // reference to an existing store item // // attribute: /* string */ // attribute to add the item at. If this is // not provided, the item's id will be used as the // attribute name. If specified attribute is an // array, the new item will be push()d on to the // end of it. // oldValue: /* old value of item[attribute] // newValue: new value item[attribute]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -