Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@eppleton
Forked from jtulach/NamesModel.java
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eppleton/bd1a8c7d2d064beed2c4 to your computer and use it in GitHub Desktop.
Save eppleton/bd1a8c7d2d064beed2c4 to your computer and use it in GitHub Desktop.
Shows usage of @function callback
package dew.demo.namesmodel;
import java.util.List;
import net.java.html.json.Function;
import net.java.html.json.Model;
import net.java.html.json.ModelOperation;
import net.java.html.json.OnReceive;
import net.java.html.json.Property;
@Model(className = "Repositories", properties = {
@Property(name = "repos", type = Repo.class, array = true),
@Property(name = "url", type = String.class)
})
final class DataModel {
static{
Repositoriespos d = new Repositories();
final String baseUrl = "https://api.github.com/users/eppleton/repos";
d.setUrl(baseUrl);
d.applyBindings();
d.connect();
}
@ModelOperation
@Function
static void connect(Repos data) {
final String u = data.getUrl();
if (u.endsWith("/")) {
data.setUrl(u.substring(0, u.length() - 1));
}
data.loadRepos(data.getUrl());
}
@OnReceive(url = "{url}", onError = "cannotConnect")
static void loadRepos(Repos ui, List<Repo> arr) {
ui.getRepos().clear();
ui.getRepos().addAll(arr);
}
static void cannotConnect(Repos data, Exception ex) {
// data.setMessage("Cannot connect " + ex.getMessage() + ". Should not you start the server project first?");
}
@Model(className = "Repo", properties = {
@Property(name = "name", type = String.class)})
public static class RepoModel {
}
}
<h1>Repo Demo</h1>
<ol data-bind="foreach: repos">
<li data-bind="text: name"></li>
</ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment