Permalink
Browse files

More robust and efficient handling of unload request.

1) it broke when state has changed on same view
2) no need to perform full buildView, only createView with
restoreViewScopeState is sufficient and faster
  • Loading branch information...
BalusC committed Oct 16, 2015
1 parent 4910a03 commit ad44be18522554ba8a5145d1daf7cb2b72b5a703
@@ -190,6 +190,12 @@ private int getMaxActiveViewScopes() {
* CDI bean storage will also be auto-created.
*/
private UUID getBeanStorageId(boolean create) {
+ FacesContext context = FacesContext.getCurrentInstance();
+
+ if (isUnloadRequest(context)) {
+ return UUID.fromString(getRequestParameter(context, "id"));
+ }
+
UUID id = getViewAttribute(ViewScopeManager.class.getName());
if (id == null || activeViewScopes.get(id) == null) {
@@ -205,6 +211,9 @@ private UUID getBeanStorageId(boolean create) {
return id;
}
+ /**
+ * Create CDI bean storage and register unload script.
+ */
private void createViewScope(UUID id) {
activeViewScopes.put(id, new BeanStorage(DEFAULT_BEANS_PER_VIEW_SCOPE));
@@ -217,7 +226,7 @@ else if (!Hacks.isScriptResourceRendered(context, new ResourceIdentifier("omnifa
addScriptResourceToBody("omnifaces", "unload.js");
}
- addScriptToBody("OmniFaces.Unload.init()");
+ addScriptToBody("OmniFaces.Unload.init('" + id + "')");
}
/**
@@ -74,9 +74,10 @@ public RestorableViewHandler(ViewHandler wrapped) {
*/
@Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
- if (isUnloadRequest(context) && getRenderKit(context).getResponseStateManager().getState(context, viewId) == null) {
- context.responseComplete();
- return new UIViewRoot();
+ if (isUnloadRequest(context)) {
+ UIViewRoot createdView = createView(context, viewId);
+ createdView.restoreViewScopeState(context, getRenderKit(context).getResponseStateManager().getState(context, viewId));
+ return createdView;
}
UIViewRoot restoredView = super.restoreView(context, viewId);
@@ -85,27 +86,23 @@ public UIViewRoot restoreView(FacesContext context, String viewId) {
return restoredView;
}
- UIViewRoot createdView;
-
try {
- createdView = buildView(viewId);
+ UIViewRoot createdView = buildView(viewId);
+ return isRestorableView(createdView) ? createdView : null;
}
catch (IOException e) {
throw new FacesException(e);
}
-
- if (TRUE.equals(createdView.getAttributes().get(EnableRestorableView.class.getName()))) {
- return createdView;
- }
- else {
- return null;
- }
}
private boolean isRestorableViewEnabled(FacesContext context) {
return TRUE.equals(getApplicationAttribute(context, EnableRestorableView.class.getName()));
}
+ private boolean isRestorableView(UIViewRoot view) {
+ return TRUE.equals(view.getAttributes().get(EnableRestorableView.class.getName()));
+ }
+
@Override
public ViewHandler getWrapped() {
return wrapped;

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -32,7 +32,7 @@ OmniFaces.Unload = (function() {
/**
* Initialize the "unload" event listener on the current document.
*/
- unload.init = function() {
+ unload.init = function(id) {
if (!window.XMLHttpRequest) {
return; // Native XHR not supported (IE6/7 not supported). End of story. Let session expiration do its job.
}
@@ -53,7 +53,7 @@ OmniFaces.Unload = (function() {
var xhr = new XMLHttpRequest();
xhr.open("POST", window.location.href.split(/[?#;]/)[0], false);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xhr.send("omnifaces.event=unload&" + VIEW_STATE_PARAM + "=" + viewState);
+ xhr.send("omnifaces.event=unload&id=" + id + "&" + VIEW_STATE_PARAM + "=" + viewState);
}
catch (e) {
// Fail silently. You never know.

0 comments on commit ad44be1

Please sign in to comment.