|
16 | 16 | create: function( type, props ) {
|
17 | 17 | return $.ui.datasource.types[ type ].create( props );
|
18 | 18 | },
|
19 |
| - get: function( type, query ) { |
20 |
| - if ( !query ) { |
21 |
| - if ( !this.items[ type ].length ) { |
22 |
| - this.items[ type ] = $.ui.datasource.types[ type ].get( query ); |
23 |
| - } |
24 |
| - return this.items[ type ]; |
| 19 | + _normalize: function( type, id ) { |
| 20 | + var item = this.items[ type ][ id ], |
| 21 | + ctor = $.ui.dataitem.types[ type ]; |
| 22 | + if ( !( item instanceof $.ui.dataitem ) ) { |
| 23 | + item = this.items[ type ][ id ] = ctor({ data: item }); |
25 | 24 | }
|
26 |
| - return $.map( this.items[ type ], function( item ) { |
27 |
| - return item.options.data.guid === query.guid ? item : undefined; |
| 25 | + return item; |
| 26 | + }, |
| 27 | + get: function( type, id ) { |
| 28 | + if ( id ) { |
| 29 | + return this._normalize( type, id ); |
| 30 | + } |
| 31 | + |
| 32 | + for ( id in this.items[ type ] ) { |
| 33 | + this._normalize( type, id ); |
| 34 | + } |
| 35 | + return $.ui.dataitems({ items: this.items[ type ] }); |
| 36 | + }, |
| 37 | + populate: function( type ) { |
| 38 | + $.ui.datasource.types[ type ].get( this ); |
| 39 | + }, |
| 40 | + _populate: function( type, items ) { |
| 41 | + var local = this.items[ type ]; |
| 42 | + $.each( items, function( i, item ) { |
| 43 | + local[ item.guid ] = item; |
28 | 44 | });
|
29 | 45 | },
|
30 | 46 | save: function() {
|
|
45 | 61 | $.ui.datastore.main.items[ this.options.type ] = {};
|
46 | 62 | },
|
47 | 63 | create: function( props ) {
|
48 |
| - this.options.push( props ); |
| 64 | + this.options.source.push( props ); |
49 | 65 | },
|
50 |
| - get: function( query ) { |
51 |
| - var type = this.options.type; |
52 |
| - return $.map( this.options.source, function( item ) { |
53 |
| - return $.ui.dataitem.types[ type ]({ data: item }); |
54 |
| - }); |
| 66 | + get: function( store ) { |
| 67 | + store._populate( this.options.type, this.options.source ); |
55 | 68 | },
|
56 | 69 | save: function( items ) {
|
57 | 70 | $.each( items, function( itemId, item ) {
|
|
81 | 94 | $.ui.dataitem.types[ type ] = $.ui[ "dataitem-" + type ];
|
82 | 95 | };
|
83 | 96 |
|
| 97 | + $.widget( "ui.dataitems", { |
| 98 | + options: { |
| 99 | + items: null |
| 100 | + } |
| 101 | + }); |
| 102 | + |
84 | 103 | //----------------------------------------
|
85 | 104 | // Application level
|
86 | 105 | //----------------------------------------
|
|
122 | 141 | type: "developer",
|
123 | 142 | source: data
|
124 | 143 | });
|
| 144 | + $.ui.datastore.main.populate( "developer" ); |
125 | 145 |
|
126 | 146 | $(function() {
|
127 | 147 | var details = $("#developer");
|
|
131 | 151 | });
|
132 | 152 |
|
133 | 153 | var tbody = summaryTable.find( "tbody" ),
|
134 |
| - developers = $.ui.datastore.main.get( "developer" ); |
| 154 | + developers = $.ui.datastore.main.get( "developer" ).options.items; |
135 | 155 | $.each( developers, function( itemId, item ) {
|
136 | 156 | $( "#row-tmpl" ).tmpl( item.options.data ).appendTo( tbody );
|
137 | 157 | });
|
138 | 158 |
|
139 |
| - showDetails( developers[ 0 ].options.data.guid ); |
| 159 | + showDetails( developers[ 1 ].options.data.guid ); |
140 | 160 | function showDetails( guid ) {
|
141 | 161 | $( "#developer-tmpl" )
|
142 |
| - .tmpl( $.ui.datastore.main.get( "developer", { guid: guid } )[ 0 ].options.data ) |
| 162 | + .tmpl( $.ui.datastore.main.get( "developer", guid ).options.data ) |
143 | 163 | .appendTo( details.empty() );
|
144 | 164 | }
|
145 | 165 | });
|
|
0 commit comments