1
\$\begingroup\$

I'm ashamed to ask such an elementary question but I tried to DRY up this template a couple ways and couldn't come up with anything I was satisfied with. Can you help?

  %ul.dropdown-menu(aria-labelledby="single-button" role="menu" uib-dropdown-menu)
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/edit") Edit
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/open_in_wizard") Create Document
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/preview" document-template-preview="true" id="documentTemplate.id") Preview
    %li(role="menuitem" ng-hide="documentTemplate.parent_id")
      %a(href=true ng-click="copy(documentTemplate)") Copy
    %li(role="menuitem")
      %a(href=true ng-click="delete(documentTemplate)") Delete
\$\endgroup\$
  • 1
    \$\begingroup\$ What templating language/framework is it written in/with? \$\endgroup\$ – Igor Soloydenko Nov 29 '17 at 7:46
1
\$\begingroup\$

I think this is solid code. All I can think of is a repeat pointing to an array in the controller. I do this on some of our web apps that have high-freq of changes to menu items.

I don't know if this would really be a good refactor otherwise as it just abstracts away more of the template but it definitely DRY's it up.

BTW I'm not familiar with that templating lib so I apologize if there are any problems with the syntax.

template

 %ul.dropdown-menu(aria-labelledby="single-button" role="menu" uib-dropdown-menu)
        %li(role="menuitem" ng-repeat="item in doc_dropdown")
          %a(
             href={{ ( item.is_path ) ? item.pathLocation : true }} 
             ng-click={{ item.clickTrigger }}
             ng-hide={{ ( item.can_hide ) ? "documentTemplate.parent_id" : "" }}
            ) 
                {{item.text}}

somewhere in the controller

doc_dropdown = [
    {
        is_path: true,
        pathLocation: "/legal-forms/admin/document_templates/{{ documentTemplate.id }}/edit", 
        text: "Edit", 
        click_trigger: null,
        can_hide: false 
    },{
        is_path: true,
        pathLocation: "/legal-forms/admin/document_templates/{{ documentTemplate.id }}/open_in_wizard", 
        text: "Create Document", 
        click_trigger: null,
        can_hide: false 
    },{
        is_path: true,
        pathLocation: "/legal-forms/admin/document_templates/{{ documentTemplate.id }}/preview", 
        text: "Preview", 
        click_trigger: null,
        can_hide: false 
    },{
        is_path: false,
        pathLocation: null, 
        text: "Copy", 
        click_trigger: "copy(documentTemplate)",
        can_hide: true  
    },{
        is_path: false,
        pathLocation: null, 
        text: "Delete", 
        click_trigger: "delete(documentTemplate)",
        can_hide: false 
    },
];
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.