Skip to content

Instantly share code, notes, and snippets.

@tmysik
Forked from jtulach/ListGists.java
Created March 4, 2014 12:37
Show Gist options
  • Save tmysik/9345731 to your computer and use it in GitHub Desktop.
Save tmysik/9345731 to your computer and use it in GitHub Desktop.
Networking with JSON and Java
User name:
<form>
<input data-bind="value: user, valueUpdate: 'afterkeydown'"
placeholder="Your own? Or try 'tmysik'">
</input>
<input type="submit" data-bind="click: computeGists, enable: validUser" value="Show!">
</form>
<h3><span data-bind="text: user"></span>'s Code Snippets</h3>
<!-- iterate through list of Gists and show their id and description -->
<ul data-bind="foreach: gists">
<li>
<span data-bind="text: id"></span> -
<a data-bind="attr: { href: html_url }, text: description" target="gists">
</a>
</li>
</ul>
package dew.demo.gists;
import java.util.Arrays;
import net.java.html.json.*;
@Model(className="UI", properties={
@Property(name="user",type=String.class),
@Property(name="gists",type=Gist.class, array = true)
})
class ListGists {
/** Generates class Gist which is a Java representation
* of appropriate JSON object send from the server.
*/
@Model(className="Gist", properties={
@Property(name="id", type=String.class),
@Property(name="url", type=String.class),
@Property(name="html_url", type=String.class),
@Property(name="description", type=String.class)
})
static class GistImpl {
}
@ComputedProperty static boolean validUser(String user) {
return user != null && !user.isEmpty();
}
/** Generates method <b>gists</b> into the associated model
* class UI which connects to here in specified URL, obtains
* results (asynchronously) and when list of gists is here,
* calls back to this method.
*/
@OnReceive(url = "https://api.github.com/users/{user}/gists")
static void gists(UI ui, Gist[] gists) {
// replace list of gists with new list
ui.getGists().clear();
ui.getGists().addAll(Arrays.asList(gists));
}
@Function static void computeGists(UI model) {
// when Show! button is pressed, query for gists of specified user
model.gists(model.getUser());
}
static {
UI m = new UI();
m.applyBindings();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment