001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.data;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013:
014: /**
015: * Data reader class to create an Array of Ext.data.Record objects from a JSON response
016: * based on mappings in a provided Ext.data.Record constructor.
017: * <p>
018: * Example code:
019: * </p><pre><code>
020: * RecordDef recordDef = new RecordDef(new FieldDef[]{
021: * new StringFieldDef("name", "name"), // "mapping" property not needed if it's the same as "name"
022: * new StringFieldDef("occupation") // this field will use "occupation" as the mapping.
023: * });
024: * <p/>
025: * JsonReader reader = new JsonReader(new JsonReaderConfig() {
026: * {
027: * setTotalProperty("results"); // The property which contains the total dataset size (optional)
028: * setRoot("rows"); // The property which contains an Array of row objects
029: * setId("id"); // The property within each row object that provides an ID for the record (optional)
030: * }}, recordDef);
031: * </code>
032: * </pre>
033: * <p>
034: * This would consume a JSON file like this:
035: * </p><pre><code>{ <em>'results'</em>: 2, <em>'rows'</em>: [
036: * { <em>'id'</em>: 1, <em>'name'</em>: <em>'Bill'</em>, occupation: <em>'Gardener'</em> },
037: * { <em>'id'</em>: 2, <em>'name'</em>: <em>'Ben'</em>, occupation: <em>'Horticulturalist'</em> } ]
038: * }</code></pre>
039: */
040: public class JsonReader extends Reader {
041:
042: /**
043: * Constructs a new JsonReader.
044: *
045: * @param recordDef the record def
046: */
047: public JsonReader(RecordDef recordDef) {
048: setRecordDef(recordDef);
049: }
050:
051: /**
052: * Constructs a new JsonReader.
053: *
054: * @param root the root property
055: * @param recordDef the record def
056: */
057: public JsonReader(String root, RecordDef recordDef) {
058: setRoot(root);
059: setRecordDef(recordDef);
060: }
061:
062: protected native JavaScriptObject create(JavaScriptObject config,
063: JavaScriptObject recordDef) /*-{
064: return new $wnd.Ext.data.JsonReader(config, recordDef);
065: }-*/;
066:
067: //config
068:
069: /**
070: * Name of the property within a row object that contains a record identifier value.
071: *
072: * @param id the id property
073: */
074: public void setId(String id) {
075: JavaScriptObjectHelper.setAttribute(configJS, "id", id);
076: }
077:
078: /**
079: * Name of the property which contains the Array of row objects.
080: *
081: * @param root the root property
082: */
083: public void setRoot(String root) {
084: JavaScriptObjectHelper.setAttribute(configJS, "root", root);
085: }
086:
087: /**
088: * Name of the property from which to retrieve the success attribute used by forms.
089: *
090: * @param successProperty the success property
091: */
092: public void setSuccessProperty(String successProperty) {
093: JavaScriptObjectHelper.setAttribute(configJS,
094: "successProperty", successProperty);
095: }
096:
097: /**
098: * Name of the property from which to retrieve the total number of records in the dataset.
099: * This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server.
100: *
101: * @param totalProperty proeprty for total number of records
102: */
103: public void setTotalProperty(String totalProperty) {
104: JavaScriptObjectHelper.setAttribute(configJS, "totalProperty",
105: totalProperty);
106: }
107: }
|