HerrB
21 Jun 2009, 4:32 AM
It has cost me some time to understand, how a ModelData class, JSONReader, HTTPProxy, ASP.NET 3.5 webservices and folder/leaf declaration have to be combined to work. The examples are great, but I needed a little bit more explanations. I will describe an example, which may answer some questions I've stumbled upon...
Note: I'm still working on a complete solution. You shouldn't follow the remote part, as using "GET" to get the data needed from an ASP.NET webservice is a BAD approach. I will post a complete solution, when I'm finished.
Let's start:
TreePanel uses a TreeStore to store the items shown in the tree. An item is called a "model" in ExtJS GWT. You have to specify which properties are available for each model (e.g. a name, an id).
Example:
The employees within a department could be described as a "Person" item. Each employee has a lastname, a firstname and an id. The PersonModel will get the properties "lastname", "firstname" and "itemid" (at least).
PersonModel.java:
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.DataField;
import com.extjs.gxt.ui.client.data.ModelType;
public class PersonModel extends BaseModelData {
//...
public PersonModel() {
// Initialize all properties -> avoid "null" exceptions
setItemID("0");
setLastName("");
setFirstName("");
setHasChildren(false);
}
public PersonModel(String strItemID, String strLastName, String strFirstName) {
setItemID(strItemID);
setLastName(strLastName);
setFirstName(strFirstName);
}
public void setItemID(String strItemID) {
set("itemid", strItemID);
}
public void setLastName(String strLastName) {
set("lastname", strLastName);
updateFullName();
}
public void setFirstName(String strFirstName) {
set("firstname", strFirstName);
updateFullName();
}
public void updateFullName() {
if (get("lastname") != "" && get("firstname") != "") {
set("fullname", get("lastname") + ", " + get("firstname"));
} else if (get("lastname") != "") {
set("fullname", get("lastname"));
} else {
set("fullname", "");
}
}
public void setHasChildren(boolean blnHasChildren) {
set("hasChildren", blnHasChildren);
}
public String getItemID() {
return get("itemid");
}
public String getLastName() {
return get("lastname");
}
public String getFirstName() {
return get("firstname");
}
public String getFullName() {
return get("fullname");
}
public boolean getHasChildren() {
return (Boolean)get("hasChildren");
}
static public ModelType getModelType() {
ModelType objType = new ModelType();
// Use "d" as root for ASP.NET 3.5 webservice
objType.setRoot("d");
DataField objField;
objField = new DataField("itemid");
objField.setMap("strId");
objType.addField(objField);
objField = new DataField("lastname");
objField.setMap("strLastName");
objType.addField(objField);
objField = new DataField("firstname");
objField.setMap("strFirstName");
objType.addField(objField);
objField = new DataField("hasChildren");
objField.setMap("blnHasChildren");
objField.setType(Boolean.class);
objType.addField(objField);
return objType;
}
// Complete items are accepted as equal, if they have the same item id
// Override the equals method of the BaseModelData implentation to compare
// just the item id
@Override
public boolean equals(Object objObject) {
if (objObject != null && objObject instanceof PersonModel) {
PersonModel objPerson = (PersonModel) objObject;
return getItemID().equals(objPerson.getItemID());
}
return super.equals(objObject);
}
}
If you use a remote data reader (e.g. XML, JSON), you have to specify a "ModelType" which primarely describes, how a property is mapped (assigned) to the data items you receive from the server. This mapping has been specified in the getModelType method.
Example:
Here, the value for "itemid" of the PersonModel item will be received as "strId" from the server. The value for "hasChildren" will be received as "blnHasChildren".
Best regards,
HerrB
Note: I'm still working on a complete solution. You shouldn't follow the remote part, as using "GET" to get the data needed from an ASP.NET webservice is a BAD approach. I will post a complete solution, when I'm finished.
Let's start:
TreePanel uses a TreeStore to store the items shown in the tree. An item is called a "model" in ExtJS GWT. You have to specify which properties are available for each model (e.g. a name, an id).
Example:
The employees within a department could be described as a "Person" item. Each employee has a lastname, a firstname and an id. The PersonModel will get the properties "lastname", "firstname" and "itemid" (at least).
PersonModel.java:
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.DataField;
import com.extjs.gxt.ui.client.data.ModelType;
public class PersonModel extends BaseModelData {
//...
public PersonModel() {
// Initialize all properties -> avoid "null" exceptions
setItemID("0");
setLastName("");
setFirstName("");
setHasChildren(false);
}
public PersonModel(String strItemID, String strLastName, String strFirstName) {
setItemID(strItemID);
setLastName(strLastName);
setFirstName(strFirstName);
}
public void setItemID(String strItemID) {
set("itemid", strItemID);
}
public void setLastName(String strLastName) {
set("lastname", strLastName);
updateFullName();
}
public void setFirstName(String strFirstName) {
set("firstname", strFirstName);
updateFullName();
}
public void updateFullName() {
if (get("lastname") != "" && get("firstname") != "") {
set("fullname", get("lastname") + ", " + get("firstname"));
} else if (get("lastname") != "") {
set("fullname", get("lastname"));
} else {
set("fullname", "");
}
}
public void setHasChildren(boolean blnHasChildren) {
set("hasChildren", blnHasChildren);
}
public String getItemID() {
return get("itemid");
}
public String getLastName() {
return get("lastname");
}
public String getFirstName() {
return get("firstname");
}
public String getFullName() {
return get("fullname");
}
public boolean getHasChildren() {
return (Boolean)get("hasChildren");
}
static public ModelType getModelType() {
ModelType objType = new ModelType();
// Use "d" as root for ASP.NET 3.5 webservice
objType.setRoot("d");
DataField objField;
objField = new DataField("itemid");
objField.setMap("strId");
objType.addField(objField);
objField = new DataField("lastname");
objField.setMap("strLastName");
objType.addField(objField);
objField = new DataField("firstname");
objField.setMap("strFirstName");
objType.addField(objField);
objField = new DataField("hasChildren");
objField.setMap("blnHasChildren");
objField.setType(Boolean.class);
objType.addField(objField);
return objType;
}
// Complete items are accepted as equal, if they have the same item id
// Override the equals method of the BaseModelData implentation to compare
// just the item id
@Override
public boolean equals(Object objObject) {
if (objObject != null && objObject instanceof PersonModel) {
PersonModel objPerson = (PersonModel) objObject;
return getItemID().equals(objPerson.getItemID());
}
return super.equals(objObject);
}
}
If you use a remote data reader (e.g. XML, JSON), you have to specify a "ModelType" which primarely describes, how a property is mapped (assigned) to the data items you receive from the server. This mapping has been specified in the getModelType method.
Example:
Here, the value for "itemid" of the PersonModel item will be received as "strId" from the server. The value for "hasChildren" will be received as "blnHasChildren".
Best regards,
HerrB