【发布时间】:2014-01-09 15:37:59
【问题描述】:
我试图让播放列表中的歌曲在用户每次输入所选歌曲时出现在屏幕上。我有以下操作将他们选择的歌曲插入数据库:
Template.search_bar.events({
'keypress #query' : function (evt,template) {
// template data, if any, is available in 'this'
if (evt.which === 13){
var url = template.find('#query').value;
$("#query").val('');
$('#playlist_container').animate({scrollTop: $('#playlist_container')[0].scrollHeight});
Template.list.search_get(url,0); //insert records into the database
}
}
});
Template.list.search_get 将记录插入数据库:
Meteor.call('update_record',Template.list.my_playlist_id, song, function(err,message){});
在服务器端,我使用以下格式将记录推送到我的数据库中:
update_record: function(sessID, songObj){
Links.update({sess: sessID}, {$push: {songs: {song_title: songObj["title"], videoId: songObj["video_id"], thumbnail: songObj["thumbnail"], index: songObj["index"]}}});
},
基本上我所有的记录都有以下格式:
{_id:,
sess:,
songs: [{song_title:,
videoId:,
thumbnail:,
index:},
{song_title:,
videoId:,
thumbnail:,
index:},...]
}
记录的歌曲字段内的歌曲对象数组。我想做的是每次用户点击搜索按钮时,让那首新歌出现在列表中。 我不确定渲染函数被调用了多少次,或者模板如何在 hmtl 中渲染数据库对象。目前我的列表有以下 html 模板:
<template name="list">
<div id="playlist_container">
<ul id="playlist">
{{#each my_playlist.songs}}
{{> track}}
{{/each}}
</ul>
</div>
我认为 my_playlist 应该在客户端上调用以下操作:
Template.list.my_playlist = function(){
console.log("myplaylist is called");
return Links.findOne({sess: Template.list.my_playlist_id});
}
它应该返回一个包含歌曲对象数组的对象,我在 #each my_playlist.songs 中对其进行迭代,它应该呈现以下每个轨道模板:
<template name="track">
<li id="{{index}}" class="list_element">
<div class="destroy"> </div>
<div class="element_style">{{song_title}}</div>
</li>
</template>
但是,在成功插入唱片后,我没有看到新歌名出现。关于我如何处理这个问题的任何建议?
【问题讨论】:
-
Template.list.my_playlist_id是如何设置的?你确定这个值真的在改变吗? -
我在meteor.starup() 上设置了my_playlist_id,并在设置后立即使用playlist_id 进行订阅。
标签: mongodb templates meteor meteorite