A throttling decorator would look like this:
public static class ThrottlingDecorator extends JavaScript { private static final JavaScript THROTTLER = new JavaScript( "wicketThrottler.throttle('${id}', ${milliseconds}, ${function});"); public ThrottlingDecorator(final String id, final Duration maxFrequency, final JavaScript script) { super(THROTTLER.merge("id", id, "milliseconds", maxFrequency .getMilliseconds(), "function", script.function())); } } and usage of that would be: code = new ThrottlingDecorator("id", Duration.ONE_SECOND, code); Jonathan Locke wrote: > > Something like class below (although perhaps more efficient) could help us > to decorate and merge javascript in our AJAX code. Because code merges > only occur one layer at a time, it's as good as an AST for our simple > purposes (we don't need to globally refactor JavaScript, for example, only > combine it neatly), but much easier to use. I personally would have no > problem breaking the API to make this better, but we could also do this > with 100% backwards compat by simply having things that need JavaScript > make a call to a getWhateverJavaScript() method first and if that returns > null (in the default impl), it could proceed to do what it does now. So > basically if you wanted to assemble your script this new way, you could > override these new methods and the old stuff would be bypassed. Feedback? > > Jonathan > > --- > > package thoof.util.javascript; > > import java.io.IOException; > import java.io.InputStream; > import java.util.Collections; > import java.util.HashMap; > import java.util.Map; > import java.util.regex.Pattern; > > import org.apache.wicket.util.io.Streams; > import org.apache.wicket.util.string.interpolator.MapVariableInterpolator; > > public class JavaScript { > > private final String script; > > private static final Pattern UNINTERPOLATED_VARIABLES = Pattern > .compile("\\s*\\$\\{\\w+\\}\\s*"); > > public JavaScript(final String script) { > this.script = script; > } > > public static JavaScript load(final Class<?> type, final String > resourceName) { > return load(type.getResourceAsStream(resourceName)); > } > > public static JavaScript load(final InputStream in) { > try { > return new JavaScript(Streams.readString(in)); > } catch (final IOException e) { > throw new IllegalStateException("Cannot load email template", > e); > } > } > > public final JavaScript merge(final Map<String, Object> map) { > final String mergedScript = new > MapVariableInterpolator(this.script, > map).toString(); > return new > JavaScript(UNINTERPOLATED_VARIABLES.matcher(mergedScript) > .replaceAll(" ")); > } > > public final JavaScript merge(final Object... args) { > if (args.length % 2 != 0) { > throw new IllegalArgumentException( > "Invalid interpolation arguments"); > } > final Map<String, Object> map = new HashMap<String, Object>(); > for (int i = 0; i < args.length; i += 2) { > if (args[i] instanceof String) { > map.put((String) args[i], args[i + 1]); > } else { > throw new IllegalArgumentException( > "Invalid interpolation arguments"); > } > } > return merge(map); > } > > public final JavaScript prepend(final JavaScript script) { > return new JavaScript(script + ";" + this.script); > } > > public final JavaScript append(final JavaScript script) { > return new JavaScript(this.script + ";" + script); > } > > public final JavaScript function(final String functionName) { > return new JavaScript("var " + functionName + " = function { " + > script > + "};"); > } > > @Override > public String toString() { > return MapVariableInterpolator.interpolate(script, > Collections.EMPTY_MAP); > } > > public static void main(final String arguments[]) { > JavaScript code = new JavaScript("var x = 10 + 3"); > System.out.println("" + code.toString()); > code = code.prepend(new JavaScript("a + b")); > System.out.println("" + code.toString()); > code = code.append(new JavaScript("c + d")); > System.out.println("" + code.toString()); > code = code.function("callback"); > System.out.println("" + code.toString()); > JavaScript ajax = new JavaScript( > "var wcall; ${beforeCallback} wcall = > wicketAjaxGet('${url}', function() { ${success} }, function() { ${failure} > }); ${afterCallback} return !wcall;);"); > code = ajax.merge("url", "/abc/def", "beforeCallback", code, > "afterCallback", "a = b + c + d"); > System.out.println("" + code.toString()); > } > } > > -- View this message in context: http://www.nabble.com/JavaScript-object-tf3610605.html#a10090957 Sent from the Wicket - Dev mailing list archive at Nabble.com.