我面临的问题是,向订阅的Collection添加数据不会自动刷新集合中显示的元素.如果我添加一个新元素,元素会显示一秒钟,然后消失!刷新浏览器(F5)并显示新元素.我将订阅放入Meteor.autorun,但事情保持不变.
lists.html(客户端):
<
{{#each lists}}
-
{{this.name}}
{{#if ownerOfList this._id}}
{{memberCount this._id}}
{{else}}
{{memberCount this._id}}
{{/if}}
{{/each}}
{{username}}
lists.js(客户端):
Template.lists.lists = function(){ return Lists.find(); }
Lists = new Meteor.Collection("lists");
Deps.autorun(function() {
Meteor.subscribe('lists');
})
lists.js
var activeListName = ""; var activeListID = ""; Template.lists.lists = function() { return Lists.find(); } Template.lists.memberCount = function(id) { var count = ""; Meteor.call("listMemberCount", id, function(error,result) { if (error) { console.log("List not initialized:" + error.reason); } else { Session.set("countMember_"+id,result); } }); return Session.get("countMember_"+id); } Template.lists.ownerOfList = function(id) { return ( Meteor.userId() == Lists.findOne({_id : id}).owner); } Template.lists.settings = function() { return { position: "top", limit: 5, rules: [ { token: '', collection: Meteor.users, field: "username", template: Template.userPill }] } } Template.lists.events({ 'submit #list-add-form' : function(e, t) { /* Checklisten ausgeben */ e.preventDefault(); var name = t.find('#list-name').value; var id = new Meteor.Collection.ObjectID().valueOf(); var id_block = new Meteor.Collection.ObjectID().valueOf(); Lists.insert({_id : id, name : name, owner : Meteor.userId()}); Meteor.users.update({_id : Meteor.userId()}, {$addToSet :{lists : id}}); Listitems.insert({_id : id_block, name : "", items: []}); Lists.update(id, {$addToSet : {items : id_block}}); }, 'click .clickOnList' : function(e) { /* Eventhandler fuer klick auf Checkliste */ Session.set("activeListId", e.target.id); $("#"+e.target.id).siblings('li').removeClass("active"); $("#"+e.target.id).addClass("active"); }, 'mouseover .clickOnList' : function (e,t) { $( ".deleteLists" ).each(function( index, item ) { if ( item.getAttribute("data-id") == e.target.getAttribute("data-id")) { item.style.visibility = 'visible'; } else { item.style.visibility = 'hidden'; } }); }, 'mouseleave .clickOnList' : function (e,t) { $( ".deleteLists" ).each(function( index, item ) { item.style.visibility = 'hidden'; }); }, 'click .deleteLists' : function(e, t) { /* Eventhandler zum loeschen einer Checkliste */ var id = e.target.getAttribute("data-id"); Meteor.call("removeList", id); console.log("test"); }, 'click .changeListnameButton' : function(e,t) { var id = e.target.getAttribute("data-id"); document.getElementById("changerForm_" + id).style.visibility = 'visible'; document.getElementById(id).style.visibility = 'hidden'; document.getElementById("changerText_" + id).focus(); }, 'dblclick .clickOnList' : function(e,t){ var id = e.target.getAttribute("data-id"); document.getElementById("changerForm_" + id).style.visibility = 'visible'; document.getElementById(id).style.visibility = 'hidden'; document.getElementById("changerText_" + id).focus(); }, 'submit .changeList-name-form' : function(e,t) { e.preventDefault(); var id = e.target.getAttribute("data-id"); var text = document.getElementById("changerText_" + id).value; if(text != '') { Meteor.call("changeListName", id, text); } if (Session.get("activeListId", e.target.id) == id ) { Session.set("activeListName", text); } document.getElementById("changerForm_" + id).style.visibility = 'hidden'; document.getElementById(id).style.visibility = 'visible'; }, 'blur .list_name' : function(e,t) { e.preventDefault(); var id = e.target.getAttribute("data-id"); var text = document.getElementById("changerText_" + id).value; if((text != '') && (document.getElementById(id).style.visibility == 'hidden')) { Meteor.call("changeListName", id, text); } if (Session.get("activeListId", e.target.id) == id ) { Session.set("activeListName", text); } document.getElementById("changerForm_" + id).style.visibility = 'hidden'; document.getElementById(id).style.visibility = 'visible'; }, 'click .userForListModal' : function(e,t) { e.preventDefault(); activeListName = e.target.id; activeListID = e.target.getAttribute("data-id"); //console.log(activeListID + " " + activeListName); //console.log("New user for Liste" + Lists.findOne({_id : activeListID}).name); userForList(activeListID); $("#userForListModalLabel").html("Benutzer der Liste '"+ activeListName+ "'"); }, 'submit #list-addUser-form' : function(e,t) { e.preventDefault(); var newUser = $('#name-list-addUser').val(); Meteor.call("addUserToList", newUser, activeListID, function(error,result) { if (error) { console.log(error.reason); } else { if (result == 1) { $('#userForListModalerrorMessage').fadeIn(1000, function() {$(this).delay(1000).fadeOut(1000);}); $('#userForListModalerrorMessage').html("Benutzer wurde nicht gefunden..."); } else if (result == 2) { $('#userForListModalerrorMessage').fadeIn(1000, function() {$(this).delay(1000).fadeOut(1000);}); $('#userForListModalerrorMessage').html("Benutzer ist Besitzer der Liste..."); } } }); } }); function userForList(id) { try { var owner = Lists.findOne({_id : id}).owner; var members = Lists.findOne({_id : id}).member; } catch(e){ } output = "
main.js(服务器):
Lists = new Meteor.Collection("lists");
Meteor.publish("lists", function(){
var ListsOfUser = Meteor.users.findOne({_id : this.userId}).lists;
return Lists.find({_id :{ $in : ListsOfUser}});
});
Lists.allow({
insert : function(userID, list)
{
return (userID && (list.owner === userID));
},
//todo
update : function(userID)
{
return true;
},
//todo
remove : function(userID)
{
return true;
}
});
提前致谢!