01: /*
02: * GWT-Ext Widget Library
03: * Copyright(c) 2007-2008, GWT-Ext.
04: * licensing@gwt-ext.com
05: *
06: * http://www.gwt-ext.com/license
07: */
08:
09: package com.gwtext.client.data;
10:
11: import com.google.gwt.core.client.JavaScriptObject;
12: import com.gwtext.client.util.JavaScriptObjectHelper;
13:
14: /**
15: * Small helper class to make creating Stores for JSON data easier.
16: * <pre>
17: * <code>
18: * JsonStore store = new JsonStore("get-images.php", "images",new RecordDef(new FieldDef[]{
19: * new StringFieldDef("name"),
20: * new StringFieldDef("url"),
21: * new FloatFieldDef("size"),
22: * new DateFieldDef("lastmod")}));
23: * </code>
24: * </pre>
25: * <p/>
26: * This would consume a returned object of the form:
27: *
28: * <pre>
29: * <code>
30: * {
31: * images: [
32: * {name: <em>'Image one'</em>, url:<em>'/GetImage.php?id=1'</em>, size:46.5, lastmod: <b>new</b> Date(2007, 10, 29)},
33: * {name: <em>'Image Two'</em>, url:<em>'/GetImage.php?id=2'</em>, size:43.2, lastmod: <b>new</b> Date(2007, 10, 30)}
34: * ]
35: * }
36: * </code>
37: * </pre>
38: */
39: public class JsonStore extends Store {
40:
41: /**
42: * Create a new JsonStore.
43: *
44: * @param url the URL that returns the Json data
45: * @param root name of the property which contains the Array of row objects
46: * @param fields the name of the fields
47: */
48: public JsonStore(String url, String root, String[] fields) {
49: JavaScriptObject config = JavaScriptObjectHelper.createObject();
50: JavaScriptObjectHelper.setAttribute(config, "url", url);
51: JavaScriptObjectHelper.setAttribute(config, "root", root);
52: JavaScriptObjectHelper.setAttribute(config, "fields", fields);
53: jsObj = create(config);
54: }
55:
56: /**
57: * Create a new JsonStore.
58: *
59: * @param url the URL that returns the Json data
60: * @param root name of the property which contains the Array of row objects
61: * @param recordDef the record def
62: */
63: public JsonStore(String url, String root, RecordDef recordDef) {
64: JavaScriptObject config = JavaScriptObjectHelper.createObject();
65: JavaScriptObjectHelper.setAttribute(config, "url", url);
66: JavaScriptObjectHelper.setAttribute(config, "root", root);
67: JavaScriptObjectHelper.setAttribute(config, "recordType",
68: recordDef.getJsObj());
69: jsObj = create(config);
70: }
71:
72: native JavaScriptObject create(JavaScriptObject config) /*-{
73: return new $wnd.Ext.data.JsonStore(config);
74: }-*/;
75: }
|