2014-02-19 116 views
1

今天我決定將我的主要應用程序從包路由器遷移到鐵路由器。 我的應用程序能夠做動態模板渲染: 某些模板以編程方式呈現,因爲它取決於來自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

是的,在代碼的某些部分,我使用代碼之王並且它崩潰了 – Rebolon

+0

它似乎與路由器沒有關係。你能提供更多的關於你在哪裏調用它的上下文以及你正在使用的是什麼數據? –

回答

1

確定,這要歸功於@ChristianFritz我看起來更在模板,事實上,我發現,鐵路由器提供{{鏈接}}幫手。問題是我的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? 

但使用鐵路由器的時候,因爲他們的命名沒有使用任何類型的命名空間,將阻止那樣的問題,我們應該看看提供的助手。