Coder Social home page Coder Social logo

thrau / wicket-js Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 2.0 654 KB

wicket-js makes it easy to write and handle custom JavaScript and jQuery from within Wicket Components and Behaviors.

Home Page: http://rauschig.org/wicket-js

Java 100.00%
wicket wicket-components

wicket-js's Introduction

Wicket JavaScript and jQuery

Build Status ย  Coverage Status

wicket-js makes it easy to write and handle custom JavaScript and jQuery from within Wicket Components and Behaviors. It helps to separate Wicket Ajax Behavior listeners from actual client-side UI functionality, while maintaining the coupling towards Wicket Components.

It provides a (rudimentary) fluent interface that allows you to build JavaScript and jQuery expressions in Java.

Example

With wicket-js you can bind arbitrary JavaScript code to Wicket Components in a clean way. It provides basic syntax elements for typesafe script creation.

component.add(new JsBehavior() {
    @Override
    protected IJavaScript domReadyJs() {
        return new JsCall("alert", "loaded component with markup-id " + id());
    }
});

The API also provides a convenient way of writing jQuery expressions and bind client-side jQuery event behavior to components. This allows for separation of the Wicket AJAX listening mechanisms and the true client-side JavaScript functionality while keeping the JavaScript in the actuall Component definitions.

component.add(new JQueryEventBehavior("mouseenter mouseleave") {
    @Override
    protected IJavaScript callback() {
        return new JsStatements(
                $(this).toggleClass("entered"),
                $(this).trigger("toggled")
        );
    }
});

With jQuery's trigger and Wickets AjaxEventBehavior you can wire client-side JavaScript behavior with Wicket AJAX event handling in a clean way while keeping them separate.

component.add(new AjaxEventBehavior("toggled") {
    @Override
    protected void onEvent(AjaxRequestTarget target) {
        System.out.println("selection has changed!");
    }
});

Dependencies

  • wicket-core 6.11.0
  • jackson-databind [2.0,)

Notice

wicket-js is in alpha phase and subject to rapid development and change

wicket-js's People

Contributors

thrau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

minikiller

wicket-js's Issues

JsObject builder to build Map type objects

To avoid

Map<Object, Object> map = new HashMap<>();
map.put(k, v);
JsObject obj = JsLiteral.of(map);

Something like the BSON builder would be nice

new JsObject().put(k, v)

JsLink feature

Like Wickets AjaxLink only that it doesn't execute an ajax call.

JsBehavior for Widgets

jQuery widgets are usually initialized like this

$('#component').myWidget({
    "property1": "value1",
    "property2": 2,
});

this idiom could be encapsulated in a Behavior extension

Integration for AjaxRequestAttributes

The easiest way i found to add "dynamic" (evaluated expressions) parameters to an ajax request that can be retrieved at the target is the following:

@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
    super.updateAjaxAttributes(attributes);

    Map<Object, Object> map = new HashMap<>();
    map.put("answer", new JsExpression("39 + 3"));
    attributes.getDynamicExtraParameters().add(JsUtils.js(new JsReturn(JsLiteral.of(map))));
}

and then get it via

getRequest().getRequestParameters().getParameterValue("answer")

This is clumsy, but something you'll do quite often when interacting with frontend components via ajax.

Expression/Statement String arguments

Replace any occurrence of String arguments with CharSequence where possible.

E.g.

public JsIf(String expression, IJsStatement thenBlock) {
    this(new JsExpression(expression), thenBlock);
}

Convenient 'toString' methods to generate js strings

Having the toString methods of the actual IJavaScript tokens generate their own code probably isn't the best design decision, but would certainly make it easier to handle cases where wicket wants javascript strings, e.g. in

public void onClick(AjaxRequestTarget target) {
    target.prependJavaScript("alert('some javascript')");
}

maybe put a method into JsUtils that calls the necessary methods? Or put a static method directly into the generator?

Make JSON serialization consider IJavaScript tokens

currently

map.put("property", new JsCall("get", "foobar"));
new JsObject(map);

would translate into

{"property":{"function":{"identifier":"get"},"arguments":[{"value":"foo"}]}}

what would be more sensible is

{"property": get('foo')}

org.rauschig.wicketjs.util.JsonSerializer (more the jackson ObjectMapper) has to be adapted for this.

String escaping

Currently any type of string is quoted with single quotes. I haven't actually thought a whole lot about where and how to escape expression strings / string literals.

Look into cases where it might be required and implement this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.