2008年3月9日日曜日

GWT on Rails : CRUDの'R' Read (Referenceではないんだ…)

ということでDashboard.javaを編集してみる.まずは初回同様参照.
app/gwt/dashboard/src/dashboard/client/Dashboard.java
//import類は省くが困ったらgwtのモノを選んどけばたいてい大丈夫
public class Dashboard implements EntryPoint {
public void onModuleLoad() {
final Button button = new Button("Click me");
final Label label = new Label();
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {

//add
ResourceCollection projects = new RailsJsonResourceCollection(
"http://localhost:3000/projects/", new ResourceFactory() {
public Resource create() {
return (Resource) GWT
.create(Project.class);
}
});
projects.find(1,
new ResourceResponseHandler() {
public void onSuccess(Resource resource) {
Window.alert("Project name is " + ((Project) resource).getName());
}

public void onError(Request request, Throwable exception) {
Window.alert("Something went wrong.");
}
});

//add

if (label.getText().equals(""))
label.setText("Hello World!");
else
label.setText("");
}
});
//slotは'app/views/dashboard/index.html.erb'内のidでの定義.
//button, labelはそこにハマる.
RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label);
}
}

これはid=1を取得する例.id=1を取得後,name属性をalertで表示する.
RailsJsonResourceCollectionの第一引数はJSON Requestを処理するアクセスポイントである.なので単に"/projects/"でも動く.その方が便利かも.

> rake dashbaord:compile
> script/server

ブラウザにて'http://localhost:3000/dashboard/'へアクセス
RailsJsonResourceCollectionの第一引数とこのアクセスサーバは文字列的に同じでないとXMLHttpRequestがセキュリティーを問題視して問合せをしないので注意.してしまうブラウザはそれはそれで注意(そんなのあるの?).そういう意味でも"http://.../projects/"とするより"/projects/"の方が便利であり可搬性(?)があるね.

0 件のコメント: