流星铁路由器和动态模板渲染

时间:2014-02-19 20:55:30

标签: meteor

今天我决定将我的主应用程序从包路由器迁移到铁路由器。 我的应用程序能够进行动态模板渲染: 一些模板以编程方式呈现,因为它取决于Mongo的一些信息 路由器包很容易,但是使用铁路由器会遇到问题。

当我做的时候

Template.myTplName(data)

它总是返回

TypeError: Object #<Object> has no method 'fn'

要获取更多日志,我在控制台中执行了此操作:

try{
   Template.myTplName(data);
} catch(e) {
   console.trace();
}

我明白了:

You called Router.path for a route named undefined but that that route doesn't seem to exist. Are you sure you created it? 
console.trace() utils.js:169
Utils.warn utils.js:169
IronRouter.path router.js:178
(anonymous function) helpers.js:49
apply evaluate-handlebars.js:241
invoke evaluate-handlebars.js:266
(anonymous function) evaluate-handlebars.js:330
Spark.labelBranch spark.js:1124
branch evaluate-handlebars.js:320
(anonymous function) evaluate-handlebars.js:329
_.each._.forEach underscore.js:79
template evaluate-handlebars.js:323
Handlebars.evaluate evaluate-handlebars.js:377
(anonymous function) evaluate-handlebars.js:11
(anonymous function) deftemplate.js:157
Spark.isolate spark.js:859
(anonymous function) deftemplate.js:154
Spark.createLandmark spark.js:1163
(anonymous function) deftemplate.js:135
Spark.labelBranch spark.js:1124
partial deftemplate.js:134
(anonymous function) VM28316:2
InjectedScript._evaluateOn VM28186:603
InjectedScript._evaluateAndWrap VM28186:562
InjectedScript.evaluate VM28186:481
console.trace() VM28316:2
(anonymous function) VM28316:2
InjectedScript._evaluateOn VM28186:603
InjectedScript._evaluateAndWrap VM28186:562
InjectedScript.evaluate

所以实际上当我运行该命令时,铁路由器似乎试图处理它。但是这个模板没有在其堆栈中命名,因此可能存在问题。但我不想让路由器管理那部分。 也许我应该改变应用程序那部分的概念,但我不想这样做。

以下是父模板的代码:

<template name="resultsList">
   {{#if selectedSearch}}
     {{#if itemCount}}
     <div class="row">
       {{#each items}}
       <div id="item_{{_id}}">{{> myitem}}</div>
       {{/each}}
     </div>

     {{> loadMore}}
     {{/if}}
</template>

现在模板的代码:

<template name="myitem">
   {{#if currentUser}}
   <div class="btn-toolbar">
     <div class="btn-group" style="margin-left: 20px;">
       {{#if hasDetail}}
       <button class="btn btn-sm btn-success"><span class="glyphicon glyphicon-zoom-in"></span></
 button>
       {{else}}
       <!-- fake button that just take the place of the detail button -->
       <button class="btn btn-sm btn-link" style="cursor:default;text-decoration:none;margin-left:12px;"></button>
       {{/if}}
       <button {{#if starSelected}}disabled="disabled"{{/if}} class="btn btn-sm btn-info StarItem" title="A
 appeler"><span class="glyphicon glyphicon-phone"></span></button>
       <button {{#if visitedSelected}}disabled="disabled"{{/if}} class="btn btn-sm btn-info VisitItem" title="A
 visiter"><span class="glyphicon glyphicon-road"></span></button>
       <button {{#if removedSelected}}disabled="disabled"{{/if}} class="btn btn-sm btn-warning DelItem"
 title="Supprimer"><span class="glyphicon glyphicon-remove icon-white"></span></button>
     </div>
   </div>
   {{/if}}

   <div class="infos">
     {{{getItemInfo}}} <!-- This one will call the Template.myItem(data) -->
   </div>

   <div class="centerContentImage">
   {{#if image}}
     <a href="{{link}}" class="item item-{{status}}" target="_blank"><img src="/img/annonces/{{_id}}" /></a>
     {{#if notCurrentUserAndHasDetail}}<button class="btn btn-info Openitem"><span class="glyphicon glyphicon-
 zoom-in"></span></button>{{/if}}
   {{else}}
   {{else}}
     <a href="{{link}}" class="item item-{{status}}" target="_blank"><img src="/img/no_photo_icon.jpg" /></a>
     {{#if notCurrentUserAndHasDetail}}<button class="btn btn-info OpenDetailItem"><span class="glyphicon glyphicon-
 zoom-in"></span></button>{{/if}}
   {{/if}}
   </div>
 </template>

JS的代码:

那我怎么能这样做呢?

感谢名单

1 个答案:

答案 0 :(得分:1)

好的,感谢@ChristianFritz,我更多地看了一下模板,事实上我发现Iron-Router提供了一个{{link}}帮助器。问题是我的Json对象有一个名为'link'的属性,Iron-Router无法产生差异而且会产生错误。 错误不是真正明确的

You called Router.path for a route named undefined but that that route doesn't seem to exist. Are you sure you created it?

但是当使用Iron Router时,我们应该看看提供的帮助器,因为它们的命名没有使用任何类型的Namespace来阻止这种问题。