Dynamic Resource Loading Feature

PrimeFaces 3.0.M4 will be introducing a new feature called “Dynamic Resource Loading” to solve a very common problem related to the resources of components that are added to the page dynamically. If you have tried to add a PrimeFaces component programmatically to a JSF page, it is likely that you’ve already faced with this. Consider the following to case where a button action adds a fileUpload component on the fly with ajax;

<p:outputPanel id="pnl" binding="#{bean.panel}" />

<p:commandButton update="pnl" actionListener="#{bean.createUI}" />
public void createUI() {
    FileUpload fu = FacesContext.getCurrentInstance().getApplication().createComponent(FileUpload.COMPONENT_TYPE);
    panel.getChildren().add(fu);
}

Problem here is, since fileupload resources like fileupload.css and fileupload.js do not exist on page load, we get PrimeFaces.widget.FileUpload not defined error. Note that this only happens in ajax updates for non-ajax updates page will work fine.

Also the same problem occurs on conditional ui:include that is updated with ajax;

<ui:include src="#{bean.pageToInclude}" />

Where bean.page can resolve to a.xhtml or b.xhtml, on page load if it resolves to a.xhtml and if b.xhtml has again a component like fileupload whose resources do not exist on page, we’ll have the same trouble. Although PrimeFaces M4 merges resources for common components, there are still some components like fileUpload, schedule, editor, timeline excluded from the merge.

With new “Dynamic Resource Loading” feature, PrimeFaces components are more intelligent than ever now, if their required resources do not exist on page, PrimeFaces first loads the necessary resources on the fly with ajax and then initializes the component. This is also implemented in a lightweight way, instead of updating the head tag, PrimeFaces just works with delta changes based on a convention. For example fileupload requires fileupload.css and fileupload.js so PrimeFaces just loads these resources.

3.0.M4 is due 31 of October including this enhancement and much more.

This entry was posted in PrimeFaces. Bookmark the permalink.

9 thoughts on “Dynamic Resource Loading Feature

  1. Great and this is something that every application needs. I hope and wish you guys are safe from the earth quake.

  2. I was just about to write you regarding this very issue…. keep up the outstanding work… you make us all look good and we are thankful.