Skip to content

Commit 4642f45

Browse files
committed
Datastore: Added dataitem widget.
1 parent 039c785 commit 4642f45

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

tests/visual/grid/data.html

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,36 @@
1010
<script src="../../../ui/jquery.ui.widget.js"></script>
1111
<script>
1212
// widget factory is nice, but we probably won't have an element
13+
$.widget( "ui.dataitem", {
14+
options: {
15+
source: null
16+
},
17+
_create: function() {
18+
this.props = this.options.source;
19+
},
20+
get: function( key ) {
21+
return this.props[ key ];
22+
},
23+
set: function( key, value ) {
24+
this.props[ key ] = value;
25+
return this;
26+
}
27+
});
28+
1329
$.widget( "ui.datastore", {
1430
options: {
1531
source: null
1632
},
1733
_create: function() {
18-
this._items = this.options.source;
34+
var that = this;
35+
this._items = [];
36+
$.each( this.options.source, function( i, item ) {
37+
var dataitem = new $.ui.dataitem({
38+
source: item
39+
}, {} );
40+
dataitem.set( "__ref__", dataitem );
41+
that._items[ i ] = dataitem;
42+
});
1943
},
2044
item: function( index ) {
2145
return this._getItems( index, 1 )[ 0 ];
@@ -28,12 +52,12 @@
2852
},
2953
select: function( index ) {
3054
var item;
31-
if (typeof index == "number" ) {
55+
if ( typeof index === "number" ) {
3256
item = this.item( index )
3357
} else {
3458
item = index;
3559
}
36-
this._trigger( "select", null, item );
60+
this._trigger( "select", null, { item: item } );
3761
}
3862
});
3963

@@ -65,22 +89,21 @@
6589
var details = $("#developer");
6690
var datastore = new $.ui.datastore({
6791
source: data,
68-
select: function( event, item ) {
69-
$( "#developer-tmpl" ).tmpl( item ).appendTo( details.empty() );
92+
select: function( event, ui ) {
93+
$( "#developer-tmpl" ).tmpl( ui.item.props ).appendTo( details.empty() );
7094
}
7195
}, {} );
72-
datastore.select(0);
96+
datastore.select( 0 );
7397

7498
var summaryTable = $( "#developers" )
7599
.delegate( "tbody > tr", "click", function() {
76-
datastore.select( $(this).tmplItem().data );
100+
datastore.select( $(this).tmplItem().data.__ref__ );
77101
});
78102

79103
var tbody = summaryTable.find( "tbody" );
80104
$.each( datastore.items( 0, 5 ), function( itemId, item ) {
81-
$( "#row-tmpl" ).tmpl( item ).appendTo( tbody );
105+
$( "#row-tmpl" ).tmpl( item.props ).appendTo( tbody );
82106
});
83-
84107
});
85108
</script>
86109
</head>

0 commit comments

Comments
 (0)