gpt4 book ai didi

javascript - meteor :隐藏或删除元素?什么是最好的方法

转载 作者:数据小太阳 更新时间:2023-10-29 04:59:17 25 4
gpt4 key购买 nike

我对 Meteor 很陌生,但真的很喜欢它,这是我正在构建的第一个响应式应用程序。

我想知道一种方法可以在用户单击时删除 .main 元素,或者更好的方法是删除现有模板(包含主要内容),然后替换为另一个 meteor 模板?像这样的事情在 html/js 应用程序中会简单明了(用户点击 -> 从 dom 中删除 el)但在这里并不是那么清楚。

我只是想学习和了解最佳实践。

//gallery.html
<template name="gallery">
<div class="main">First run info.... Only on first visit should user see this info.</div>
<div id="gallery">
<img src="{{selectedPhoto.url}}">
</div>
</template>

//gallery.js
firstRun = true;

Template.gallery.events({
'click .main' : function(){
$(".main").fadeOut();
firstRun = false;
}
})

if (Meteor.isClient) {

function showSelectedPhoto(photo){
var container = $('#gallery');
container.fadeOut(1000, function(){
Session.set('selectedPhoto', photo);
Template.gallery.rendered = function(){
var $gallery = $(this.lastNode);
if(!firstRun){
$(".main").css({display:"none"});
console.log("not");
}
setTimeout(function(){
$gallery.fadeIn(1000);
}, 1000)
}
});
}

Deps.autorun(function(){
selectedPhoto = Photos.findOne({active : true});
showSelectedPhoto(selectedPhoto);
});

Meteor.setInterval(function(){
selectedPhoto = Session.get('selectedPhoto');

//some selections happen here for getting photos.

Photos.update({_id: selectedPhoto._id}, { $set: { active: false } });
Photos.update({_id: newPhoto._id}, { $set: { active: true } });
}, 10000 );
}

最佳答案

如果你想有条件地隐藏或显示一个元素,你应该使用 Meteor 的 react 行为:向你的模板添加一个条件:

<template name="gallery">
{{#if isFirstRun}}
<div class="main">First run info.... Only on first visit should user see this info.</div>
{{/if}}
<div id="gallery">
<img src="{{selectedPhoto.url}}">
</div>
</template>

然后向您的模板添加一个助手:

Template.gallery.isFirstRun = function(){
// because the Session variable will most probably be undefined the first time
return !Session.get("hasRun");
}

并更改点击操作:

Template.gallery.events({
'click .main' : function(){
$(".main").fadeOut();
Session.set("hasRun", true);
}
})

您仍然可以淡出该元素,但不是隐藏它或删除它并让它在下一个render 中返回,而是确保它永远不会返回。

渲染是通过改变 Session 变量来触发的,这是 react 性的。

关于javascript - meteor :隐藏或删除元素?什么是最好的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17755385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com