X

Geertjan's Blog

  • April 22, 2014

Meaningful HTML/Java DukeScript Framework Applications (Part 1)

Geertjan Wielenga
Product Manager

In an effort to make a meaningful application on the DukeScript Framework, here's the start of a JPA-based CRUD app (right now, only R is supported):

There are three parts to this.

  1. Model. I generated JPA entity classes from my database. I ended up with the "Customer" class, which has relationships with "DiscountCode" and "MicroMarket". Then I added a new annotation, on top of all the JPA annotations, to the "Customer" class:

    @Model(className = "CustomerData", properties = {})

    Then, in the same class, i.e., in "Customer", I exposed the data as follows:

    @ComputedProperty
    static java.util.List customers() {
    EntityManager entityManager
    = Persistence.createEntityManagerFactory("myPU").
    createEntityManager();
    Query query = entityManager.createNamedQuery("Customer.findAll");
    return query.getResultList();
    }
  2. Controler. In the Main class, I changed "onPageLoad" to this:
    public static void onPageLoad(String... args) throws Exception {
    new CustomerData().applyBindings();
    }
  3. View. Finally, the "index.html" that is bound to the model is as follows:
    <html>
    <body>
    <h1>Sample Database</h1>
    <table border="1">
    <thead>
    <tr>
    <th>Name</th>
    <th>City</th>
    <th>State</th>
    <th>Mail</th>
    </tr>
    </thead>
    <tbody data-bind="foreach: customers">
    <tr>
    <td data-bind="text: $data.getName()"></td>
    <td data-bind="text: $data.getCity()"></td>
    <td data-bind="text: $data.getState()"></td>
    <td data-bind="text: $data.getEmail()"></td>
    </tr>
    </tbody>
    </table>
    <p>
    </body>
    </html>

Notice that the method "customers()" in the model is bound in the "foreach" in the view.

Probably I'm doing all kinds of things wrong. Probably I should be using properties in the model. Happy to be corrected. 

Join the discussion

Comments ( 2 )
  • guest Wednesday, April 23, 2014

    Nice try, but needs few homey touches. I've sent you an improved version.


  • Jade Monday, October 20, 2014

    Hello, just wanted to ask. If this is converted to javascript? How does it connect to the Database? Does dukescript use NodeJS?


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.