Skip to content

Commit

Permalink
Fix timing error in addScriptToBody().
Browse files Browse the repository at this point in the history
It failed when we're in render response phase and the head script was
already added but not rendered yet at the moment the view scoped bean
was constructed for first time.
  • Loading branch information
Bauke Scholtz committed Oct 30, 2015
1 parent d8f8d29 commit 5216b57
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 8 additions & 11 deletions src/main/java/org/omnifaces/cdi/viewscope/ViewScopeManager.java
Expand Up @@ -18,6 +18,7 @@
import static org.omnifaces.util.Components.addScriptResourceToBody; import static org.omnifaces.util.Components.addScriptResourceToBody;
import static org.omnifaces.util.Components.addScriptResourceToHead; import static org.omnifaces.util.Components.addScriptResourceToHead;
import static org.omnifaces.util.Components.addScriptToBody; import static org.omnifaces.util.Components.addScriptToBody;
import static org.omnifaces.util.Faces.getContext;
import static org.omnifaces.util.Faces.getInitParameter; import static org.omnifaces.util.Faces.getInitParameter;
import static org.omnifaces.util.Faces.getViewAttribute; import static org.omnifaces.util.Faces.getViewAttribute;
import static org.omnifaces.util.Faces.setViewAttribute; import static org.omnifaces.util.Faces.setViewAttribute;
Expand All @@ -34,13 +35,14 @@
import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.BeanManager;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Inject; import javax.inject.Inject;


import org.omnifaces.application.ViewScopeEventListener; import org.omnifaces.application.ViewScopeEventListener;
import org.omnifaces.cdi.BeanStorage; import org.omnifaces.cdi.BeanStorage;
import org.omnifaces.cdi.ViewScoped; import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.el.ReadOnlyValueExpression;
import org.omnifaces.resourcehandler.ResourceIdentifier; import org.omnifaces.resourcehandler.ResourceIdentifier;
import org.omnifaces.util.Callback;
import org.omnifaces.util.Hacks; import org.omnifaces.util.Hacks;
import org.omnifaces.util.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import org.omnifaces.util.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import org.omnifaces.util.concurrentlinkedhashmap.EvictionListener; import org.omnifaces.util.concurrentlinkedhashmap.EvictionListener;
Expand Down Expand Up @@ -190,7 +192,7 @@ private int getMaxActiveViewScopes() {
* CDI bean storage will also be auto-created. * CDI bean storage will also be auto-created.
*/ */
private UUID getBeanStorageId(boolean create) { private UUID getBeanStorageId(boolean create) {
FacesContext context = FacesContext.getCurrentInstance(); FacesContext context = getContext();


if (isUnloadRequest(context)) { if (isUnloadRequest(context)) {
return UUID.fromString(getRequestParameter(context, "id")); return UUID.fromString(getRequestParameter(context, "id"));
Expand All @@ -217,15 +219,10 @@ private UUID getBeanStorageId(boolean create) {
private void createViewScope(UUID id) { private void createViewScope(UUID id) {
activeViewScopes.put(id, new BeanStorage(DEFAULT_BEANS_PER_VIEW_SCOPE)); activeViewScopes.put(id, new BeanStorage(DEFAULT_BEANS_PER_VIEW_SCOPE));


FacesContext context = FacesContext.getCurrentInstance(); addScriptResourceToHead("omnifaces", "omnifaces.js");

addScriptResourceToBody("omnifaces", "unload.js").setValueExpression("rendered", new ReadOnlyValueExpression(Boolean.class, new Callback.SerializableReturning<Object>() { private static final long serialVersionUID = 1L; @Override public Object invoke() {
if (context.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE) { return !Hacks.isScriptResourceRendered(getContext(), new ResourceIdentifier("omnifaces", "omnifaces.js"));
addScriptResourceToHead("omnifaces", "omnifaces.js"); }}));
}
else if (!Hacks.isScriptResourceRendered(context, new ResourceIdentifier("omnifaces", "omnifaces.js"))) {
addScriptResourceToBody("omnifaces", "unload.js");
}

addScriptToBody("OmniFaces.Unload.init('" + id + "')"); addScriptToBody("OmniFaces.Unload.init('" + id + "')");
} }


Expand Down
Expand Up @@ -121,7 +121,7 @@ public int hashCode() {


@Override @Override
public boolean isLiteralText() { public boolean isLiteralText() {
return true; return callbackReturning == null && callbackWithArgument == null;
} }


/** /**
Expand Down

0 comments on commit 5216b57

Please sign in to comment.