]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/data/ItemFileReadStore.js
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if (! dojo
. _hasResource
[ "dojo.data.ItemFileReadStore" ]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
. _hasResource
[ "dojo.data.ItemFileReadStore" ] = true ;
10 dojo
. provide ( "dojo.data.ItemFileReadStore" );
12 dojo
. require ( "dojo.data.util.filter" );
13 dojo
. require ( "dojo.data.util.simpleFetch" );
14 dojo
. require ( "dojo.date.stamp" );
16 dojo
. declare ( "dojo.data.ItemFileReadStore" , null ,{
18 // The ItemFileReadStore implements the dojo.data.api.Read API and reads
19 // data from JSON files that have contents in this format --
21 // { name:'Kermit', color:'green', age:12, friends:['Gonzo', {_reference:{name:'Fozzie Bear'}}]},
22 // { name:'Fozzie Bear', wears:['hat', 'tie']},
23 // { name:'Miss Piggy', pets:'Foo-Foo'}
25 // Note that it can also contain an 'identifer' property that specified which attribute on the items
26 // in the array of items that acts as the unique identifier for that item.
28 constructor : function ( /* Object */ keywordParameters
){
29 // summary: constructor
30 // keywordParameters: {url: String}
31 // keywordParameters: {data: jsonObject}
32 // keywordParameters: {typeMap: object)
33 // The structure of the typeMap object is as follows:
35 // type0: function || object,
36 // type1: function || object,
38 // typeN: function || object
40 // Where if it is a function, it is assumed to be an object constructor that takes the
41 // value of _value as the initialization parameters. If it is an object, then it is assumed
42 // to be an object of general form:
44 // type: function, //constructor.
45 // deserialize: function(value) //The function that parses the value and constructs the object defined by type appropriately.
48 this . _arrayOfAllItems
= [];
49 this . _arrayOfTopLevelItems
= [];
50 this . _loadFinished
= false ;
51 this . _jsonFileUrl
= keywordParameters
. url
;
52 this . _ccUrl
= keywordParameters
. url
;
53 this . url
= keywordParameters
. url
;
54 this . _jsonData
= keywordParameters
. data
;
56 this . _datatypeMap
= keywordParameters
. typeMap
|| {};
57 if (! this . _datatypeMap
[ 'Date' ]){
58 //If no default mapping for dates, then set this as default.
59 //We use the dojo.date.stamp here because the ISO format is the 'dojo way'
60 //of generically representing dates.
61 this . _datatypeMap
[ 'Date' ] = {
63 deserialize : function ( value
){
64 return dojo
. date
. stamp
. fromISOString ( value
);
68 this . _features
= { 'dojo.data.api.Read' : true , 'dojo.data.api.Identity' : true };
69 this . _itemsByIdentity
= null ;
70 this . _storeRefPropName
= "_S" ; // Default name for the store reference to attach to every item.
71 this . _itemNumPropName
= "_0" ; // Default Item Id for isItem to attach to every item.
72 this . _rootItemPropName
= "_RI" ; // Default Item Id for isItem to attach to every item.
73 this . _reverseRefMap
= "_RRM" ; // Default attribute for constructing a reverse reference map for use with reference integrity
74 this . _loadInProgress
= false ; //Got to track the initial load to prevent duelling loads of the dataset.
75 this . _queuedFetches
= [];
76 if ( keywordParameters
. urlPreventCache
!== undefined ){
77 this . urlPreventCache
= keywordParameters
. urlPreventCache
? true : false ;
79 if ( keywordParameters
. hierarchical
!== undefined ){
80 this . hierarchical
= keywordParameters
. hierarchical
? true : false ;
82 if ( keywordParameters
. clearOnClose
){
83 this . clearOnClose
= true ;
85 if ( "failOk" in keywordParameters
){
86 this . failOk
= keywordParameters
. failOk
? true : false ;
90 url
: "" , // use "" rather than undefined for the benefit of the parser (#3539)
92 //Internal var, crossCheckUrl. Used so that setting either url or _jsonFileUrl, can still trigger a reload
93 //when clearOnClose and close is used.
96 data
: null , // define this so that the parser can populate it
98 typeMap
: null , //Define so parser can populate.
100 //Parameter to allow users to specify if a close call should force a reload or not.
101 //By default, it retains the old behavior of not clearing if close is called. But
102 //if set true, the store will be reset to default state. Note that by doing this,
103 //all item handles will become invalid and a new fetch must be issued.
106 //Parameter to allow specifying if preventCache should be passed to the xhrGet call or not when loading data from a url.
107 //Note this does not mean the store calls the server on each fetch, only that the data load has preventCache set as an option.
108 //Added for tracker: #6072
109 urlPreventCache
: false ,
111 //Parameter for specifying that it is OK for the xhrGet call to fail silently.
114 //Parameter to indicate to process data from the url as hierarchical
115 //(data items can contain other data items in js form). Default is true
116 //for backwards compatibility. False means only root items are processed
117 //as items, all child objects outside of type-mapped objects and those in
118 //specific reference format, are left straight JS data objects.
121 _assertIsItem : function ( /* item */ item
){
123 // This function tests whether the item passed in is indeed an item in the store.
125 // The item to test for being contained by the store.
126 if (! this . isItem ( item
)){
127 throw new Error ( "dojo.data.ItemFileReadStore: Invalid item argument." );
131 _assertIsAttribute : function ( /* attribute-name-string */ attribute
){
133 // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
135 // The attribute to test for being contained by the store.
136 if ( typeof attribute
!== "string" ){
137 throw new Error ( "dojo.data.ItemFileReadStore: Invalid attribute argument." );
141 getValue : function ( /* item */ item
,
142 /* attribute-name-string */ attribute
,
143 /* value? */ defaultValue
){
145 // See dojo.data.api.Read.getValue()
146 var values
= this . getValues ( item
, attribute
);
147 return ( values
. length
> 0 )? values
[ 0 ]: defaultValue
; // mixed
150 getValues : function ( /* item */ item
,
151 /* attribute-name-string */ attribute
){
153 // See dojo.data.api.Read.getValues()
155 this . _assertIsItem ( item
);
156 this . _assertIsAttribute ( attribute
);
157 // Clone it before returning. refs: #10474
158 return ( item
[ attribute
] || []). slice ( 0 ); // Array
161 getAttributes : function ( /* item */ item
){
163 // See dojo.data.api.Read.getAttributes()
164 this . _assertIsItem ( item
);
166 for ( var key
in item
){
167 // Save off only the real item attributes, not the special id marks for O(1) isItem.
168 if (( key
!== this . _storeRefPropName
) && ( key
!== this . _itemNumPropName
) && ( key
!== this . _rootItemPropName
) && ( key
!== this . _reverseRefMap
)){
169 attributes
. push ( key
);
172 return attributes
; // Array
175 hasAttribute : function ( /* item */ item
,
176 /* attribute-name-string */ attribute
){
178 // See dojo.data.api.Read.hasAttribute()
179 this . _assertIsItem ( item
);
180 this . _assertIsAttribute ( attribute
);
181 return ( attribute
in item
);
184 containsValue : function ( /* item */ item
,
185 /* attribute-name-string */ attribute
,
186 /* anything */ value
){
188 // See dojo.data.api.Read.containsValue()
189 var regexp
= undefined ;
190 if ( typeof value
=== "string" ){
191 regexp
= dojo
. data
. util
. filter
. patternToRegExp ( value
, false );
193 return this . _containsValue ( item
, attribute
, value
, regexp
); //boolean.
196 _containsValue : function ( /* item */ item
,
197 /* attribute-name-string */ attribute
,
198 /* anything */ value
,
199 /* RegExp?*/ regexp
){
201 // Internal function for looking at the values contained by the item.
203 // Internal function for looking at the values contained by the item. This
204 // function allows for denoting if the comparison should be case sensitive for
205 // strings or not (for handling filtering cases where string case should not matter)
208 // The data item to examine for attribute values.
210 // The attribute to inspect.
212 // The value to match.
214 // Optional regular expression generated off value if value was of string type to handle wildcarding.
215 // If present and attribute values are string, then it can be used for comparison instead of 'value'
216 return dojo
. some ( this . getValues ( item
, attribute
), function ( possibleValue
){
217 if ( possibleValue
!== null && ! dojo
. isObject ( possibleValue
) && regexp
){
218 if ( possibleValue
. toString (). match ( regexp
)){
219 return true ; // Boolean
221 } else if ( value
=== possibleValue
){
222 return true ; // Boolean
227 isItem : function ( /* anything */ something
){
229 // See dojo.data.api.Read.isItem()
230 if ( something
&& something
[ this . _storeRefPropName
] === this ){
231 if ( this . _arrayOfAllItems
[ something
[ this . _itemNumPropName
]] === something
){
235 return false ; // Boolean
238 isItemLoaded : function ( /* anything */ something
){
240 // See dojo.data.api.Read.isItemLoaded()
241 return this . isItem ( something
); //boolean
244 loadItem : function ( /* object */ keywordArgs
){
246 // See dojo.data.api.Read.loadItem()
247 this . _assertIsItem ( keywordArgs
. item
);
250 getFeatures : function (){
252 // See dojo.data.api.Read.getFeatures()
253 return this . _features
; //Object
256 getLabel : function ( /* item */ item
){
258 // See dojo.data.api.Read.getLabel()
259 if ( this . _labelAttr
&& this . isItem ( item
)){
260 return this . getValue ( item
, this . _labelAttr
); //String
262 return undefined ; //undefined
265 getLabelAttributes : function ( /* item */ item
){
267 // See dojo.data.api.Read.getLabelAttributes()
269 return [ this . _labelAttr
]; //array
274 _fetchItems : function ( /* Object */ keywordArgs
,
275 /* Function */ findCallback
,
276 /* Function */ errorCallback
){
278 // See dojo.data.util.simpleFetch.fetch()
280 filter = function ( requestArgs
, arrayOfItems
){
283 if ( requestArgs
. query
){
285 ignoreCase
= requestArgs
. queryOptions
? requestArgs
. queryOptions
. ignoreCase
: false ;
287 //See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the
288 //same value for each item examined. Much more efficient.
290 for ( key
in requestArgs
. query
){
291 value
= requestArgs
. query
[ key
];
292 if ( typeof value
=== "string" ){
293 regexpList
[ key
] = dojo
. data
. util
. filter
. patternToRegExp ( value
, ignoreCase
);
294 } else if ( value
instanceof RegExp
){
295 regexpList
[ key
] = value
;
298 for ( i
= 0 ; i
< arrayOfItems
. length
; ++ i
){
300 var candidateItem
= arrayOfItems
[ i
];
301 if ( candidateItem
=== null ){
304 for ( key
in requestArgs
. query
){
305 value
= requestArgs
. query
[ key
];
306 if (! self
. _containsValue ( candidateItem
, key
, value
, regexpList
[ key
])){
312 items
. push ( candidateItem
);
315 findCallback ( items
, requestArgs
);
317 // We want a copy to pass back in case the parent wishes to sort the array.
318 // We shouldn't allow resort of the internal list, so that multiple callers
319 // can get lists and sort without affecting each other. We also need to
320 // filter out any null values that have been left as a result of deleteItem()
321 // calls in ItemFileWriteStore.
322 for ( i
= 0 ; i
< arrayOfItems
. length
; ++ i
){
323 var item
= arrayOfItems
[ i
];
328 findCallback ( items
, requestArgs
);
332 if ( this . _loadFinished
){
333 filter ( keywordArgs
, this . _getItemsArray ( keywordArgs
. queryOptions
));
335 //Do a check on the JsonFileUrl and crosscheck it.
336 //If it doesn't match the cross-check, it needs to be updated
337 //This allows for either url or _jsonFileUrl to he changed to
338 //reset the store load location. Done this way for backwards
339 //compatibility. People use _jsonFileUrl (even though officially
341 if ( this . _jsonFileUrl
!== this . _ccUrl
){
342 dojo
. deprecated ( "dojo.data.ItemFileReadStore: " ,
343 "To change the url, set the url property of the store," +
344 " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0" );
345 this . _ccUrl
= this . _jsonFileUrl
;
346 this . url
= this . _jsonFileUrl
;
347 } else if ( this . url
!== this . _ccUrl
){
348 this . _jsonFileUrl
= this . url
;
349 this . _ccUrl
= this . url
;
352 //See if there was any forced reset of data.
353 if ( this . data
!= null && this . _jsonData
== null ){
354 this . _jsonData
= this . data
;
358 if ( this . _jsonFileUrl
){
359 //If fetches come in before the loading has finished, but while
360 //a load is in progress, we have to defer the fetching to be
361 //invoked in the callback.
362 if ( this . _loadInProgress
){
363 this . _queuedFetches
. push ({ args
: keywordArgs
, filter
: filter
});
365 this . _loadInProgress
= true ;
367 url
: self
. _jsonFileUrl
,
368 handleAs
: "json-comment-optional" ,
369 preventCache
: this . urlPreventCache
,
372 var getHandler
= dojo
. xhrGet ( getArgs
);
373 getHandler
. addCallback ( function ( data
){
375 self
. _getItemsFromLoadedData ( data
);
376 self
. _loadFinished
= true ;
377 self
. _loadInProgress
= false ;
379 filter ( keywordArgs
, self
. _getItemsArray ( keywordArgs
. queryOptions
));
380 self
. _handleQueuedFetches ();
382 self
. _loadFinished
= true ;
383 self
. _loadInProgress
= false ;
384 errorCallback ( e
, keywordArgs
);
387 getHandler
. addErrback ( function ( error
){
388 self
. _loadInProgress
= false ;
389 errorCallback ( error
, keywordArgs
);
392 //Wire up the cancel to abort of the request
393 //This call cancel on the deferred if it hasn't been called
394 //yet and then will chain to the simple abort of the
395 //simpleFetch keywordArgs
397 if ( keywordArgs
. abort
){
398 oldAbort
= keywordArgs
. abort
;
400 keywordArgs
. abort = function (){
402 if ( df
&& df
. fired
=== - 1 ){
407 oldAbort
. call ( keywordArgs
);
411 } else if ( this . _jsonData
){
413 this . _loadFinished
= true ;
414 this . _getItemsFromLoadedData ( this . _jsonData
);
415 this . _jsonData
= null ;
416 filter ( keywordArgs
, this . _getItemsArray ( keywordArgs
. queryOptions
));
418 errorCallback ( e
, keywordArgs
);
421 errorCallback ( new Error ( "dojo.data.ItemFileReadStore: No JSON source data was provided as either URL or a nested Javascript object." ), keywordArgs
);
426 _handleQueuedFetches : function (){
428 // Internal function to execute delayed request in the store.
429 //Execute any deferred fetches now.
430 if ( this . _queuedFetches
. length
> 0 ){
431 for ( var i
= 0 ; i
< this . _queuedFetches
. length
; i
++){
432 var fData
= this . _queuedFetches
[ i
],
433 delayedQuery
= fData
. args
,
434 delayedFilter
= fData
. filter
;
436 delayedFilter ( delayedQuery
, this . _getItemsArray ( delayedQuery
. queryOptions
));
438 this . fetchItemByIdentity ( delayedQuery
);
441 this . _queuedFetches
= [];
445 _getItemsArray : function ( /*object?*/ queryOptions
){
447 // Internal function to determine which list of items to search over.
448 // queryOptions: The query options parameter, if any.
449 if ( queryOptions
&& queryOptions
. deep
){
450 return this . _arrayOfAllItems
;
452 return this . _arrayOfTopLevelItems
;
455 close : function ( /*dojo.data.api.Request || keywordArgs || null */ request
){
457 // See dojo.data.api.Read.close()
458 if ( this . clearOnClose
&&
459 this . _loadFinished
&&
460 ! this . _loadInProgress
){
461 //Reset all internalsback to default state. This will force a reload
462 //on next fetch. This also checks that the data or url param was set
463 //so that the store knows it can get data. Without one of those being set,
464 //the next fetch will trigger an error.
466 if ((( this . _jsonFileUrl
== "" || this . _jsonFileUrl
== null ) &&
467 ( this . url
== "" || this . url
== null )
468 ) && this . data
== null ){
469 console
. debug ( "dojo.data.ItemFileReadStore: WARNING! Data reload " +
470 " information has not been provided." +
471 " Please set 'url' or 'data' to the appropriate value before" +
474 this . _arrayOfAllItems
= [];
475 this . _arrayOfTopLevelItems
= [];
476 this . _loadFinished
= false ;
477 this . _itemsByIdentity
= null ;
478 this . _loadInProgress
= false ;
479 this . _queuedFetches
= [];
483 _getItemsFromLoadedData : function ( /* Object */ dataObject
){
485 // Function to parse the loaded data into item format and build the internal items array.
487 // Function to parse the loaded data into item format and build the internal items array.
490 // The JS data object containing the raw data to convery into item format.
493 // Array of items in store item format.
495 // First, we define a couple little utility functions...
496 var addingArrays
= false ,
499 function valueIsAnItem ( /* anything */ aValue
){
501 // Given any sort of value that could be in the raw json data,
502 // return true if we should interpret the value as being an
503 // item itself, rather than a literal value or a reference.
505 // | false == valueIsAnItem("Kermit");
506 // | false == valueIsAnItem(42);
507 // | false == valueIsAnItem(new Date());
508 // | false == valueIsAnItem({_type:'Date', _value:'May 14, 1802'});
509 // | false == valueIsAnItem({_reference:'Kermit'});
510 // | true == valueIsAnItem({name:'Kermit', color:'green'});
511 // | true == valueIsAnItem({iggy:'pop'});
512 // | true == valueIsAnItem({foo:42});
515 ( typeof aValue
=== "object" ) &&
516 (! dojo
. isArray ( aValue
) || addingArrays
) &&
517 (! dojo
. isFunction ( aValue
)) &&
518 ( aValue
. constructor == Object
|| dojo
. isArray ( aValue
)) &&
519 ( typeof aValue
. _reference
=== "undefined" ) &&
520 ( typeof aValue
. _type
=== "undefined" ) &&
521 ( typeof aValue
. _value
=== "undefined" ) &&
527 function addItemAndSubItemsToArrayOfAllItems ( /* Item */ anItem
){
528 self
. _arrayOfAllItems
. push ( anItem
);
529 for ( var attribute
in anItem
){
530 var valueForAttribute
= anItem
[ attribute
];
531 if ( valueForAttribute
){
532 if ( dojo
. isArray ( valueForAttribute
)){
533 var valueArray
= valueForAttribute
;
534 for ( var k
= 0 ; k
< valueArray
. length
; ++ k
){
535 var singleValue
= valueArray
[ k
];
536 if ( valueIsAnItem ( singleValue
)){
537 addItemAndSubItemsToArrayOfAllItems ( singleValue
);
541 if ( valueIsAnItem ( valueForAttribute
)){
542 addItemAndSubItemsToArrayOfAllItems ( valueForAttribute
);
549 this . _labelAttr
= dataObject
. label
;
551 // We need to do some transformations to convert the data structure
552 // that we read from the file into a format that will be convenient
553 // to work with in memory.
555 // Step 1: Walk through the object hierarchy and build a list of all items
558 this . _arrayOfAllItems
= [];
559 this . _arrayOfTopLevelItems
= dataObject
. items
;
561 for ( i
= 0 ; i
< this . _arrayOfTopLevelItems
. length
; ++ i
){
562 item
= this . _arrayOfTopLevelItems
[ i
];
563 if ( dojo
. isArray ( item
)){
566 addItemAndSubItemsToArrayOfAllItems ( item
);
567 item
[ this . _rootItemPropName
]= true ;
570 // Step 2: Walk through all the attribute values of all the items,
571 // and replace single values with arrays. For example, we change this:
572 // { name:'Miss Piggy', pets:'Foo-Foo'}
574 // { name:['Miss Piggy'], pets:['Foo-Foo']}
576 // We also store the attribute names so we can validate our store
577 // reference and item id special properties for the O(1) isItem
578 var allAttributeNames
= {},
581 for ( i
= 0 ; i
< this . _arrayOfAllItems
. length
; ++ i
){
582 item
= this . _arrayOfAllItems
[ i
];
584 if ( key
!== this . _rootItemPropName
){
585 var value
= item
[ key
];
587 if (! dojo
. isArray ( value
)){
594 allAttributeNames
[ key
]= key
;
598 // Step 3: Build unique property names to use for the _storeRefPropName and _itemNumPropName
599 // This should go really fast, it will generally never even run the loop.
600 while ( allAttributeNames
[ this . _storeRefPropName
]){
601 this . _storeRefPropName
+= "_" ;
603 while ( allAttributeNames
[ this . _itemNumPropName
]){
604 this . _itemNumPropName
+= "_" ;
606 while ( allAttributeNames
[ this . _reverseRefMap
]){
607 this . _reverseRefMap
+= "_" ;
610 // Step 4: Some data files specify an optional 'identifier', which is
611 // the name of an attribute that holds the identity of each item.
612 // If this data file specified an identifier attribute, then build a
613 // hash table of items keyed by the identity of the items.
616 var identifier
= dataObject
. identifier
;
618 this . _itemsByIdentity
= {};
619 this . _features
[ 'dojo.data.api.Identity' ] = identifier
;
620 for ( i
= 0 ; i
< this . _arrayOfAllItems
. length
; ++ i
){
621 item
= this . _arrayOfAllItems
[ i
];
622 arrayOfValues
= item
[ identifier
];
623 var identity
= arrayOfValues
[ 0 ];
624 if (! this . _itemsByIdentity
[ identity
]){
625 this . _itemsByIdentity
[ identity
] = item
;
627 if ( this . _jsonFileUrl
){
628 throw new Error ( "dojo.data.ItemFileReadStore: The json data as specified by: [" + this . _jsonFileUrl
+ "] is malformed. Items within the list have identifier: [" + identifier
+ "]. Value collided: [" + identity
+ "]" );
629 } else if ( this . _jsonData
){
630 throw new Error ( "dojo.data.ItemFileReadStore: The json data provided by the creation arguments is malformed. Items within the list have identifier: [" + identifier
+ "]. Value collided: [" + identity
+ "]" );
635 this . _features
[ 'dojo.data.api.Identity' ] = Number
;
638 // Step 5: Walk through all the items, and set each item's properties
639 // for _storeRefPropName and _itemNumPropName, so that store.isItem() will return true.
640 for ( i
= 0 ; i
< this . _arrayOfAllItems
. length
; ++ i
){
641 item
= this . _arrayOfAllItems
[ i
];
642 item
[ this . _storeRefPropName
] = this ;
643 item
[ this . _itemNumPropName
] = i
;
646 // Step 6: We walk through all the attribute values of all the items,
647 // looking for type/value literals and item-references.
649 // We replace item-references with pointers to items. For example, we change:
650 // { name:['Kermit'], friends:[{_reference:{name:'Miss Piggy'}}] }
652 // { name:['Kermit'], friends:[miss_piggy] }
653 // (where miss_piggy is the object representing the 'Miss Piggy' item).
655 // We replace type/value pairs with typed-literals. For example, we change:
656 // { name:['Nelson Mandela'], born:[{_type:'Date', _value:'July 18, 1918'}] }
658 // { name:['Kermit'], born:(new Date('July 18, 1918')) }
660 // We also generate the associate map for all items for the O(1) isItem function.
661 for ( i
= 0 ; i
< this . _arrayOfAllItems
. length
; ++ i
){
662 item
= this . _arrayOfAllItems
[ i
]; // example: { name:['Kermit'], friends:[{_reference:{name:'Miss Piggy'}}] }
664 arrayOfValues
= item
[ key
]; // example: [{_reference:{name:'Miss Piggy'}}]
665 for ( var j
= 0 ; j
< arrayOfValues
. length
; ++ j
){
666 value
= arrayOfValues
[ j
]; // example: {_reference:{name:'Miss Piggy'}}
667 if ( value
!== null && typeof value
== "object" ){
668 if (( "_type" in value
) && ( "_value" in value
)){
669 var type
= value
. _type
; // examples: 'Date', 'Color', or 'ComplexNumber'
670 var mappingObj
= this . _datatypeMap
[ type
]; // examples: Date, dojo.Color, foo.math.ComplexNumber, {type: dojo.Color, deserialize(value){ return new dojo.Color(value)}}
672 throw new Error ( "dojo.data.ItemFileReadStore: in the typeMap constructor arg, no object class was specified for the datatype '" + type
+ "'" );
673 } else if ( dojo
. isFunction ( mappingObj
)){
674 arrayOfValues
[ j
] = new mappingObj ( value
. _value
);
675 } else if ( dojo
. isFunction ( mappingObj
. deserialize
)){
676 arrayOfValues
[ j
] = mappingObj
. deserialize ( value
. _value
);
678 throw new Error ( "dojo.data.ItemFileReadStore: Value provided in typeMap was neither a constructor, nor a an object with a deserialize function" );
681 if ( value
. _reference
){
682 var referenceDescription
= value
. _reference
; // example: {name:'Miss Piggy'}
683 if (! dojo
. isObject ( referenceDescription
)){
684 // example: 'Miss Piggy'
685 // from an item like: { name:['Kermit'], friends:[{_reference:'Miss Piggy'}]}
686 arrayOfValues
[ j
] = this . _getItemByIdentity ( referenceDescription
);
688 // example: {name:'Miss Piggy'}
689 // from an item like: { name:['Kermit'], friends:[{_reference:{name:'Miss Piggy'}}] }
690 for ( var k
= 0 ; k
< this . _arrayOfAllItems
. length
; ++ k
){
691 var candidateItem
= this . _arrayOfAllItems
[ k
],
693 for ( var refKey
in referenceDescription
){
694 if ( candidateItem
[ refKey
] != referenceDescription
[ refKey
]){
699 arrayOfValues
[ j
] = candidateItem
;
703 if ( this . referenceIntegrity
){
704 var refItem
= arrayOfValues
[ j
];
705 if ( this . isItem ( refItem
)){
706 this . _addReferenceToMap ( refItem
, item
, key
);
709 } else if ( this . isItem ( value
)){
710 //It's a child item (not one referenced through _reference).
711 //We need to treat this as a referenced item, so it can be cleaned up
712 //in a write store easily.
713 if ( this . referenceIntegrity
){
714 this . _addReferenceToMap ( value
, item
, key
);
723 _addReferenceToMap : function ( /*item*/ refItem
, /*item*/ parentItem
, /*string*/ attribute
){
725 // Method to add an reference map entry for an item and attribute.
727 // Method to add an reference map entry for an item and attribute. //
729 // The item that is referenced.
731 // The item that holds the new reference to refItem.
733 // The attribute on parentItem that contains the new reference.
735 //Stub function, does nothing. Real processing is in ItemFileWriteStore.
738 getIdentity : function ( /* item */ item
){
740 // See dojo.data.api.Identity.getIdentity()
741 var identifier
= this . _features
[ 'dojo.data.api.Identity' ];
742 if ( identifier
=== Number
){
743 return item
[ this . _itemNumPropName
]; // Number
745 var arrayOfValues
= item
[ identifier
];
747 return arrayOfValues
[ 0 ]; // Object || String
753 fetchItemByIdentity : function ( /* Object */ keywordArgs
){
755 // See dojo.data.api.Identity.fetchItemByIdentity()
757 // Hasn't loaded yet, we have to trigger the load.
760 if (! this . _loadFinished
){
762 //Do a check on the JsonFileUrl and crosscheck it.
763 //If it doesn't match the cross-check, it needs to be updated
764 //This allows for either url or _jsonFileUrl to he changed to
765 //reset the store load location. Done this way for backwards
766 //compatibility. People use _jsonFileUrl (even though officially
768 if ( this . _jsonFileUrl
!== this . _ccUrl
){
769 dojo
. deprecated ( "dojo.data.ItemFileReadStore: " ,
770 "To change the url, set the url property of the store," +
771 " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0" );
772 this . _ccUrl
= this . _jsonFileUrl
;
773 this . url
= this . _jsonFileUrl
;
774 } else if ( this . url
!== this . _ccUrl
){
775 this . _jsonFileUrl
= this . url
;
776 this . _ccUrl
= this . url
;
779 //See if there was any forced reset of data.
780 if ( this . data
!= null && this . _jsonData
== null ){
781 this . _jsonData
= this . data
;
785 if ( this . _jsonFileUrl
){
787 if ( this . _loadInProgress
){
788 this . _queuedFetches
. push ({ args
: keywordArgs
});
790 this . _loadInProgress
= true ;
792 url
: self
. _jsonFileUrl
,
793 handleAs
: "json-comment-optional" ,
794 preventCache
: this . urlPreventCache
,
797 var getHandler
= dojo
. xhrGet ( getArgs
);
798 getHandler
. addCallback ( function ( data
){
799 var scope
= keywordArgs
. scope
? keywordArgs
. scope
: dojo
. global
;
801 self
. _getItemsFromLoadedData ( data
);
802 self
. _loadFinished
= true ;
803 self
. _loadInProgress
= false ;
804 item
= self
. _getItemByIdentity ( keywordArgs
. identity
);
805 if ( keywordArgs
. onItem
){
806 keywordArgs
. onItem
. call ( scope
, item
);
808 self
. _handleQueuedFetches ();
810 self
. _loadInProgress
= false ;
811 if ( keywordArgs
. onError
){
812 keywordArgs
. onError
. call ( scope
, error
);
816 getHandler
. addErrback ( function ( error
){
817 self
. _loadInProgress
= false ;
818 if ( keywordArgs
. onError
){
819 var scope
= keywordArgs
. scope
? keywordArgs
. scope
: dojo
. global
;
820 keywordArgs
. onError
. call ( scope
, error
);
825 } else if ( this . _jsonData
){
826 // Passed in data, no need to xhr.
827 self
. _getItemsFromLoadedData ( self
. _jsonData
);
828 self
. _jsonData
= null ;
829 self
. _loadFinished
= true ;
830 item
= self
. _getItemByIdentity ( keywordArgs
. identity
);
831 if ( keywordArgs
. onItem
){
832 scope
= keywordArgs
. scope
? keywordArgs
. scope
: dojo
. global
;
833 keywordArgs
. onItem
. call ( scope
, item
);
837 // Already loaded. We can just look it up and call back.
838 item
= this . _getItemByIdentity ( keywordArgs
. identity
);
839 if ( keywordArgs
. onItem
){
840 scope
= keywordArgs
. scope
? keywordArgs
. scope
: dojo
. global
;
841 keywordArgs
. onItem
. call ( scope
, item
);
846 _getItemByIdentity : function ( /* Object */ identity
){
848 // Internal function to look an item up by its identity map.
850 if ( this . _itemsByIdentity
){
851 item
= this . _itemsByIdentity
[ identity
];
853 item
= this . _arrayOfAllItems
[ identity
];
855 if ( item
=== undefined ){
858 return item
; // Object
861 getIdentityAttributes : function ( /* item */ item
){
863 // See dojo.data.api.Identity.getIdentifierAttributes()
865 var identifier
= this . _features
[ 'dojo.data.api.Identity' ];
866 if ( identifier
=== Number
){
867 // If (identifier === Number) it means getIdentity() just returns
868 // an integer item-number for each item. The dojo.data.api.Identity
869 // spec says we need to return null if the identity is not composed
873 return [ identifier
]; // Array
877 _forceLoad : function (){
879 // Internal function to force a load of the store if it hasn't occurred yet. This is required
880 // for specific functions to work properly.
882 //Do a check on the JsonFileUrl and crosscheck it.
883 //If it doesn't match the cross-check, it needs to be updated
884 //This allows for either url or _jsonFileUrl to he changed to
885 //reset the store load location. Done this way for backwards
886 //compatibility. People use _jsonFileUrl (even though officially
888 if ( this . _jsonFileUrl
!== this . _ccUrl
){
889 dojo
. deprecated ( "dojo.data.ItemFileReadStore: " ,
890 "To change the url, set the url property of the store," +
891 " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0" );
892 this . _ccUrl
= this . _jsonFileUrl
;
893 this . url
= this . _jsonFileUrl
;
894 } else if ( this . url
!== this . _ccUrl
){
895 this . _jsonFileUrl
= this . url
;
896 this . _ccUrl
= this . url
;
899 //See if there was any forced reset of data.
900 if ( this . data
!= null && this . _jsonData
== null ){
901 this . _jsonData
= this . data
;
905 if ( this . _jsonFileUrl
){
907 url
: this . _jsonFileUrl
,
908 handleAs
: "json-comment-optional" ,
909 preventCache
: this . urlPreventCache
,
913 var getHandler
= dojo
. xhrGet ( getArgs
);
914 getHandler
. addCallback ( function ( data
){
916 //Check to be sure there wasn't another load going on concurrently
917 //So we don't clobber data that comes in on it. If there is a load going on
918 //then do not save this data. It will potentially clobber current data.
919 //We mainly wanted to sync/wait here.
920 //TODO: Revisit the loading scheme of this store to improve multi-initial
922 if ( self
. _loadInProgress
!== true && ! self
. _loadFinished
){
923 self
. _getItemsFromLoadedData ( data
);
924 self
. _loadFinished
= true ;
925 } else if ( self
. _loadInProgress
){
926 //Okay, we hit an error state we can't recover from. A forced load occurred
927 //while an async load was occurring. Since we cannot block at this point, the best
928 //that can be managed is to throw an error.
929 throw new Error ( "dojo.data.ItemFileReadStore: Unable to perform a synchronous load, an async load is in progress." );
936 getHandler
. addErrback ( function ( error
){
939 } else if ( this . _jsonData
){
940 self
. _getItemsFromLoadedData ( self
. _jsonData
);
941 self
. _jsonData
= null ;
942 self
. _loadFinished
= true ;
946 //Mix in the simple fetch implementation to this class.
947 dojo
. extend ( dojo
. data
. ItemFileReadStore
, dojo
. data
. util
. simpleFetch
);