Integrate JavaScript in JSF composite component, the clean way

前端 未结 1 1080
挽巷
挽巷 2020-11-27 06:59

In JSF, what would be the \"right\" and \"clean\" way to integrate JavaScript i.e. into a composite-compenent? I am a fan of Unobtrusive JavaScript, and separating HTML from

相关标签:
1条回答
  • 2020-11-27 07:17

    I'd say that what you've there is the best you can get, provided that you're absolutely positive that the HTML element's nature is so unique that you absolutely need to select it by an ID, every time again.

    If the HTML representation is not that unique (I can imagine that a Facelets template or include file might contain application-wide unique HTML elements like header, menu, footer, etc, but a composite component which can be reused multiple times in a single view? I can't for life imagine that), then you could also consider using an unique classname and hook initialization on it.

    E.g. /resources/composites/yourComposite.xhtml

    <cc:implementation>
        <h:outputScript library="composites" name="yourComposite.js" target="head" />
        <div id="#{cc.clientId}" class="your-composite">
            ...
        </div>
    </cc:implementation>
    

    with in the /resources/composites/yourComposite.js (assuming that you're using jQuery)

    var $yourComposites = $(".your-composite");
    // ...
    

    You can if necessary extract the autogenerated HTML element ID for each of them in a jQuery.each():

    $yourComposites.each(function(index, yourComposite) {
        var id = yourComposite.id;
        // ...
    });
    
    0 讨论(0)
提交回复
热议问题