2015-01-30 3 views
2

전판을 살펴 보았지만 이에 대한 대답을 찾지 못했습니다.Handlebars 도우미에서 여러 인수 허용

내 목표는 전달 된 모든 인수가 true인지 확인하고, 그렇다면 내용을 표시하는 핸들 바 도우미를 만드는 것입니다. 예를 들어

:

{{#ifAll data.something data.somethingElse data.oneMore}} 
    Show me if all of these arguments exist! 
{{/ifAll}} 

이 내 가장 친한 찌르기 -하지만 그것은 내가 옵션 속성을 것입니다 마지막 속성을 믿고있어하는 것이 위험하다? 이 작업을 수행하는 더 좋은 방법이 있습니까?

Handlebars.registerHelper "ifAll", -> 
    options = arguments[arguments.length - 1] 
    for arg, i in arguments when i isnt arguments.length - 1 
     return options.inverse @ if !arg 
    options.fn @ 
+0

도우미 오히려 호출하지 않습니다'ifAll'이 아닌'ifAny' 당신이 그것을 wan't 이후가를 보여 : ifAny에 대한 귀하의 논리는 정말 이들 중 하나 여야합니다 ifAllifAny라는 도우미에 있어야합니다 모든 인수가있는 경우 내용? –

답변

1

AFAIK options은 항상 마지막 인수가됩니다. 도우미가 정확하게 문서화되거나 지정되지는 않았지만 마지막 인수가 항상 options이 될 것이라고 가정하는 것이 안전하다고 생각합니다.

Handlebars.registerHelper "ifAny", (conditions..., options)-> 
    for condition in conditions 
     return options.inverse @ if !condition 
    options.fn @ 

혹은 :

Handlebars.registerHelper "ifAny", (conditions..., options)-> 
    for condition in conditions 
     return options.inverse @ unless condition 
    options.fn @ 

또는 어쩌면 :

Handlebars.registerHelper "ifAny", (conditions..., options)-> 
    return options.inverse @ for condition in conditions when !condition 
    options.fn @ 

당신이 플랫 인수를 사용하여 커피 스크립트이 조금 청소기를 할 수 있다고 말했다

,

Kamil Szot에 포인트가 있습니다. 헬퍼 이름 (ifAny)이 사용중인 로직과 일치하지 않습니다.

Handlebars.registerHelper 'ifAny', (conditions..., options)-> 
    for condition in conditions 
     return options.fn @ if condition 
    options.inverse @ 

Handlebars.registerHelper 'ifAny', (conditions..., options)-> 
    return options.fn @ for condition in conditions when condition 
    options.inverse @ 
+1

'ifAll'이 아니라'ifAll'의 논리가 아닌가요? –

+0

@KamilSzot : 예, 당신 말이 맞습니다. 나는 도우미 이름조차 몰랐다. –

+0

@KamilSzot이 지적 해 주셔서 감사합니다. - 질문을 업데이트했습니다. – Ben