2017-02-23 3 views
0

javascript 함수를 렌더링하는 사용자 지정 구성 요소를 작성하고 싶습니다. 기본적으로 onloadscript가 omnifaces로 작동하며, 자바 스크립트 함수의 매개 변수로 둘러싼 부모의 클라이언트 ID를 사용합니다.일부 자바 스크립트를 렌더링 할 때 부모의 ID를 사용하는 사용자 지정 구성 요소를 작성하는 방법은 무엇입니까?

예 :

<h:panelGroup id="panel"> 
    <my:component /> 
</h:panelGroup> 

나는 onloadscript 지침으로 omnifaces의 구현을 사용하여 이러한 사용자 정의 구성 요소를 쓰기 시작했습니다. 지금까지는 좋았지 만, 둘러싼 부모가 복합 요소 인 경우 내 맞춤 구성 요소의 실제 부모는 복합 요소가 아니라 insertChildren 태그의 부모입니다.

이것은 의미가 있지만, 물론 내가 원하는 행동이 아닙니다.

예 :

<my:composite id="panel"> 
    <my:component /> 
</my:composite> 

제의 부모의 클라이언트 ID : 합성이 구현에서 insertChildren 태그를 사용하는 경우는 성분 '패널'아니다.

질문 :

  • 는이 문제를 어떻게 해결할 수

      ?
    • 내 사용자 지정 구성 요소가 뷰 루트로 이동 한 실제 UI 구성 요소 대신 뷰 루트에 단일 리소스 구성 요소를 만드는 태그 처리기 일 수 있습니까?
  • 답변

    0

    마지막으로 해결책을 찾았습니다.

    태그 처리시 뷰 트리에 UI 구성 요소를 추가하는 태그 처리기를 구현했습니다. 아래 코드의 주석을 참조하십시오. 태그 핸들러 :

    private final TagAttribute someAttribute; 
    
    @Override 
    public void apply(FaceletContext ctx, UIComponent parent) { 
        UIComponent parentOfParent = parent != null ? parent.getParent() : null; 
    
        // process only when created 
        if (parent != null && parentOfParent == null) { 
         Supplier<String> scriptProvider = getScriptProvider(ctx, parent); 
         ScriptComponent scriptComponent = new ScriptComponent(scriptProvider); 
         parent.getChildren().add(scriptComponent); 
         // note: the actual script is obtained when the ScriptComponent is rendered 
        } 
    } 
    
    protected Supplier<String> getScriptProvider(FaceletContext ctx, UIComponent target) { 
        // this is invoked when the tag is processed, so it's Ok for the EL context 
        String someValue = someAttribute.getValue(ctx); 
    
        return() -> { 
         // this is invoked by the script component when it is rendered, 
         // so we have the correct client ID even if the target is a composite component 
         String targetId = target.getClientId(); 
         return String.format("myFunction('%s', '%s');", targetId, someValue); 
        }; 
    } 
    

    UI 컴포넌트 :

    private final Supplier<String> scriptProvider; 
    
    @Override 
    public void encodeBegin(FacesContext context) throws IOException { 
        ResponseWriter writer = context.getResponseWriter(); 
        writer.append("\n"); 
        writer.startElement("script", this); 
        writer.append("\n"); 
    
        String script = scriptProvider.get(); 
        writer.append(script); 
        writer.append("\n"); 
    } 
    
    @Override 
    public void encodeEnd(FacesContext context) throws IOException { 
        ResponseWriter writer = context.getResponseWriter(); 
        writer.endElement("script"); 
    } 
    
    관련 문제