Changeset 9351

Show
Ignore:
Timestamp:
06/28/07 07:21:35 (19 months ago)
Author:
jaredj
Message:

API update for dojo.data.api.Identity. refs #3554

Location:
dojo/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/data/api/Identity.js

    r9021 r9351  
    2121                //              Returns a unique identifier for an item.  The return value will be 
    2222                //              either a string or something that has a toString() method (such as, 
    23                 //              for example, a dojo.uuid.Uuid object). 
     23                //              for example, a dojox.uuid.Uuid object). 
    2424                //      item: 
    2525                //              The item from the store from which to obtain its identifier. 
     
    5656        }, 
    5757 
    58         getItemByIdentity: function(/* string || object */ identity){ 
     58 
     59        fetchItemByIdentity: function(/* object */ keywordArgs){ 
    5960                //      summary: 
    6061                //              Given the identity of an item, this method returns the item that has  
    61                 //              that identity.  Conforming implementations should return null if there  
    62                 //              is no item with the given identity.  Implementations of getItemByIdentity()  
    63                 //              may sometimes return an item from a local cache and may sometimes  
    64                 //              fetch an item from a remote server, in which case the call to  
    65                 //              getItemByIdentity() will block until the getItemByIdentity() implementation  
    66                 //              has the item to return.  
     62                //              that identity through the onItem callback.  Conforming implementations  
     63                //              should return null if there is no item with the given identity.   
     64                //              Implementations of fetchItemByIdentity() may sometimes return an item  
     65                //              from a local cache and may sometimes fetch an item from a remote server,  
    6766                // 
    68                 //      identity: 
    69                 //              The identity of the object to locate.  It should be a string or an  
    70                 //              object that toString() can be called on (such as a dojo.uuid object). 
     67                //      keywordArgs: 
     68                //              An anonymous object that defines the item to locate and callbacks to invoke when the  
     69                //              item has been located and load has completed.  The format of the object is as follows: 
     70                //              { 
     71                //                      identity: string|object, 
     72                //                      onItem: Function, 
     73                //                      onError: Function, 
     74                //                      scope: object 
     75                //              } 
     76                //      The *identity* parameter. 
     77                //              The identity parameter is the identity of the item you wish to locate and load 
     78                //              This attribute is required.  It should be a string or an object that toString()  
     79                //              can be called on. 
     80                //               
     81                //      The *onItem* parameter. 
     82                //              Function(item) 
     83                //              The onItem parameter is the callback to invoke when the item has been loaded.  It takes only one 
     84                //              parameter, the item located, or null if none found. 
    7185                // 
    72                 //      examples: 
    73                 //              var alaska = store.getItemByIdentity("AK"); 
    74                 //              assert("AK" == store.getItemByIdentity(store.getItemByIdentity("AK"))); 
    75                 throw new Error('Unimplemented API: dojo.data.api.Identity.getItemByIdentity'); 
    76                 var item = null; 
    77                 return item; // item 
     86                //      The *onError* parameter. 
     87                //              Function(error) 
     88                //              The onError parameter is the callback to invoke when the item load encountered an error.  It takes only one 
     89                //              parameter, the error object 
     90                // 
     91                //      The *scope* parameter. 
     92                //              If a scope object is provided, all of the callback functions (onItem,  
     93                //              onError, etc) will be invoked in the context of the scope object. 
     94                //              In the body of the callback function, the value of the "this" 
     95                //              keyword will be the scope object.   If no scope object is provided, 
     96                //              the callback functions will be called in the context of dojo.global. 
     97                //              For example, onItem.call(scope, item, request) vs.  
     98                //              onItem.call(dojo.global, item, request) 
     99                if (!this.isItemLoaded(keywordArgs.item)) { 
     100                        throw new Error('Unimplemented API: dojo.data.api.Identity.fetchItemByIdentity'); 
     101                } 
    78102        } 
    79103}); 
  • dojo/trunk/data/JsonItemStore.js

    r9320 r9351  
    419419        }, 
    420420 
    421         getItemByIdentity: function(/* String */ identity){ 
    422                 //      summary:  
    423                 //              See dojo.data.api.Identity.getItemByIdentity() 
    424  
    425                 // Force a sync'ed load if it hasn't occurred yet 
     421        fetchItemByIdentity: function(/* Object */ keywordArgs){ 
     422                //      summary:  
     423                //              See dojo.data.api.Identity.fetchItemByIdentity() 
     424 
     425                //Hasn't loaded yet, we have to trigger the load. 
    426426                if(!this._loadFinished){ 
    427                         this._forceLoad(); 
    428                 } 
     427                        var self = this; 
     428                        if(this._jsonFileUrl){ 
     429                                var getArgs = { 
     430                                                url: self._jsonFileUrl,  
     431                                                handleAs: "json-comment-optional" 
     432                                        }; 
     433                                var getHandler = dojo.xhrGet(getArgs); 
     434                                getHandler.addCallback(function(data){ 
     435                                        var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global; 
     436                                        try{ 
     437                                                self._arrayOfAllItems = self._getItemsFromLoadedData(data); 
     438                                                self._loadFinished = true; 
     439                                                var item = self._getItemByIdentity(keywordArgs.identity); 
     440                                                if(keywordArgs.onItem){ 
     441                                                        keywordArgs.onItem.call(scope, item); 
     442                                                } 
     443                                        }catch(error){ 
     444                                                if(keywordArgs.onError){ 
     445                                                        keywordArgs.onError.call(scope, error); 
     446                                                } 
     447                                        } 
     448                                }); 
     449                                getHandler.addErrback(function(error){ 
     450                                        if(keywordArgs.onError){ 
     451                                                var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global; 
     452                                                keywordArgs.onError.call(scope, error); 
     453                                        } 
     454                                }); 
     455                        }else if(this._jsonData){ 
     456                                //Passe din data, no need to xhr. 
     457                                self._arrayOfAllItems = self._getItemsFromLoadedData(self._jsonData); 
     458                                self._jsonData = null; 
     459                                self._loadFinished = true; 
     460                                var item = self._getItemByIdentity(keywordArgs.identity); 
     461                                if(keywordArgs.onItem){ 
     462                                        var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global; 
     463                                        keywordArgs.onItem.call(scope, item); 
     464                                } 
     465                        }  
     466                }else{ 
     467                        //Already loaded.  We can just look it up and call back. 
     468                        var item = this._getItemByIdentity(keywordArgs.identity); 
     469                        if(keywordArgs.onItem){ 
     470                                var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global; 
     471                                keywordArgs.onItem.call(scope, item); 
     472                        } 
     473                } 
     474        }, 
     475 
     476        _getItemByIdentity: function(/* Object */ identity){ 
     477                //      summary: 
     478                //              Internal function to look an item up by its identity map. 
     479                var item = null; 
    429480                if(this._itemsByIdentity){ 
    430                         var item = this._itemsByIdentity[identity]; 
    431                         if(item !== undefined){ 
    432                                 return item; //Object 
    433                         } 
    434                 } 
    435                 return null; //null 
     481                        item = this._itemsByIdentity[identity]; 
     482                        if(item === undefined){ 
     483                                item = null; 
     484                        } 
     485                } 
     486                return item; // Object 
    436487        }, 
    437488 
  • dojo/trunk/tests/data/JsonItemStore.js

    r9049 r9351  
    6363doh.register("tests.data.JsonItemStore",  
    6464        [ 
    65                 function testIdentityAPI_getItemByIdentity(t){ 
    66                         //      summary:  
    67                         //              Simple test of the getItemByIdentity function of the store. 
    68                         //      description: 
    69                         //              Simple test of the getItemByIdentity function of the store. 
    70                         var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
    71  
    72                         var item = jsonItemStore.getItemByIdentity("sv"); 
    73                         t.assertTrue(item !== null); 
    74                         if(item !== null){ 
    75                                 var name = jsonItemStore.getValue(item,"name"); 
    76                                 t.assertEqual(name, "El Salvador"); 
    77                         } 
    78                         item = jsonItemStore.getItemByIdentity("sv_not"); 
    79                         t.assertTrue(item === null); 
    80                 }, 
     65                function testIdentityAPI_fetchItemByIdentity(t){ 
     66                        //      summary:  
     67                        //              Simple test of the fetchItemByIdentity function of the store. 
     68                        //      description: 
     69                        //              Simple test of the fetchItemByIdentity function of the store. 
     70                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
     71 
     72                        var d = new doh.Deferred(); 
     73                        function onItem(item){ 
     74                                t.assertTrue(item !== null); 
     75                                if(item !== null){ 
     76                                        var name = jsonItemStore.getValue(item,"name"); 
     77                                        t.assertEqual(name, "El Salvador"); 
     78                                } 
     79                                d.callback(true); 
     80                        } 
     81                        function onError(errData){ 
     82                                t.assertTrue(false); 
     83                                d.errback(errData); 
     84                        } 
     85                        jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     86                        return d // Deferred 
     87                }, 
     88 
     89                function testIdentityAPI_fetchItemByIdentity_notFound(t){ 
     90                        //      summary:  
     91                        //              Simple test of the fetchItemByIdentity function of the store. 
     92                        //      description: 
     93                        //              Simple test of the fetchItemByIdentity function of the store. 
     94                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
     95 
     96                        var d = new doh.Deferred(); 
     97                        function onItem(item){ 
     98                                t.assertTrue(item === null); 
     99                                d.callback(true); 
     100                        } 
     101                        function onError(errData){ 
     102                                t.assertTrue(false); 
     103                                d.errback(errData); 
     104                        } 
     105                        jsonItemStore.fetchItemByIdentity({identity: "sv_not", onItem: onItem, onError: onError}); 
     106                        return d // Deferred 
     107                }, 
     108 
    81109                function testIdentityAPI_getIdentityAttributes(t){ 
    82110                        //      summary:  
     
    86114                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
    87115 
    88                         var item = jsonItemStore.getItemByIdentity("sv"); 
    89                         t.assertTrue(item !== null); 
    90                         if(item !== null){ 
     116                        var d = new doh.Deferred(); 
     117                        function onItem(item){ 
     118                                t.assertTrue(item !== null) 
    91119                                var identifiers = jsonItemStore.getIdentityAttributes(item); 
    92120                t.assertTrue(dojo.isArray(identifiers)); 
    93121                                t.assertEqual(1, identifiers.length); 
    94122                                t.assertEqual("abbr", identifiers[0]); 
    95                         } 
    96                 }, 
    97                 function testIdentityAPI_getItemByIdentity_commentFilteredJson(t){ 
    98                         //      summary:  
    99                         //              Simple test of the getItemByIdentity function of the store. 
    100                         //      description: 
    101                         //              Simple test of the getItemByIdentity function of the store. 
     123                                d.callback(true); 
     124                        } 
     125                        function onError(errData){ 
     126                                t.assertTrue(false); 
     127                                d.errback(errData); 
     128                        } 
     129                        jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     130                        return d // Deferred 
     131                }, 
     132                function testIdentityAPI_fetchItemByIdentity_commentFilteredJson(t){ 
     133                        //      summary:  
     134                        //              Simple test of the fetchItemByIdentity function of the store. 
     135                        //      description: 
     136                        //              Simple test of the fetchItemByIdentity function of the store. 
    102137                        //              This tests loading a comment-filtered json file so that people using secure 
    103138                        //              data with this store can bypass the JavaSceipt hijack noted in Fortify's 
     
    107142                                var jsonItemStore = new dojo.data.JsonItemStore({url: dojo.moduleUrl("tests", "data/countries_commentFiltered.json").toString()}); 
    108143 
    109                                 var item = jsonItemStore.getItemByIdentity("sv"); 
    110                                 t.assertTrue(item !== null); 
    111                                 if(item !== null){ 
     144                                var d = new doh.Deferred(); 
     145                                function onItem(item){ 
     146                                        t.assertTrue(item !== null); 
    112147                                        var name = jsonItemStore.getValue(item,"name"); 
    113148                                        t.assertEqual(name, "El Salvador"); 
    114                                 } 
    115                         } 
    116                 }, 
    117                 function testIdentityAPI_getItemByIdentity_nullValue(t){ 
    118                         //      summary:  
    119                         //              Simple test of the getItemByIdentity function of the store, checling a null value. 
    120                         //      description: 
    121                         //              Simple test of the getItemByIdentity function of the store, checking a null value. 
     149                    d.callback(true); 
     150                                } 
     151                                function onError(errData){ 
     152                                        t.assertTrue(false); 
     153                                        d.errback(errData); 
     154                                } 
     155                                jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     156                                return d // Deferred 
     157                        } 
     158                }, 
     159                function testIdentityAPI_fetchItemByIdentity_nullValue(t){ 
     160                        //      summary:  
     161                        //              Simple test of the fetchItemByIdentity function of the store, checling a null value. 
     162                        //      description: 
     163                        //              Simple test of the fetchItemByIdentity function of the store, checking a null value. 
    122164                        //              This tests handling attributes in json that were defined as null properly. 
    123165                        //              Introduced because of tracker: #3153 
     
    125167                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStoreWithNull(); 
    126168 
    127                         var item = jsonItemStore.getItemByIdentity("ec"); 
    128                         t.assertTrue(item !== null); 
    129                         if(item !== null){ 
     169                        var d = new doh.Deferred(); 
     170                        function onItem(item){ 
     171                                t.assertTrue(item !== null); 
    130172                                var name = jsonItemStore.getValue(item,"name"); 
    131173                                t.assertEqual(name, null); 
    132                         } 
    133  
    134                         item = jsonItemStore.getItemByIdentity("sv"); 
    135                         t.assertTrue(item !== null); 
    136                         if(item !== null){ 
     174                                d.callback(true); 
     175                        } 
     176                        function onError(errData){ 
     177                                t.assertTrue(false); 
     178                                d.errback(errData); 
     179                        } 
     180                        jsonItemStore.fetchItemByIdentity({identity: "ec", onItem: onItem, onError: onError}); 
     181                        return d // Deferred 
     182                }, 
     183                function testIdentityAPI_fetchItemByIdentity_booleanValue(t){ 
     184                        //      summary:  
     185                        //              Simple test of the fetchItemByIdentity function of the store, checking a boolean value. 
     186                        //      description: 
     187                        //              Simple test of the fetchItemByIdentity function of the store, checking a boolean value. 
     188 
     189                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStoreWithBoolean(); 
     190 
     191                        var d = new doh.Deferred(); 
     192                        function onItem(item){ 
     193                                t.assertTrue(item !== null); 
    137194                                var name = jsonItemStore.getValue(item,"name"); 
    138                                 t.assertEqual(name, "El Salvador"); 
    139                         } 
    140                 }, 
    141                 function testIdentityAPI_getItemByIdentity_booleanValue(t){ 
    142                         //      summary:  
    143                         //              Simple test of the getItemByIdentity function of the store, checking a boolean value. 
    144                         //      description: 
    145                         //              Simple test of the getItemByIdentity function of the store, checking a boolean value. 
    146  
    147                         var jsonItemStore = tests.data.JsonItemStore.getCountriesStoreWithBoolean(); 
    148  
    149                         try{ 
    150                                 var item = jsonItemStore.getItemByIdentity("sv"); 
     195                                t.assertEqual(name, "Utopia"); 
     196                                var real = jsonItemStore.getValue(item,"real"); 
     197                                t.assertEqual(real, false); 
     198                d.callback(true); 
     199                        } 
     200                        function onError(errData){ 
     201                                t.assertTrue(false); 
     202                                d.errback(errData); 
     203                        } 
     204                        jsonItemStore.fetchItemByIdentity({identity: "ut", onItem: onItem, onError: onError}); 
     205                        return d // Deferred 
     206                }, 
     207                function testIdentityAPI_getIdentity(t){ 
     208                        //      summary:  
     209                        //              Simple test of the getIdentity function of the store. 
     210                        //      description: 
     211                        //              Simple test of the getIdentity function of the store. 
     212 
     213                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
     214 
     215                        var d = new doh.Deferred(); 
     216                        function onItem(item){ 
    151217                                t.assertTrue(item !== null); 
    152                                 if(item !== null){ 
    153                                     var name = jsonItemStore.getValue(item,"name"); 
    154                                         t.assertEqual(name, "El Salvador"); 
    155                                         var real = jsonItemStore.getValue(item,"real"); 
    156                                         t.assertEqual(real, true); 
    157                                 } 
    158  
    159                                 item = jsonItemStore.getItemByIdentity("ut"); 
    160                                 t.assertTrue(item !== null); 
    161                                 if(item !== null){ 
    162                                         var name = jsonItemStore.getValue(item,"name"); 
    163                                         t.assertEqual(name, "Utopia"); 
    164                                         var real = jsonItemStore.getValue(item,"real"); 
    165                                         t.assertEqual(real, false); 
    166                                 } 
    167                         }catch(e){ 
    168                                 for(i in e){ 
    169                                         console.log("I is: [" + i + "] with value: [" +e[i] + "]"); 
    170                                 } 
    171                                 throw e; 
    172                         } 
    173                 }, 
    174                 function testIdentityAPI_getIdentity(t){ 
    175                         //      summary:  
    176                         //              Simple test of the getIdentity function of the store. 
    177                         //      description: 
    178                         //              Simple test of the getIdentity function of the store. 
    179  
    180                         var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
    181  
    182                         var item = jsonItemStore.getItemByIdentity("sv"); 
    183                         t.assertTrue(item !== null); 
    184                         t.assertTrue(jsonItemStore.getIdentity(item) === "sv"); 
     218                                t.assertTrue(jsonItemStore.getIdentity(item) === "sv"); 
     219                d.callback(true); 
     220                        } 
     221                        function onError(errData){ 
     222                                t.assertTrue(false); 
     223                                d.errback(errData); 
     224                        } 
     225                        jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     226                        return d // Deferred 
    185227                }, 
    186228                function testReadAPI_fetch_all(t){ 
     
    447489                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
    448490 
    449                         var item = jsonItemStore.getItemByIdentity("sv"); 
    450                         t.assertTrue(item !== null); 
    451                         var name = jsonItemStore.getValue(item,"name"); 
    452                         t.assertTrue(name === "El Salvador"); 
     491                        var d = new doh.Deferred(); 
     492                        function onItem(item){ 
     493                                t.assertTrue(item !== null); 
     494                                var name = jsonItemStore.getValue(item,"name"); 
     495                                t.assertTrue(name === "El Salvador"); 
     496                                d.callback(true); 
     497                        } 
     498                        function onError(errData){ 
     499                                t.assertTrue(false); 
     500                                d.errback(errData); 
     501                        } 
     502                        jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     503                        return d // Deferred 
    453504                }, 
    454505                function testReadAPI_getValues(t){ 
     
    460511                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
    461512 
    462                         var item = jsonItemStore.getItemByIdentity("sv"); 
    463                         t.assertTrue(item !== null); 
    464                         var names = jsonItemStore.getValues(item,"name"); 
    465             t.assertTrue(dojo.isArray(names)); 
    466                         t.assertEqual(names.length, 1); 
    467                         t.assertEqual(names[0], "El Salvador"); 
     513                        var d = new doh.Deferred(); 
     514                        function onItem(item){ 
     515                                t.assertTrue(item !== null); 
     516                                var names = jsonItemStore.getValues(item,"name"); 
     517                                t.assertTrue(dojo.isArray(names)); 
     518                                t.assertEqual(names.length, 1); 
     519                                t.assertEqual(names[0], "El Salvador"); 
     520                                d.callback(true); 
     521                        } 
     522                        function onError(errData){ 
     523                                t.assertTrue(false); 
     524                                d.errback(errData); 
     525                        } 
     526                        jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     527                        return d // Deferred 
    468528                }, 
    469529                function testReadAPI_isItem(t){ 
     
    475535                        var jsonItemStore = tests.data.JsonItemStore.getCountriesStore(); 
    476536 
    477                         var item = jsonItemStore.getItemByIdentity("sv"); 
    478                         t.assertTrue(item !== null); 
    479                         t.assertTrue(jsonItemStore.isItem(item)); 
    480                         t.assertTrue(!jsonItemStore.isItem({})); 
     537                        var d = new doh.Deferred(); 
     538                        function onItem(item){ 
     539                                t.assertTrue(item !== null); 
     540                                t.assertTrue(jsonItemStore.isItem(item)); 
     541                                t.assertTrue(!jsonItemStore.isItem({})); 
     542                                d.callback(true); 
     543                        } 
     544                        function onError(errData){ 
     545                                t.assertTrue(false); 
     546                                d.errback(errData); 
     547                        } 
     548                        jsonItemStore.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); 
     549                        return d // Deferred 
    481550                }, 
    482551                function testReadAPI_isItem_multistore(t){ 
     
    495564                        var jsonItemStore2 = tests.data.JsonItemStore.getCountriesStore(); 
    496565 
    497                         var item1 = jsonItemStore1.getItemByIdentity("sv"); 
    498                         var item2 = jsonItemStore2.getItemByIdentity("sv"); 
    499                         t.assertTrue(item1 !== null); 
    500                         t.assertTrue(item2 !== null); 
    501                         t.assertTrue(jsonItemStore1.isItem(item1)); 
    502                         t.assertTrue(jsonItemStore2.isItem(item2)); 
    503                         t.assertTrue(!jsonItemStore1.isItem(item2)); 
    504