jeff
jeff

Reputation: 3742

JSF composite component with duplicate code, would like to parameterize

To reduce duplicate code amongst views with similar behaviors I've been utilizing composite component like the following:

employee_task_view.xhtml

<stk:task_table_component viewBean="#{myTaskViewImplementation}" id="taskComp" myself="true" actionColumn="false" showWaivedColumn="#{auth.userHasRole('adminStatuses')}" />

manager_roster_task_view.xhtml

<stk:task_table_component viewBean="#{managerTaskViewImplementation}" id="taskComp" myself="false" actionColumn="true" showWaivedColumn="#{auth.userHasRole('adminStatuses')}" />

With different view beans passed in. But now would like to reduce duplicate code within the composite component itself. For example I'd like to clean up the Create and Update dialog forms which are basically duplicates except for whether I am referencing the selected item from the datatable row or a New Item to be inserted.

task_table_component.xhtml

     <p:dialog id="addTaskDialogId" header="Add Task #{cc.attrs.viewBean.employeeToView ne auth.employee ? ' for '.concat(cc.attrs.viewBean.employeeToView.lname) : ''}" dynamic="true"
                widgetVar="addTaskDialogWv">
          <h:form id="addTaskFormId">
               <p:outputLabel id="taskTitleLbl" value="Task Title : " for="taskTitle" />
               <p:inputText id="taskTitle" value="#{cc.attrs.viewBean.newTask.title}" />
           ...
     </p:dialog>
     <p:dialog id="editTaskDialogId" header="Edit task #{cc.attrs.viewBean.selectedTask.id}" dynamic="true" widgetVar="editTaskDialogWv">
      <h:form id="editTaskFormId">
           <p:outputLabel id="taskTitleLbl" value="Task Title : " for="taskTitle" />
             <p:inputText id="taskTitle" value="#{cc.attrs.viewBean.selectedTask.title}" />
            ...
      </p:dialog>

How do I parameterize the cc.attrs.viewBean.newTask.title and cc.attrs.viewBean.selectedTask.title to reduce down to just one popup p:dialog? I find myself reformating one of the Dialog layouts to find that I forgot to make the same adjustment in the other Dialog.

Upvotes: 1

Views: 41

Answers (1)

jeff
jeff

Reputation: 3742

I ended up passing parameter in an <ui:include> of which object to work on and then the src contained all the common form labels and fields

task_table_component.xhtml

    <p:dialog id="addTaskDialogId" header="Add Task #{cc.attrs.viewBean.employeeToView ne auth.employee ? ' for '.concat(cc.attrs.viewBean.employeeToView.lname) : ''}" dynamic="true"
        widgetVar="addTaskDialogWv" modal="true">
        <h:form id="addTaskFormId">
            <ui:include src="/WEB-INF/templates/includes/task_insert_edit.xhtml">
                <ui:param name="task" value="#{cc.attrs.viewBean.newTask}" />
            </ui:include>
            <p:commandButton value="Add Task" update="addTaskFormId taskComp:taskForm messages growl" action="#{cc.attrs.viewBean.addTask()}" />
        </h:form>
    </p:dialog>

    <p:dialog id="editTaskDialogId" header="Edit task #{cc.attrs.viewBean.selectedTask.id}" dynamic="true" widgetVar="editTaskDialogWv" modal="true">
        <h:form id="editTaskFormId">
            <ui:include src="/WEB-INF/templates/includes/task_insert_edit.xhtml">
                <ui:param name="task" value="#{cc.attrs.viewBean.selectedTask}" />
            </ui:include>
            <p:commandButton value="Update" update="editTaskFormId taskComp:taskForm growl messages" action="#{cc.attrs.viewBean.updateTask()}" />

        </h:form>
    </p:dialog>

task_insert_edit.xhtml

    <p:outputLabel value="Task Title : " />
    <p:inputText value="#{task.title}" />

    <p:outputLabel value="Task Description : " />
    <p:inputTextarea value="#{task.description}" />

    <p:outputLabel value="My Priority : " rendered="#{cc.attrs.myself}" />
    <p:inputText value="#{task.employeePriority}" rendered="#{cc.attrs.myself}" />

    <p:outputLabel value="AssignerPriority : " rendered="#{not cc.attrs.myself}" />
    <p:inputText value="#{task.assignerPriority}" rendered="#{not cc.attrs.myself}" />


            ...

Upvotes: 2

Related Questions