package com.mycompany.ractive;

import com.mycompany.json.Json;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestHandler;

/**
 *
 */
public class RactiveRequestHandler extends AjaxRequestHandler
{
	/**
	 * Constructor
	 *
	 * @param page the currently active page
	 */
	public RactiveRequestHandler(Page page)
	{
		super(page);
	}

	@Override
	public void add(Component... components)
	{
		for (Component component : components)
		{
			boolean processed = false;
			Component cursor = component;
			while (cursor instanceof Page == false)
			{
				Object modelObject = cursor.getDefaultModelObject();
				if (modelObject instanceof RactiveObject) {

					String json = Json.stringify(cursor);

					appendJavaScript(String.format("Wicket.Ractive['%s'].set('w', %s)", cursor.getMarkupId(), json));
					processed = true;
					break;
				}
				cursor = cursor.getParent();
			}
			if (processed == false)
			{
				super.add(component);
			}
		}
	}
}

@@ -25,16 +25,70 @@
 ctive;%0A%0A
+import java.util.Deque;%0Aimport java.util.LinkedList;%0A%0A
 import c
@@ -207,31 +207,177 @@
 ket.
-ajax.AjaxRequestHandler
+WicketRuntimeException;%0Aimport org.apache.wicket.ajax.AjaxRequestHandler;%0Aimport org.apache.wicket.ajax.json.JSONException;%0Aimport org.apache.wicket.ajax.json.JSONObject
 ;%0A%0A/
@@ -716,16 +716,17 @@
  false;%0A
+%0A
 %09%09%09Compo
@@ -750,16 +750,67 @@
 ponent;%0A
+%09%09%09Deque%3CString%3E paths = new LinkedList%3CString%3E();%0A
 %09%09%09while
@@ -955,54 +955,291 @@
 ect)
- %7B%0A%0A%09%09%09%09%09String json = Json.stringify(cursor);
+%0A%09%09%09%09%7B%0A%09%09%09%09%09CharSequence childPath = getChildPath(paths);%0A%09%09%09%09%09JSONObject json = Json.toJsonObject(cursor);%0A%09%09%09%09%09JSONObject childJson;%0A%09%09%09%09%09try%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09childJson = getChildJson(json, paths);%0A%09%09%09%09%09%7D catch (JSONException e)%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09throw new WicketRuntimeException(e);%0A%09%09%09%09%09%7D
 %0A%0A%09%09
@@ -1300,16 +1300,18 @@
 %5D.set('w
+%25s
 ', %25s)%22,
@@ -1333,17 +1333,33 @@
 upId(), 
-j
+childPath, childJ
 son));%0A%09
@@ -1398,16 +1398,52 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09paths.addFirst(cursor.getId());%0A
 %09%09%09%09curs
@@ -1541,11 +1541,510 @@
 %7D%0A%09%09%7D%0A%09%7D
+%0A%0A%09private CharSequence getChildPath(Deque%3CString%3E paths)%0A%09%7B%0A%09%09StringBuilder result = new StringBuilder();%0A%09%09if (paths.isEmpty() == false)%0A%09%09%7B%0A%09%09%09for (String path : paths)%0A%09%09%09%7B%0A%09%09%09%09result.append('.').append(path);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return result;%0A%09%7D%0A%0A%09private JSONObject getChildJson(JSONObject json, Deque%3CString%3E paths) throws JSONException%0A%09%7B%0A%09%09if (paths.isEmpty())%0A%09%09%7B%0A%09%09%09return json;%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09String path = paths.removeFirst();%0A%09%09%09return getChildJson(json.getJSONObject(path), paths);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Return only the JSON for the changed inner object of the RactiveObject