Skip to content

Commit d6b7edf

Browse files
committed
Moved populate call for local data into datasource. Added some more
TODOs. Extended data extraction to include field metadata and store that on the resulting dataitem prototype. Use that for sortablegrid impl.
1 parent 1b9e3d2 commit d6b7edf

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

tests/visual/grid/data.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
type: "developer",
5757
source: data
5858
});
59-
// TODO when will be a need for a non-main datastore?
60-
$.ui.datastore.main.populate( "developer" );
6159

6260
$(function() {
6361
$( "#developers" ).grid({

tests/visual/grid/datasort.html

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,16 @@
3535
});
3636
},
3737
_sort: function( header ) {
38-
var type = header.data( "type" ),
39-
culture = header.data( "culture" ),
40-
format = header.data( "format" ),
41-
order = header.data("sort-order") || 1;
38+
var fieldname = this.options.columns[ header[0].cellIndex ];
39+
// TODO access shouldn't happen through the prototype
40+
var field = $.ui.dataitem.types[ this.options.type ].prototype.fields[ fieldname ];
4241

43-
// TODO map this.cellIndex to field name instead
44-
var field = header.text().toLowerCase().replace( /\s|[^a-z0-9]/g, "_" );
45-
4642
function extract( row ) {
47-
var text = row.options.data[ field ];
48-
if ( type == "currency" ) {
49-
return $.global.parseFloat( text, culture );
50-
} else if (type == "date") {
51-
return $.global.parseDate( text, format, culture );
43+
var text = row.options.data[ fieldname ];
44+
if ( field.type == "currency" ) {
45+
return $.global.parseFloat( text, field.culture );
46+
} else if (field.type == "date") {
47+
return $.global.parseDate( text, field.format, field.culture );
5248
} else {
5349
return text;
5450
}
@@ -61,10 +57,9 @@
6157
// a better implementation would seperate data extraction from sorting
6258
// that isn't possible here, as methods like .map() produce a new array
6359
items.sort(function( a, b ) {
64-
return ( extract( a ) < extract( b ) ? -1 : 1 ) * order;
60+
return ( extract( a ) < extract( b ) ? -1 : 1 ) * field.sortOrder;
6561
});
66-
order *= -1;
67-
header.data( "sort-order", order );
62+
field.sortOrder *= -1;
6863
this.refresh();
6964
}
7065
});

tests/visual/grid/datasource.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
_create: function() {
1515
$.ui.datasource.types[ this.options.type ] = this;
1616
$.ui.datastore.main.items[ this.options.type ] = [];
17+
// populate store with static data directly
18+
if ($.isArray(this.options.source)) {
19+
// TODO pass this (as the datasource instance) instead of type?
20+
$.ui.datastore.main.populate( this.options.type );
21+
}
1722
},
1823
create: function( props ) {
1924
this.options.source.push( props );

tests/visual/grid/datastore.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
(function( $ ) {
1010

1111
$.widget( "ui.datastore", {
12-
// TODO hackity hack to get the same dataitems instance back for calls to datastore.get(type)
13-
dataitems: {},
1412
_create: function() {
13+
// TODO hackity hack to get the same dataitems instance back for calls to datastore.get(type)
14+
this.dataitems = {};
1515
this.items = {};
1616
},
1717
create: function( type, props ) {
@@ -44,10 +44,12 @@
4444
}
4545
return this.dataitems[type] = $.ui.dataitems({ items: this.items[ type ] });
4646
},
47+
// TODO rename this
4748
populate: function( type ) {
48-
// TODO datasource.get is calling datastore._populate - inline that?
49+
// TODO or rename datasource.get
4950
$.ui.datasource.types[ type ].get( this );
5051
},
52+
// TODO or rename this
5153
_populate: function( type, items ) {
5254
// TODO just assign items to this.items[type]?
5355
var local = this.items[ type ];

tests/visual/grid/grid.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,22 @@
4040
var type = "generated" + $.now();
4141
this.options.type = type;
4242

43-
var fields = this.element.find( "th" ).map(function() {
44-
var field = $( this ).data( "field" );
43+
var fieldDescriptions = {};
44+
var fields = this.options.columns = this.element.find( "th" ).map(function() {
45+
var th = $( this ),
46+
field = $( this ).data( "field" );
4547
if ( !field ) {
4648
// generate field name if missing
4749
field = $( this ).text().toLowerCase().replace(/\s|[^a-z0-9]/g, "_");
4850
}
51+
52+
fieldDescriptions[field] = {
53+
type: th.data( "type" ),
54+
culture: th.data( "culture" ),
55+
format: th.data( "format" ),
56+
sortOrder: th.data( "sort-order" ) || 1
57+
};
58+
4959
return field;
5060
}).get();
5161

@@ -62,18 +72,20 @@
6272
return item;
6373
}).get();
6474

75+
// TODO seperate template generation from data extraction
6576
var template = $.map( fields, function( field ) {
6677
return "<td>${" + field + "}</td>";
6778
}).join( "" );
6879
template = "<tr>" + template + "</tr>";
6980
this.options.rowTemplate = template;
7081

82+
$.ui.dataitem.extend( type, {
83+
fields: fieldDescriptions
84+
} );
7185
$.ui.datasource({
7286
type: type,
7387
source: items
7488
});
75-
$.ui.dataitem.extend( type, {} );
76-
$.ui.datastore.main.populate( type );
7789
}
7890
});
7991

0 commit comments

Comments
 (0)