-
-
Save eppleton/bd1a8c7d2d064beed2c4 to your computer and use it in GitHub Desktop.
Shows usage of @function callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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