开发者

ExtGWT- GXT, how can i define a class that extend from BaseModel?

I have read some tutorials, documentations but i don't know how to define a class that extend from BaseModel or ModelData? Specifically, I don't know how to name getter and setter methods and the values ins开发者_如何学Goide the method. Are there any code convention? Example:

public void setName(String value) {
    set("name", value);  //why is it "name"? does it relate to a attribute of Data Object?
}

public String getName() {
    return get("name");
}

In "Appress Developing with ExtGWT" they said: "ModelData allows a widget to interrogate data objects without having to have concrete support for any particular data type, providing separation from the model and view".

Could i infer that instead of creating a instance like:

Foo foo= new Foo(); foo.getName();

I can call "getName()" ?


Heres what I normally do when coding a pojo that extends ModelData. Essentially calling set() or get() just accesses a hashmap. This allows your data object to have loosly typed properties for "reflection type" runtime access. So effectively you can pass any string as the first parameter and that will define the property name.

I use static final strings to make it easier to to maintain the object.

You can also interface the getters and setters if you want separation of layers WRT your data objects but that might make GXT type binding harder.

public class MockModel extends BeanModel implements ModelData {
private static final long serialVersionUID = -5276682038816452567L;

public static final String ID = "ID";
public static final String NAME = "NAME";
public static final String DESCRIPTION = "DESCRIPTION";
public static final String FIELD1 = "FIELD1";
public static final String FIELD2 = "FIELD2";
public static final String FUNCTION1 = "FUNCTION1";
public static final String FUNCTION2 = "FUNCTION2";

public MockModel(int id, String name, String desc, int f1, int f2) {
    set(ID,id);
    set(NAME,name);
    set(DESCRIPTION,desc);
    set(FIELD1,f1);
    set(FIELD2,f2);
    set(FUNCTION1, 0);
    set(FUNCTION2, 0);
}

}


Here our class, which extends the BaseModel

import com.extjs.gxt.ui.client.data.BaseModel;

public class Order extends BaseModel {

 private static final long serialVersionUID = 1L;

 /**
  * Default constructor
  */
 public Order() {
  super();
 }

 public Order(Integer lieferungID) {
  this();

  setLieferungID(lieferungID); 
 }

 public void setLieferungID(int lieferungID) {
  set("lieferungID", lieferungID);
 }

 public Integer getLieferungID() {
  return get("lieferungID");
 }
}

I'm using this class to fill a paging and simple grids with data and it works perfect. Your getter and setter looks good...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜