Skip to content

Commit 21935f3

Browse files
committed
Data model: Basic implementation of lazy initialization for dataitem instances.
1 parent 4c90f2f commit 21935f3

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

tests/visual/grid/data.html

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,31 @@
1616
create: function( type, props ) {
1717
return $.ui.datasource.types[ type ].create( props );
1818
},
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 });
2524
}
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;
2844
});
2945
},
3046
save: function() {
@@ -45,13 +61,10 @@
4561
$.ui.datastore.main.items[ this.options.type ] = {};
4662
},
4763
create: function( props ) {
48-
this.options.push( props );
64+
this.options.source.push( props );
4965
},
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 );
5568
},
5669
save: function( items ) {
5770
$.each( items, function( itemId, item ) {
@@ -81,6 +94,12 @@
8194
$.ui.dataitem.types[ type ] = $.ui[ "dataitem-" + type ];
8295
};
8396

97+
$.widget( "ui.dataitems", {
98+
options: {
99+
items: null
100+
}
101+
});
102+
84103
//----------------------------------------
85104
// Application level
86105
//----------------------------------------
@@ -122,6 +141,7 @@
122141
type: "developer",
123142
source: data
124143
});
144+
$.ui.datastore.main.populate( "developer" );
125145

126146
$(function() {
127147
var details = $("#developer");
@@ -131,15 +151,15 @@
131151
});
132152

133153
var tbody = summaryTable.find( "tbody" ),
134-
developers = $.ui.datastore.main.get( "developer" );
154+
developers = $.ui.datastore.main.get( "developer" ).options.items;
135155
$.each( developers, function( itemId, item ) {
136156
$( "#row-tmpl" ).tmpl( item.options.data ).appendTo( tbody );
137157
});
138158

139-
showDetails( developers[ 0 ].options.data.guid );
159+
showDetails( developers[ 1 ].options.data.guid );
140160
function showDetails( guid ) {
141161
$( "#developer-tmpl" )
142-
.tmpl( $.ui.datastore.main.get( "developer", { guid: guid } )[ 0 ].options.data )
162+
.tmpl( $.ui.datastore.main.get( "developer", guid ).options.data )
143163
.appendTo( details.empty() );
144164
}
145165
});

0 commit comments

Comments
 (0)