|
10 | 10 | <script src="../../../ui/jquery.ui.widget.js"></script> |
11 | 11 | <script> |
12 | 12 | // 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 | + |
13 | 29 | $.widget( "ui.datastore", { |
14 | 30 | options: { |
15 | 31 | source: null |
16 | 32 | }, |
17 | 33 | _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 | + }); |
19 | 43 | }, |
20 | 44 | item: function( index ) { |
21 | 45 | return this._getItems( index, 1 )[ 0 ]; |
|
28 | 52 | }, |
29 | 53 | select: function( index ) { |
30 | 54 | var item; |
31 | | - if (typeof index == "number" ) { |
| 55 | + if ( typeof index === "number" ) { |
32 | 56 | item = this.item( index ) |
33 | 57 | } else { |
34 | 58 | item = index; |
35 | 59 | } |
36 | | - this._trigger( "select", null, item ); |
| 60 | + this._trigger( "select", null, { item: item } ); |
37 | 61 | } |
38 | 62 | }); |
39 | 63 |
|
|
65 | 89 | var details = $("#developer"); |
66 | 90 | var datastore = new $.ui.datastore({ |
67 | 91 | 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() ); |
70 | 94 | } |
71 | 95 | }, {} ); |
72 | | - datastore.select(0); |
| 96 | + datastore.select( 0 ); |
73 | 97 |
|
74 | 98 | var summaryTable = $( "#developers" ) |
75 | 99 | .delegate( "tbody > tr", "click", function() { |
76 | | - datastore.select( $(this).tmplItem().data ); |
| 100 | + datastore.select( $(this).tmplItem().data.__ref__ ); |
77 | 101 | }); |
78 | 102 |
|
79 | 103 | var tbody = summaryTable.find( "tbody" ); |
80 | 104 | $.each( datastore.items( 0, 5 ), function( itemId, item ) { |
81 | | - $( "#row-tmpl" ).tmpl( item ).appendTo( tbody ); |
| 105 | + $( "#row-tmpl" ).tmpl( item.props ).appendTo( tbody ); |
82 | 106 | }); |
83 | | - |
84 | 107 | }); |
85 | 108 | </script> |
86 | 109 | </head> |
|
0 commit comments