2015-12-25 56 views
0

我想用流星建立一个网站。这里只是我的html代码的一个例子。流星:无法显示我的文字

<head> 
    <title>version_0.0.4</title> 
</head> 

<body> 
    <h1>Welcome to Meteor!</h1> 
    {{> myTemplate}} 
</body> 

<template name="myTemplate"> 
    <p id="&firstContent">Test to show my first content</p> 
    <p id="&secondContent">Test to show my second content</p> 
    <button type="button" id="save">Save content</button> 
</template> 

现在,当有人按下保存内容我想用在我的数据库开始了“&”的牌子,工作正常保存所有的id的innerHTML。

/* ----- functions to include ----- */ 
function inputContent(collection, inhalt, alteID){ 
    var id = collection.insert({ 
     content: inhalt, 
     htmlId: alteID 
    }); 
    return id; 
}  

Template.myTemplate.events({ 
    'click #save': function() { 
     $(document).ready(function() { //Damit der Code erst ausgeführt wird, wenn der DOM geladen ist 
      var all = $('*[id^="&"]');  // Das "&" Zeichen ist der Identifier/ in all speichern aller elemente 
      console.log(all); 
       for (var i = 0; i < all.length; ++i) { 
        var item = all[i]; //Durch all iterieren 
        var idNeuesItem = inputContent(textList, item.innerHTML, item.id);//Enthält ID über Callback 
        console.log("Gespeichert :" + textList.findOne({_id: idNeuesItem}).content); 
       } 
      }); 
     } 
    }); 

我这样做的人,我此刻手动删除innerHTML的代码,这样我HTMT文件看起来像这样:

<template name="myTemplate"> 
<p id="&firstContent"></p> 
<p id="&secondContent"></p> 
<button type="button" id="save">Save content</button> 

现在我想一个possability显示内容在我的ID内,以便用户看起来像以前一样,但内容来自数据库。我已经尝试了不同的版本,但他们都没有工作。有人可以帮我吗?对不起,当这是一个nooby的问题,我很新流星... 感谢您的帮助

回答

0

您将需要编写一个辅助方法来发布您存储到您的数据库的JSON对象的某些属性。

此致会是这个样子(书面服务器端)

Template.myTemplate.helpers({ 
    item: function() { 
    return itemList.findOne({_id: idNeuesItem}); //retrieve item 
    } 
}); 

然后,你就必须将它放在模板排序是这样的:

<p id="&firstContent"> 
    {#with item} 
     {{content}} 
    {/with] 
    </p> 

了解更多关于使用流星和Spacebars这里: https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/