source: Dev/branches/rest-dojo-ui/client/dojox/collections/_base.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.8 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array"],
2  function(dojo, lang, arr){
3        var collections = lang.getObject("dojox.collections", true);
4
5/*=====
6        collections = dojox.collections;
7=====*/
8
9        collections.DictionaryEntry=function(/* string */k, /* object */v){
10                //      summary
11                //      return an object of type dojox.collections.DictionaryEntry
12                this.key=k;
13                this.value=v;
14                this.valueOf=function(){
15                        return this.value;      //      object
16                };
17                this.toString=function(){
18                        return String(this.value);      //      string
19                };
20        }
21
22        /*      Iterators
23         *      The collections.Iterators (Iterator and DictionaryIterator) are built to
24         *      work with the Collections included in this module.  However, they *can*
25         *      be used with arrays and objects, respectively, should one choose to do so.
26         */
27        collections.Iterator=function(/* array */a){
28                //      summary
29                //      return an object of type dojox.collections.Iterator
30                var position=0;
31                this.element=a[position]||null;
32                this.atEnd=function(){
33                        //      summary
34                        //      Test to see if the internal cursor has reached the end of the internal collection.
35                        return (position>=a.length);    //      bool
36                };
37                this.get=function(){
38                        //      summary
39                        //      Get the next member in the collection.
40                        if(this.atEnd()){
41                                return null;            //      object
42                        }
43                        this.element=a[position++];
44                        return this.element;    //      object
45                };
46                this.map=function(/* function */fn, /* object? */scope){
47                        //      summary
48                        //      Functional iteration with optional scope.
49                        return arr.map(a, fn, scope);
50                };
51                this.reset=function(){
52                        //      summary
53                        //      reset the internal cursor.
54                        position=0;
55                        this.element=a[position];
56                };
57        }
58
59        /*      Notes:
60         *      The DictionaryIterator no longer supports a key and value property;
61         *      the reality is that you can use this to iterate over a JS object
62         *      being used as a hashtable.
63         */
64        collections.DictionaryIterator=function(/* object */obj){
65                //      summary
66                //      return an object of type dojox.collections.DictionaryIterator
67                var a=[];       //      Create an indexing array
68                var testObject={};
69                for(var p in obj){
70                        if(!testObject[p]){
71                                a.push(obj[p]); //      fill it up
72                        }
73                }
74                var position=0;
75                this.element=a[position]||null;
76                this.atEnd=function(){
77                        //      summary
78                        //      Test to see if the internal cursor has reached the end of the internal collection.
79                        return (position>=a.length);    //      bool
80                };
81                this.get=function(){
82                        //      summary
83                        //      Get the next member in the collection.
84                        if(this.atEnd()){
85                                return null;            //      object
86                        }
87                        this.element=a[position++];
88                        return this.element;    //      object
89                };
90                this.map=function(/* function */fn, /* object? */scope){
91                        //      summary
92                        //      Functional iteration with optional scope.
93                        return arr.map(a, fn, scope);
94                };
95                this.reset=function() {
96                        //      summary
97                        //      reset the internal cursor.
98                        position=0;
99                        this.element=a[position];
100                };
101        };
102
103        return collections;
104});
Note: See TracBrowser for help on using the repository browser.