2011-12-14 47 views
2

我有knockout.js工作这样的一段代码:匿名模板knockoutJS

<div> 
    ... some other markup here 

    <div class="topicDetail" data-bind='template: { name: "topicTemplate", data: activeTopic}'> </div> 

</div> 

<script type="text/html" id="topicTemplate"> 
    <ul class="querylist" data-bind='template: {name: "queryTemplate", foreach: queries}'></ul> 
</script>           

<script type="text/html" id ="queryTemplate"> 
    <li class="query" data-bind="css: { active: selected()}, queryType: type"> 
     <span class="querylink" data-bind="click: select">{{= text}}</span> 
     <span data-bind="withdocs: positiveExamples"></span> 
     <span data-bind='person: searcher'>&nbsp;</span> 
     <span data-bind='time: time'></span> 
    </li> 
</script> 

withdocspersontime是我的自定义绑定。我想我应该更简洁地重写它使用匿名的模板是这样的:

<div class="topicDetail" data-bind="with: activeTopic"> 
    <ul class="querylist" data-bind="foreach: queries"> 
     <li class="query"> 
      <span class="querylink" data-bind="click: select">{{= text}}</span> 
      <span data-bind="withdocs: positiveExamples"></span> 
      <span data-bind='person: searcher'>&nbsp;</span> 
      <span data-bind='time: time'></span> 
     </li> 
    </ul> 
</div> 

但这种失败,出现错误:

Uncaught Error: Unable to parse binding attribute. 
Message: ReferenceError: queries is not defined; 
Attribute value: foreach: queries 

在淘汰赛1.2.1.debug.js的1226线。这是指UL数据绑定。

我创建了一个jsfiddle,它抽象出这个问题,但小提琴的作品。还有什么我应该关注的?

+1

这听起来像你有一个情况,至少最初是在'activeTopic'中有什么东西,但没有'queries'数组。 'activeTopic'如何初始化?它会有没有'查询'数组? –

+0

是的,匿名模板在KO 1.3中工作。你正在使用1.2。 –

回答

3

如果您正在使用淘汰赛1.2.1,你说:

...in line 1226 of knockout-1.2.1.debug.js. This refers to the UL data binding.

......那么这是你的问题。您需要使用版本1.3.0。有关更多详细信息,请参见http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/中的“控制流绑定”部分。

你的小提琴工作的原因是它使用最新版本的淘汰赛。

+0

谢谢,那真是太棒了! –