2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.data.CsvStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.data.CsvStore"] = true;
|
|
|
3 |
dojo.provide("dojox.data.CsvStore");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojo.data.util.filter");
|
|
|
6 |
dojo.require("dojo.data.util.simpleFetch");
|
|
|
7 |
|
|
|
8 |
dojo.declare("dojox.data.CsvStore", null, {
|
|
|
9 |
// summary:
|
|
|
10 |
// The CsvStore implements the dojo.data.api.Read API and reads
|
|
|
11 |
// data from files in CSV (Comma Separated Values) format.
|
|
|
12 |
// All values are simple string values. References to other items
|
|
|
13 |
// are not supported as attribute values in this datastore.
|
|
|
14 |
//
|
|
|
15 |
// Example data file:
|
|
|
16 |
// name, color, age, tagline
|
|
|
17 |
// Kermit, green, 12, "Hi, I'm Kermit the Frog."
|
|
|
18 |
// Fozzie Bear, orange, 10, "Wakka Wakka Wakka!"
|
|
|
19 |
// Miss Piggy, pink, 11, "Kermie!"
|
|
|
20 |
//
|
|
|
21 |
// Note that values containing a comma must be enclosed with quotes ("")
|
|
|
22 |
// Also note that values containing quotes must be escaped with two consecutive quotes (""quoted"")
|
|
|
23 |
|
|
|
24 |
/* examples:
|
|
|
25 |
* var csvStore = new dojox.data.CsvStore({url:"movies.csv");
|
|
|
26 |
* var csvStore = new dojox.data.CsvStore({url:"http://example.com/movies.csv");
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
constructor: function(/* Object */ keywordParameters){
|
|
|
30 |
// summary: initializer
|
|
|
31 |
// keywordParameters: {url: String}
|
|
|
32 |
// keywordParameters: {data: String}
|
|
|
33 |
// keywordParameters: {label: String} The column label for the column to use for the label returned by getLabel.
|
|
|
34 |
|
|
|
35 |
this._attributes = []; // e.g. ["Title", "Year", "Producer"]
|
|
|
36 |
this._attributeIndexes = {}; // e.g. {Title: 0, Year: 1, Producer: 2}
|
|
|
37 |
this._dataArray = []; // e.g. [[<Item0>],[<Item1>],[<Item2>]]
|
|
|
38 |
this._arrayOfAllItems = []; // e.g. [{_csvId:0,_csvStore:store},...]
|
|
|
39 |
this._loadFinished = false;
|
|
|
40 |
if(keywordParameters.url){
|
|
|
41 |
this.url = keywordParameters.url;
|
|
|
42 |
}
|
|
|
43 |
this._csvData = keywordParameters.data;
|
|
|
44 |
if(keywordParameters.label){
|
|
|
45 |
this.label = keywordParameters.label;
|
|
|
46 |
}else if(this.label === ""){
|
|
|
47 |
this.label = undefined;
|
|
|
48 |
}
|
|
|
49 |
this._storeProp = "_csvStore"; // Property name for the store reference on every item.
|
|
|
50 |
this._idProp = "_csvId"; // Property name for the Item Id on every item.
|
|
|
51 |
this._features = {
|
|
|
52 |
'dojo.data.api.Read': true,
|
|
|
53 |
'dojo.data.api.Identity': true
|
|
|
54 |
};
|
|
|
55 |
this._loadInProgress = false; //Got to track the initial load to prevent duelling loads of the dataset.
|
|
|
56 |
this._queuedFetches = [];
|
|
|
57 |
},
|
|
|
58 |
|
|
|
59 |
url: "", //Declarative hook for setting Csv source url.
|
|
|
60 |
|
|
|
61 |
label: "", //Declarative hook for setting the label attribute.
|
|
|
62 |
|
|
|
63 |
_assertIsItem: function(/* item */ item){
|
|
|
64 |
// summary:
|
|
|
65 |
// This function tests whether the item passed in is indeed an item in the store.
|
|
|
66 |
// item:
|
|
|
67 |
// The item to test for being contained by the store.
|
|
|
68 |
if(!this.isItem(item)){
|
|
|
69 |
throw new Error("dojox.data.CsvStore: a function was passed an item argument that was not an item");
|
|
|
70 |
}
|
|
|
71 |
},
|
|
|
72 |
|
|
|
73 |
_assertIsAttribute: function(/* item || String */ attribute){
|
|
|
74 |
// summary:
|
|
|
75 |
// This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
|
|
|
76 |
// attribute:
|
|
|
77 |
// The attribute to test for being contained by the store.
|
|
|
78 |
if(!dojo.isString(attribute)){
|
|
|
79 |
throw new Error("dojox.data.CsvStore: a function was passed an attribute argument that was not an attribute object nor an attribute name string");
|
|
|
80 |
}
|
|
|
81 |
},
|
|
|
82 |
|
|
|
83 |
/***************************************
|
|
|
84 |
dojo.data.api.Read API
|
|
|
85 |
***************************************/
|
|
|
86 |
getValue: function( /* item */ item,
|
|
|
87 |
/* attribute || attribute-name-string */ attribute,
|
|
|
88 |
/* value? */ defaultValue){
|
|
|
89 |
// summary:
|
|
|
90 |
// See dojo.data.api.Read.getValue()
|
|
|
91 |
// Note that for the CsvStore, an empty string value is the same as no value,
|
|
|
92 |
// so the defaultValue would be returned instead of an empty string.
|
|
|
93 |
this._assertIsItem(item);
|
|
|
94 |
this._assertIsAttribute(attribute);
|
|
|
95 |
var itemValue = defaultValue;
|
|
|
96 |
if(this.hasAttribute(item, attribute)){
|
|
|
97 |
var itemData = this._dataArray[this.getIdentity(item)];
|
|
|
98 |
itemValue = itemData[this._attributeIndexes[attribute]];
|
|
|
99 |
}
|
|
|
100 |
return itemValue; //String
|
|
|
101 |
},
|
|
|
102 |
|
|
|
103 |
getValues: function(/* item */ item,
|
|
|
104 |
/* attribute || attribute-name-string */ attribute){
|
|
|
105 |
// summary:
|
|
|
106 |
// See dojo.data.api.Read.getValues()
|
|
|
107 |
// CSV syntax does not support multi-valued attributes, so this is just a
|
|
|
108 |
// wrapper function for getValue().
|
|
|
109 |
var value = this.getValue(item, attribute);
|
|
|
110 |
return (value ? [value] : []); //Array
|
|
|
111 |
},
|
|
|
112 |
|
|
|
113 |
getAttributes: function(/* item */ item){
|
|
|
114 |
// summary:
|
|
|
115 |
// See dojo.data.api.Read.getAttributes()
|
|
|
116 |
this._assertIsItem(item);
|
|
|
117 |
var attributes = [];
|
|
|
118 |
var itemData = this._dataArray[this.getIdentity(item)];
|
|
|
119 |
for(var i=0; i<itemData.length; i++){
|
|
|
120 |
// Check for empty string values. CsvStore treats empty strings as no value.
|
|
|
121 |
if(itemData[i] != ""){
|
|
|
122 |
attributes.push(this._attributes[i]);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
return attributes; //Array
|
|
|
126 |
},
|
|
|
127 |
|
|
|
128 |
hasAttribute: function( /* item */ item,
|
|
|
129 |
/* attribute || attribute-name-string */ attribute){
|
|
|
130 |
// summary:
|
|
|
131 |
// See dojo.data.api.Read.hasAttribute()
|
|
|
132 |
// The hasAttribute test is true if attribute has an index number within the item's array length
|
|
|
133 |
// AND if the item has a value for that attribute. Note that for the CsvStore, an
|
|
|
134 |
// empty string value is the same as no value.
|
|
|
135 |
this._assertIsItem(item);
|
|
|
136 |
this._assertIsAttribute(attribute);
|
|
|
137 |
var attributeIndex = this._attributeIndexes[attribute];
|
|
|
138 |
var itemData = this._dataArray[this.getIdentity(item)];
|
|
|
139 |
return (typeof attributeIndex != "undefined" && attributeIndex < itemData.length && itemData[attributeIndex] != ""); //Boolean
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
containsValue: function(/* item */ item,
|
|
|
143 |
/* attribute || attribute-name-string */ attribute,
|
|
|
144 |
/* anything */ value){
|
|
|
145 |
// summary:
|
|
|
146 |
// See dojo.data.api.Read.containsValue()
|
|
|
147 |
var regexp = undefined;
|
|
|
148 |
if(typeof value === "string"){
|
|
|
149 |
regexp = dojo.data.util.filter.patternToRegExp(value, false);
|
|
|
150 |
}
|
|
|
151 |
return this._containsValue(item, attribute, value, regexp); //boolean.
|
|
|
152 |
},
|
|
|
153 |
|
|
|
154 |
_containsValue: function( /* item */ item,
|
|
|
155 |
/* attribute || attribute-name-string */ attribute,
|
|
|
156 |
/* anything */ value,
|
|
|
157 |
/* RegExp?*/ regexp){
|
|
|
158 |
// summary:
|
|
|
159 |
// Internal function for looking at the values contained by the item.
|
|
|
160 |
// description:
|
|
|
161 |
// Internal function for looking at the values contained by the item. This
|
|
|
162 |
// function allows for denoting if the comparison should be case sensitive for
|
|
|
163 |
// strings or not (for handling filtering cases where string case should not matter)
|
|
|
164 |
//
|
|
|
165 |
// item:
|
|
|
166 |
// The data item to examine for attribute values.
|
|
|
167 |
// attribute:
|
|
|
168 |
// The attribute to inspect.
|
|
|
169 |
// value:
|
|
|
170 |
// The value to match.
|
|
|
171 |
// regexp:
|
|
|
172 |
// Optional regular expression generated off value if value was of string type to handle wildcarding.
|
|
|
173 |
// If present and attribute values are string, then it can be used for comparison instead of 'value'
|
|
|
174 |
var values = this.getValues(item, attribute);
|
|
|
175 |
for(var i = 0; i < values.length; ++i){
|
|
|
176 |
var possibleValue = values[i];
|
|
|
177 |
if(typeof possibleValue === "string" && regexp){
|
|
|
178 |
return (possibleValue.match(regexp) !== null);
|
|
|
179 |
}else{
|
|
|
180 |
//Non-string matching.
|
|
|
181 |
if(value === possibleValue){
|
|
|
182 |
return true; // Boolean
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
return false; // Boolean
|
|
|
187 |
},
|
|
|
188 |
|
|
|
189 |
isItem: function(/* anything */ something){
|
|
|
190 |
// summary:
|
|
|
191 |
// See dojo.data.api.Read.isItem()
|
|
|
192 |
if(something && something[this._storeProp] === this){
|
|
|
193 |
var identity = something[this._idProp];
|
|
|
194 |
if(identity >= 0 && identity < this._dataArray.length){
|
|
|
195 |
return true; //Boolean
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
return false; //Boolean
|
|
|
199 |
},
|
|
|
200 |
|
|
|
201 |
isItemLoaded: function(/* anything */ something){
|
|
|
202 |
// summary:
|
|
|
203 |
// See dojo.data.api.Read.isItemLoaded()
|
|
|
204 |
// The CsvStore always loads all items, so if it's an item, then it's loaded.
|
|
|
205 |
return this.isItem(something); //Boolean
|
|
|
206 |
},
|
|
|
207 |
|
|
|
208 |
loadItem: function(/* item */ item){
|
|
|
209 |
// summary:
|
|
|
210 |
// See dojo.data.api.Read.loadItem()
|
|
|
211 |
// description:
|
|
|
212 |
// The CsvStore always loads all items, so if it's an item, then it's loaded.
|
|
|
213 |
// From the dojo.data.api.Read.loadItem docs:
|
|
|
214 |
// If a call to isItemLoaded() returns true before loadItem() is even called,
|
|
|
215 |
// then loadItem() need not do any work at all and will not even invoke
|
|
|
216 |
// the callback handlers.
|
|
|
217 |
},
|
|
|
218 |
|
|
|
219 |
getFeatures: function(){
|
|
|
220 |
// summary:
|
|
|
221 |
// See dojo.data.api.Read.getFeatures()
|
|
|
222 |
return this._features; //Object
|
|
|
223 |
},
|
|
|
224 |
|
|
|
225 |
getLabel: function(/* item */ item){
|
|
|
226 |
// summary:
|
|
|
227 |
// See dojo.data.api.Read.getLabel()
|
|
|
228 |
if(this.label && this.isItem(item)){
|
|
|
229 |
return this.getValue(item,this.label); //String
|
|
|
230 |
}
|
|
|
231 |
return undefined; //undefined
|
|
|
232 |
},
|
|
|
233 |
|
|
|
234 |
getLabelAttributes: function(/* item */ item){
|
|
|
235 |
// summary:
|
|
|
236 |
// See dojo.data.api.Read.getLabelAttributes()
|
|
|
237 |
if(this.label){
|
|
|
238 |
return [this.label]; //array
|
|
|
239 |
}
|
|
|
240 |
return null; //null
|
|
|
241 |
},
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
// The dojo.data.api.Read.fetch() function is implemented as
|
|
|
245 |
// a mixin from dojo.data.util.simpleFetch.
|
|
|
246 |
// That mixin requires us to define _fetchItems().
|
|
|
247 |
_fetchItems: function( /* Object */ keywordArgs,
|
|
|
248 |
/* Function */ findCallback,
|
|
|
249 |
/* Function */ errorCallback){
|
|
|
250 |
// summary:
|
|
|
251 |
// See dojo.data.util.simpleFetch.fetch()
|
|
|
252 |
|
|
|
253 |
var self = this;
|
|
|
254 |
|
|
|
255 |
var filter = function(requestArgs, arrayOfAllItems){
|
|
|
256 |
var items = null;
|
|
|
257 |
if(requestArgs.query){
|
|
|
258 |
items = [];
|
|
|
259 |
var ignoreCase = requestArgs.queryOptions ? requestArgs.queryOptions.ignoreCase : false;
|
|
|
260 |
|
|
|
261 |
//See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the
|
|
|
262 |
//same value for each item examined. Much more efficient.
|
|
|
263 |
var regexpList = {};
|
|
|
264 |
for(var key in requestArgs.query){
|
|
|
265 |
var value = requestArgs.query[key];
|
|
|
266 |
if(typeof value === "string"){
|
|
|
267 |
regexpList[key] = dojo.data.util.filter.patternToRegExp(value, ignoreCase);
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
for(var i = 0; i < arrayOfAllItems.length; ++i){
|
|
|
272 |
var match = true;
|
|
|
273 |
var candidateItem = arrayOfAllItems[i];
|
|
|
274 |
for(var key in requestArgs.query){
|
|
|
275 |
var value = requestArgs.query[key];
|
|
|
276 |
if(!self._containsValue(candidateItem, key, value, regexpList[key])){
|
|
|
277 |
match = false;
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
if(match){
|
|
|
281 |
items.push(candidateItem);
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
}else{
|
|
|
285 |
// We want a copy to pass back in case the parent wishes to sort the array. We shouldn't allow resort
|
|
|
286 |
// of the internal list so that multiple callers can get lists and sort without affecting each other.
|
|
|
287 |
if(arrayOfAllItems.length> 0){
|
|
|
288 |
items = arrayOfAllItems.slice(0,arrayOfAllItems.length);
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
findCallback(items, requestArgs);
|
|
|
292 |
};
|
|
|
293 |
|
|
|
294 |
if(this._loadFinished){
|
|
|
295 |
filter(keywordArgs, this._arrayOfAllItems);
|
|
|
296 |
}else{
|
|
|
297 |
if(this.url !== ""){
|
|
|
298 |
//If fetches come in before the loading has finished, but while
|
|
|
299 |
//a load is in progress, we have to defer the fetching to be
|
|
|
300 |
//invoked in the callback.
|
|
|
301 |
if(this._loadInProgress){
|
|
|
302 |
this._queuedFetches.push({args: keywordArgs, filter: filter});
|
|
|
303 |
}else{
|
|
|
304 |
this._loadInProgress = true;
|
|
|
305 |
var getArgs = {
|
|
|
306 |
url: self.url,
|
|
|
307 |
handleAs: "text"
|
|
|
308 |
};
|
|
|
309 |
var getHandler = dojo.xhrGet(getArgs);
|
|
|
310 |
getHandler.addCallback(function(data){
|
|
|
311 |
self._processData(data);
|
|
|
312 |
filter(keywordArgs, self._arrayOfAllItems);
|
|
|
313 |
self._handleQueuedFetches();
|
|
|
314 |
});
|
|
|
315 |
getHandler.addErrback(function(error){
|
|
|
316 |
self._loadInProgress = false;
|
|
|
317 |
throw error;
|
|
|
318 |
});
|
|
|
319 |
}
|
|
|
320 |
}else if(this._csvData){
|
|
|
321 |
this._processData(this._csvData);
|
|
|
322 |
this._csvData = null;
|
|
|
323 |
filter(keywordArgs, this._arrayOfAllItems);
|
|
|
324 |
}else{
|
|
|
325 |
throw new Error("dojox.data.CsvStore: No CSV source data was provided as either URL or String data input.");
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
},
|
|
|
329 |
|
|
|
330 |
close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
|
|
|
331 |
// summary:
|
|
|
332 |
// See dojo.data.api.Read.close()
|
|
|
333 |
},
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
// -------------------------------------------------------------------
|
|
|
337 |
// Private methods
|
|
|
338 |
_getArrayOfArraysFromCsvFileContents: function(/* string */ csvFileContents){
|
|
|
339 |
/* summary:
|
|
|
340 |
* Parses a string of CSV records into a nested array structure.
|
|
|
341 |
* description:
|
|
|
342 |
* Given a string containing CSV records, this method parses
|
|
|
343 |
* the string and returns a data structure containing the parsed
|
|
|
344 |
* content. The data structure we return is an array of length
|
|
|
345 |
* R, where R is the number of rows (lines) in the CSV data. The
|
|
|
346 |
* return array contains one sub-array for each CSV line, and each
|
|
|
347 |
* sub-array contains C string values, where C is the number of
|
|
|
348 |
* columns in the CSV data.
|
|
|
349 |
*/
|
|
|
350 |
|
|
|
351 |
/* example:
|
|
|
352 |
* For example, given this CSV string as input:
|
|
|
353 |
* "Title, Year, Producer \n Alien, 1979, Ridley Scott \n Blade Runner, 1982, Ridley Scott"
|
|
|
354 |
* this._dataArray will be set to:
|
|
|
355 |
* [["Alien", "1979", "Ridley Scott"],
|
|
|
356 |
* ["Blade Runner", "1982", "Ridley Scott"]]
|
|
|
357 |
* And this._attributes will be set to:
|
|
|
358 |
* ["Title", "Year", "Producer"]
|
|
|
359 |
* And this._attributeIndexes will be set to:
|
|
|
360 |
* { "Title":0, "Year":1, "Producer":2 }
|
|
|
361 |
*/
|
|
|
362 |
if(dojo.isString(csvFileContents)){
|
|
|
363 |
var lineEndingCharacters = new RegExp("\r\n|\n|\r");
|
|
|
364 |
var leadingWhiteSpaceCharacters = new RegExp("^\\s+",'g');
|
|
|
365 |
var trailingWhiteSpaceCharacters = new RegExp("\\s+$",'g');
|
|
|
366 |
var doubleQuotes = new RegExp('""','g');
|
|
|
367 |
var arrayOfOutputRecords = [];
|
|
|
368 |
|
|
|
369 |
var arrayOfInputLines = csvFileContents.split(lineEndingCharacters);
|
|
|
370 |
for(var i = 0; i < arrayOfInputLines.length; ++i){
|
|
|
371 |
var singleLine = arrayOfInputLines[i];
|
|
|
372 |
if(singleLine.length > 0){
|
|
|
373 |
var listOfFields = singleLine.split(',');
|
|
|
374 |
var j = 0;
|
|
|
375 |
while(j < listOfFields.length){
|
|
|
376 |
var space_field_space = listOfFields[j];
|
|
|
377 |
var field_space = space_field_space.replace(leadingWhiteSpaceCharacters, ''); // trim leading whitespace
|
|
|
378 |
var field = field_space.replace(trailingWhiteSpaceCharacters, ''); // trim trailing whitespace
|
|
|
379 |
var firstChar = field.charAt(0);
|
|
|
380 |
var lastChar = field.charAt(field.length - 1);
|
|
|
381 |
var secondToLastChar = field.charAt(field.length - 2);
|
|
|
382 |
var thirdToLastChar = field.charAt(field.length - 3);
|
|
|
383 |
if(field.length === 2 && field == "\"\""){
|
|
|
384 |
listOfFields[j] = ""; //Special case empty string field.
|
|
|
385 |
}else if((firstChar == '"') &&
|
|
|
386 |
((lastChar != '"') ||
|
|
|
387 |
((lastChar == '"') && (secondToLastChar == '"') && (thirdToLastChar != '"')))){
|
|
|
388 |
if(j+1 === listOfFields.length){
|
|
|
389 |
// alert("The last field in record " + i + " is corrupted:\n" + field);
|
|
|
390 |
return null; //null
|
|
|
391 |
}
|
|
|
392 |
var nextField = listOfFields[j+1];
|
|
|
393 |
listOfFields[j] = field_space + ',' + nextField;
|
|
|
394 |
listOfFields.splice(j+1, 1); // delete element [j+1] from the list
|
|
|
395 |
}else{
|
|
|
396 |
if((firstChar == '"') && (lastChar == '"')){
|
|
|
397 |
field = field.slice(1, (field.length - 1)); // trim the " characters off the ends
|
|
|
398 |
field = field.replace(doubleQuotes, '"'); // replace "" with "
|
|
|
399 |
}
|
|
|
400 |
listOfFields[j] = field;
|
|
|
401 |
j += 1;
|
|
|
402 |
}
|
|
|
403 |
}
|
|
|
404 |
arrayOfOutputRecords.push(listOfFields);
|
|
|
405 |
}
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
// The first item of the array must be the header row with attribute names.
|
|
|
409 |
this._attributes = arrayOfOutputRecords.shift();
|
|
|
410 |
for(var i=0; i<this._attributes.length; i++){
|
|
|
411 |
// Store the index of each attribute
|
|
|
412 |
this._attributeIndexes[this._attributes[i]] = i;
|
|
|
413 |
}
|
|
|
414 |
this._dataArray = arrayOfOutputRecords; //Array
|
|
|
415 |
}
|
|
|
416 |
},
|
|
|
417 |
|
|
|
418 |
_processData: function(/* String */ data){
|
|
|
419 |
this._getArrayOfArraysFromCsvFileContents(data);
|
|
|
420 |
this._arrayOfAllItems = [];
|
|
|
421 |
for(var i=0; i<this._dataArray.length; i++){
|
|
|
422 |
this._arrayOfAllItems.push(this._createItemFromIdentity(i));
|
|
|
423 |
}
|
|
|
424 |
this._loadFinished = true;
|
|
|
425 |
this._loadInProgress = false;
|
|
|
426 |
},
|
|
|
427 |
|
|
|
428 |
_createItemFromIdentity: function(/* String */ identity){
|
|
|
429 |
var item = {};
|
|
|
430 |
item[this._storeProp] = this;
|
|
|
431 |
item[this._idProp] = identity;
|
|
|
432 |
return item; //Object
|
|
|
433 |
},
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
/***************************************
|
|
|
437 |
dojo.data.api.Identity API
|
|
|
438 |
***************************************/
|
|
|
439 |
getIdentity: function(/* item */ item){
|
|
|
440 |
// summary:
|
|
|
441 |
// See dojo.data.api.Identity.getIdentity()
|
|
|
442 |
if(this.isItem(item)){
|
|
|
443 |
return item[this._idProp]; //String
|
|
|
444 |
}
|
|
|
445 |
return null; //null
|
|
|
446 |
},
|
|
|
447 |
|
|
|
448 |
fetchItemByIdentity: function(/* Object */ keywordArgs){
|
|
|
449 |
// summary:
|
|
|
450 |
// See dojo.data.api.Identity.fetchItemByIdentity()
|
|
|
451 |
|
|
|
452 |
//Hasn't loaded yet, we have to trigger the load.
|
|
|
453 |
|
|
|
454 |
|
|
|
455 |
if(!this._loadFinished){
|
|
|
456 |
var self = this;
|
|
|
457 |
if(this.url !== ""){
|
|
|
458 |
//If fetches come in before the loading has finished, but while
|
|
|
459 |
//a load is in progress, we have to defer the fetching to be
|
|
|
460 |
//invoked in the callback.
|
|
|
461 |
if(this._loadInProgress){
|
|
|
462 |
this._queuedFetches.push({args: keywordArgs});
|
|
|
463 |
}else{
|
|
|
464 |
this._loadInProgress = true;
|
|
|
465 |
var getArgs = {
|
|
|
466 |
url: self.url,
|
|
|
467 |
handleAs: "text"
|
|
|
468 |
};
|
|
|
469 |
var getHandler = dojo.xhrGet(getArgs);
|
|
|
470 |
getHandler.addCallback(function(data){
|
|
|
471 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
472 |
try{
|
|
|
473 |
self._processData(data);
|
|
|
474 |
var item = self._createItemFromIdentity(keywordArgs.identity);
|
|
|
475 |
if(!self.isItem(item)){
|
|
|
476 |
item = null;
|
|
|
477 |
}
|
|
|
478 |
if(keywordArgs.onItem){
|
|
|
479 |
keywordArgs.onItem.call(scope, item);
|
|
|
480 |
}
|
|
|
481 |
self._handleQueuedFetches();
|
|
|
482 |
}catch(error){
|
|
|
483 |
if(keywordArgs.onError){
|
|
|
484 |
keywordArgs.onError.call(scope, error);
|
|
|
485 |
}
|
|
|
486 |
}
|
|
|
487 |
});
|
|
|
488 |
getHandler.addErrback(function(error){
|
|
|
489 |
this._loadInProgress = false;
|
|
|
490 |
if(keywordArgs.onError){
|
|
|
491 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
492 |
keywordArgs.onError.call(scope, error);
|
|
|
493 |
}
|
|
|
494 |
});
|
|
|
495 |
}
|
|
|
496 |
}else if(this._csvData){
|
|
|
497 |
self._processData(self._csvData);
|
|
|
498 |
self._csvData = null;
|
|
|
499 |
var item = self._createItemFromIdentity(keywordArgs.identity);
|
|
|
500 |
if(!self.isItem(item)){
|
|
|
501 |
item = null;
|
|
|
502 |
}
|
|
|
503 |
if(keywordArgs.onItem){
|
|
|
504 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
505 |
keywordArgs.onItem.call(scope, item);
|
|
|
506 |
}
|
|
|
507 |
}
|
|
|
508 |
}else{
|
|
|
509 |
//Already loaded. We can just look it up and call back.
|
|
|
510 |
var item = this._createItemFromIdentity(keywordArgs.identity);
|
|
|
511 |
if(!this.isItem(item)){
|
|
|
512 |
item = null;
|
|
|
513 |
}
|
|
|
514 |
if(keywordArgs.onItem){
|
|
|
515 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
516 |
keywordArgs.onItem.call(scope, item);
|
|
|
517 |
}
|
|
|
518 |
}
|
|
|
519 |
},
|
|
|
520 |
|
|
|
521 |
getIdentityAttributes: function(/* item */ item){
|
|
|
522 |
// summary:
|
|
|
523 |
// See dojo.data.api.Identity.getIdentifierAttributes()
|
|
|
524 |
|
|
|
525 |
//Identity isn't a public attribute in the item, it's the row position index.
|
|
|
526 |
//So, return null.
|
|
|
527 |
return null;
|
|
|
528 |
},
|
|
|
529 |
|
|
|
530 |
_handleQueuedFetches: function(){
|
|
|
531 |
// summary:
|
|
|
532 |
// Internal function to execute delayed request in the store.
|
|
|
533 |
//Execute any deferred fetches now.
|
|
|
534 |
if (this._queuedFetches.length > 0) {
|
|
|
535 |
for(var i = 0; i < this._queuedFetches.length; i++){
|
|
|
536 |
var fData = this._queuedFetches[i];
|
|
|
537 |
var delayedFilter = fData.filter;
|
|
|
538 |
var delayedQuery = fData.args;
|
|
|
539 |
if(delayedFilter){
|
|
|
540 |
delayedFilter(delayedQuery, this._arrayOfAllItems);
|
|
|
541 |
}else{
|
|
|
542 |
this.fetchItemByIdentity(fData.args);
|
|
|
543 |
}
|
|
|
544 |
}
|
|
|
545 |
this._queuedFetches = [];
|
|
|
546 |
}
|
|
|
547 |
}
|
|
|
548 |
});
|
|
|
549 |
//Mix in the simple fetch implementation to this class.
|
|
|
550 |
dojo.extend(dojox.data.CsvStore,dojo.data.util.simpleFetch);
|
|
|
551 |
|
|
|
552 |
}
|