我正在制作一个webapp,使用image url在MongoDB中添加图像。但是当我使用userFilter
使用Session
时,它无法正常工作,并且任何与Session
相关的内容都无效。
我已经使用了命令“meteor add session”,但它再次无效
我的start.js
代码是:
Images=new Mongo.Collection("images");
if(Meteor.isClient) {
Session.set("imageLimit",8);
$(window).scroll(function(event){
console.log(new Date());
})
Template.images.helpers({
images:function(){
if(Session.get("userFilter")){
return Images.find({createBy:Session.get("userFilter")},{sort: {date:-1,rating:-1}});
} else {
return Images.find({},{sort:{date:-1,rating:-1}});
}
},
getUser:function(user_id){
var user=Meteor.users.findOne({_id:user_id});
if(user) {
return user.username;
} else {
return "none"
}
}
});
Template.body.helpers({
username:function(){
if(Meteor.user()) {
return Meteor.user().username;
// return Meteor.user().emails[0].address;
} else {
return "";
}
}
});
Template.images.events({
'click .js-image':function(event){
$(event.target).css("width","50px");
},
'click .btn-del':function(event){
var id=this._id;
$("#"+id).hide('slow',function(){
Images.remove({"_id":id});
});
},
'click .js-rate':function(event){
var rating=$(event.currentTarget).data("userrating");
console.log(rating);
var image_id=this.id;
Images.update({_id:image_id},{$set:{rating:rating}});
},
'click .js-form-show':function(event){
$("#add-form").modal('show');
},
'click .js-click-name':function(event){
Session.set("userFilter",this.createBy);
}
});
Template.img_text.events({
'submit .js-form':function(event){
var img_src,img_alt;
img_src=event.target.img_src.value;
img_alt=event.target.img_alt.value;
Images.insert({
img_src:img_src,
img_alt:img_alt,
date:new Date(),
createBy:Meteor.user()._id
});
return false;
},
});
}
我的HTML代码是:
<head>
<title>Meteor JS</title>
</head>
<body>
{{> img_text}}
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
{{> loginButtons}}
</div>
</nav>
<div class="container">
<h1>Welcome to mySite {{username}}!</h1>
{{> images}}
</div>
</body>
<template name="img_text">
<div class="modal fade" id="add-form">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title">
<h2>Image Form</h2>
</div>
</div>
<div class="modal-body">
<form class="js-form">
<input type="text" name="img_src">
<br/><input type="text" name="img_alt">
<button class="btn btn-success" >Submit</button>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-warning" data-dismiss="modal" >cancel</button>
</div>
</div>
</div>
</div>
</template>
<template name="images">
{{#if currentUser }}
<div id="btn">
<button class="btn btn-success js-form-show" >Add Image</button>
</div>
{{/if}}
<div class="row">
{{#each images}}
<div class="col-xs-12 col-md-3" id="{{_id}}">
<div class="thumbnail">
<img class="js-image" src="{{img_src}}" alt="{{img_alt}}"/>
<div class="caption">
<h3>Rating : {{rating}}</h3>
<p>{{img_alt}}</p>
<p>
User:
<a href="#" class="js-click-name">{{getUser createBy}}</a></p>
<p>
{{>starsRating mutable=true class="js-rate" id=_id}}
</p>
<button class="btn-del btn btn-warning">delete</button>
</div>
</div>
</div>
{{/each}}
</div>
</template>