2012-10-19 1 views
0
<div id="metro-scrollbar" data-bind="foreach: data.sections , visible: data.sections.length > 0"> 
    <section data-bind="foreach: $data.tiles, visible: $data.tiles.length > 0, css: { 'last' : $parent.isLastSection($index)}"> 
     <div data-bind="attr : {'class' : $root.getClass($data,$parentContext.$index,$index)} "> 

      <div data-bind="attr : {'class' : $root.getAspectClass($data,$index)} "></div> 
      <div class="live-tile"> 
       <span class="tile-title" data-bind="text: title, css:{'big' : bigtitle}"></span> 
       <div> 
        <p>a</p> 
       </div> 
      </div> 
     </div> 
    </section> 
</div> 

위 바인딩이 있습니다. 내부 div 주위에 래퍼를 삽입하려면 일부 캐스에서 필요합니다.knockout.js 바인딩을 사용하여 래퍼 div를 삽입 할 수 있습니까? foreach

<div> 
    <section> 
     <-- if $myFunction() inject <div> --> 
     <div> 
      <div></div> // THIS IN HERE SHOULD BE THERE ALWAYS: 
      <div >  // THIS IN HERE SHOULD BE THERE ALWAYS: 
      </div>  // THIS IN HERE SHOULD BE THERE ALWAYS: 
     </div> 
     <-- if $myFunction() inject </div> --> 
    </section> 
</div> 

나는 그것이 바람직하다고 생각합니다.

+0

는 가능하면 내부 div에 대한 템플릿 및 템플릿을 생성 주입 된 div에 대해서. 주입 된 div에 대한 템플릿은 innerdiv에 대한 템플릿을 호출해야합니다. 그런 다음 myFunction을 기반으로 두 템플릿 중 하나가 사용됩니다. 가능한? –

+0

미안하지만, 당신이하려는 일이 분명하지 않습니다. 그래서 당신이하려고하는 것은'section' 엘리먼트의 자식을 감싸는 어떤 요소를 추가하는 것입니다. –

답변

2

s093294가 올바른 아이디어를 가졌습니다. 여기에 솔루션의 예 (그리고 jsFiddle)입니다 :

보기

<button data-bind="click: click">Toggle</button> 

<section data-bind="template: { name: templateName }"></section> 

<script type="text/html" id="outer-tmpl"> 
    <div data-bind="template: { name: 'inner-tmpl' }"> 
    </div> 
</script> 
<script type="text/html" id="inner-tmpl"> 
    <div>first inner div</div> 
    <div>second inner div</div> 
</script> 

모델

viewModel = { 
    toggle: new ko.observable(true), 
    templateName: function() { 
     return viewModel.toggle() ? 'outer-tmpl' : 'inner-tmpl'; 
    }, 
    click: function() { 
     viewModel.toggle(!viewModel.toggle()); 
    } 
} 
ko.applyBindings(viewModel); 
관련 문제