|
3 | 3 | <head>
|
4 | 4 | <meta charset="utf-8">
|
5 | 5 | <title>Grid: Data</title>
|
| 6 | + <link rel="stylesheet" href="../visual.css" /> |
6 | 7 | <link rel="stylesheet" href="../../../themes/base/jquery.ui.all.css">
|
| 8 | + <link rel="stylesheet" href="grid.css" /> |
7 | 9 | <script src="../../../jquery-1.4.4.js"></script>
|
8 | 10 | <script src="../../../external/jquery.tmpl.js"></script>
|
9 | 11 | <script src="../../../ui/jquery.ui.core.js"></script>
|
10 | 12 | <script src="../../../ui/jquery.ui.widget.js"></script>
|
| 13 | + <script src="dataitem.js"></script> |
| 14 | + <script src="datasource.js"></script> |
| 15 | + <script src="datastore.js"></script> |
| 16 | + <script src="grid.js"></script> |
11 | 17 | <script>
|
12 |
| - $.widget( "ui.datastore", { |
13 |
| - _create: function() { |
14 |
| - this.items = {}; |
15 |
| - }, |
16 |
| - create: function( type, props ) { |
17 |
| - return $.ui.datasource.types[ type ].create( props ); |
18 |
| - }, |
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 }); |
24 |
| - } |
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; |
44 |
| - }); |
45 |
| - }, |
46 |
| - save: function() { |
47 |
| - $.each( this.items, function( type, items ) { |
48 |
| - $.ui.datasource.types[ type ].save( items ); |
49 |
| - }); |
50 |
| - } |
51 |
| - }); |
52 |
| - $.ui.datastore.main = $.ui.datastore(); |
53 |
| - |
54 |
| - $.widget( "ui.datasource", { |
55 |
| - options: { |
56 |
| - type: null, |
57 |
| - source: null |
58 |
| - }, |
59 |
| - _create: function() { |
60 |
| - $.ui.datasource.types[ this.options.type ] = this; |
61 |
| - $.ui.datastore.main.items[ this.options.type ] = {}; |
62 |
| - }, |
63 |
| - create: function( props ) { |
64 |
| - this.options.source.push( props ); |
65 |
| - }, |
66 |
| - get: function( store ) { |
67 |
| - store._populate( this.options.type, this.options.source ); |
68 |
| - }, |
69 |
| - save: function( items ) { |
70 |
| - $.each( items, function( itemId, item ) { |
71 |
| - item.save(); |
72 |
| - }); |
73 |
| - }, |
74 |
| - }); |
75 |
| - $.ui.datasource.types = {}; |
76 |
| - |
77 |
| - $.widget( "ui.dataitem", { |
78 |
| - options: { |
79 |
| - data: null |
80 |
| - }, |
81 |
| - _create: function() { |
82 |
| - }, |
83 |
| - get: function( key ) { |
84 |
| - return this.options.data[ key ]; |
85 |
| - }, |
86 |
| - set: function( key, vaule ) { |
87 |
| - this.options.data[ key ] = value; |
88 |
| - return this; |
89 |
| - } |
90 |
| - }); |
91 |
| - $.ui.dataitem.types = {}; |
92 |
| - $.ui.dataitem.extend = function( type, prototype ) { |
93 |
| - $.widget( "ui.dataitem-" + type, $.ui.dataitem, prototype ); |
94 |
| - $.ui.dataitem.types[ type ] = $.ui[ "dataitem-" + type ]; |
95 |
| - }; |
96 |
| - |
97 |
| - $.widget( "ui.dataitems", { |
98 |
| - options: { |
99 |
| - items: null |
100 |
| - } |
101 |
| - }); |
102 |
| - |
103 |
| - $.widget( "ui.grid", { |
104 |
| - options: { |
105 |
| - type: null, |
106 |
| - rowTemplate: null |
107 |
| - }, |
108 |
| - _create: function() { |
109 |
| - if ( !this.options.type ) { |
110 |
| - this._parseData(); |
111 |
| - } |
112 |
| - var that = this; |
113 |
| - this.element.addClass( "ui-widget" ); |
114 |
| - this.element.find( "th" ).addClass( "ui-widget-header" ); |
115 |
| - this.element.delegate( "tbody > tr", "click", function( event ) { |
116 |
| - that._trigger( "select", event, { |
117 |
| - item: $.ui.datastore.main.get( that.options.type, |
118 |
| - $( this ).tmplItem().data.guid ) |
119 |
| - }); |
120 |
| - }); |
121 |
| - this.refresh(); |
122 |
| - }, |
123 |
| - refresh: function() { |
124 |
| - var tbody = this.element.find( "tbody" ).empty(), |
125 |
| - items = $.ui.datastore.main.get( this.options.type ).options.items, |
126 |
| - template = this.options.rowTemplate; |
127 |
| - $.each( items, function( itemId, item ) { |
128 |
| - $.tmpl( template, item.options.data ).appendTo( tbody ); |
129 |
| - }); |
130 |
| - tbody.find( "td" ).addClass( "ui-widget-content" ); |
131 |
| - }, |
132 |
| - _parseData: function() { |
133 |
| - var type = "generated" + $.now(); |
134 |
| - this.options.type = type; |
135 |
| - |
136 |
| - var fields = this.element.find( "th" ).map(function() { |
137 |
| - return $( this ).data( "field" ); |
138 |
| - }).get(); |
139 |
| - |
140 |
| - var items = this.element.find( "tbody" ).children().map(function() { |
141 |
| - var item = { guid: $( this ).data( "guid" ) }; |
142 |
| - $( this ).children().each(function( i ) { |
143 |
| - item[ fields[ i ] ] = $( this ).text(); |
144 |
| - }); |
145 |
| - return item; |
146 |
| - }).get(); |
147 |
| - |
148 |
| - var template = $.map( fields, function( field ) { |
149 |
| - return "<td>${" + field + "}</td>"; |
150 |
| - }).join( "" ); |
151 |
| - template = "<tr>" + template + "</tr>"; |
152 |
| - this.options.rowTemplate = template; |
153 |
| - |
154 |
| - $.ui.datasource({ |
155 |
| - type: type, |
156 |
| - source: items |
157 |
| - }); |
158 |
| - $.ui.dataitem.extend( type, {} ); |
159 |
| - $.ui.datastore.main.populate( type ); |
160 |
| - } |
161 |
| - }); |
162 |
| - |
163 |
| - //---------------------------------------- |
164 |
| - // Application level |
165 |
| - //---------------------------------------- |
166 |
| - |
| 18 | + // TODO this isn't used anywhere - how to access this method from #row-tmpl? |
| 19 | + // grid passed dataitem.options.data, that doesn't include access to this method |
167 | 20 | $.ui.dataitem.extend( "developer", {
|
168 | 21 | fullName: function() {
|
169 | 22 | return this.get( "firstName" ) + " " + this.get( "lastName" );
|
|
197 | 50 | }
|
198 | 51 | ];
|
199 | 52 |
|
| 53 | + // TODO what's the relation between datasource and datastore? |
| 54 | + // couldn't/shouldn't this be just one widget? |
200 | 55 | $.ui.datasource({
|
201 | 56 | type: "developer",
|
202 | 57 | source: data
|
203 | 58 | });
|
| 59 | + // TODO when will be a need for a non-main datastore? |
204 | 60 | $.ui.datastore.main.populate( "developer" );
|
205 | 61 |
|
206 | 62 | $(function() {
|
|
210 | 66 | select: showDetails
|
211 | 67 | });
|
212 | 68 |
|
| 69 | + // TODO shouldn't the extraction be seperate from the grid? |
| 70 | + // e.g. as a datasource extracting the table data and providing the result to grid |
213 | 71 | $( "#developers-pe" ).grid({
|
214 | 72 | select: showDetails
|
215 | 73 | });
|
216 | 74 |
|
| 75 | + // TODO this could be a form view, having to handle events |
| 76 | + // and modifying item accordingly |
| 77 | + // and telling datastore/source about the changes |
| 78 | + // |
| 79 | + // implement as extension of a view widget? |
217 | 80 | function showDetails( event, ui ) {
|
218 | 81 | $( "#developer-tmpl" )
|
| 82 | + // TODO accessing data though item.options makes no sense |
| 83 | + // this should be just ui.item passed to .tmpl |
219 | 84 | .tmpl( ui.item.options.data )
|
220 | 85 | .appendTo( $( "#developer" ).empty() );
|
221 | 86 | }
|
|
0 commit comments