Permalink
Browse files

Fix timing error in addScriptToBody().

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...
1 parent d8f8d29 commit 5216b57504f4a14ed5b99422e8f811d2b94bc418 @BalusC BalusC committed Oct 30, 2015
@@ -18,6 +18,7 @@
import static org.omnifaces.util.Components.addScriptResourceToBody;
import static org.omnifaces.util.Components.addScriptResourceToHead;
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.getViewAttribute;
import static org.omnifaces.util.Faces.setViewAttribute;
@@ -34,13 +35,14 @@
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.BeanManager;
import javax.faces.context.FacesContext;
-import javax.faces.event.PhaseId;
import javax.inject.Inject;
import org.omnifaces.application.ViewScopeEventListener;
import org.omnifaces.cdi.BeanStorage;
import org.omnifaces.cdi.ViewScoped;
+import org.omnifaces.el.ReadOnlyValueExpression;
import org.omnifaces.resourcehandler.ResourceIdentifier;
+import org.omnifaces.util.Callback;
import org.omnifaces.util.Hacks;
import org.omnifaces.util.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import org.omnifaces.util.concurrentlinkedhashmap.EvictionListener;
@@ -190,7 +192,7 @@ private int getMaxActiveViewScopes() {
* CDI bean storage will also be auto-created.
*/
private UUID getBeanStorageId(boolean create) {
- FacesContext context = FacesContext.getCurrentInstance();
+ FacesContext context = getContext();
if (isUnloadRequest(context)) {
return UUID.fromString(getRequestParameter(context, "id"));
@@ -217,15 +219,10 @@ private UUID getBeanStorageId(boolean create) {
private void createViewScope(UUID id) {
activeViewScopes.put(id, new BeanStorage(DEFAULT_BEANS_PER_VIEW_SCOPE));
- FacesContext context = FacesContext.getCurrentInstance();
-
- if (context.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE) {
- addScriptResourceToHead("omnifaces", "omnifaces.js");
- }
- else if (!Hacks.isScriptResourceRendered(context, new ResourceIdentifier("omnifaces", "omnifaces.js"))) {
- addScriptResourceToBody("omnifaces", "unload.js");
- }
-
+ 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() {
+ return !Hacks.isScriptResourceRendered(getContext(), new ResourceIdentifier("omnifaces", "omnifaces.js"));
+ }}));
addScriptToBody("OmniFaces.Unload.init('" + id + "')");
}
@@ -121,7 +121,7 @@ public int hashCode() {
@Override
public boolean isLiteralText() {
- return true;
+ return callbackReturning == null && callbackWithArgument == null;
}
/**

0 comments on commit 5216b57

Please sign in to comment.