Meteor:当页面刷新或其他页面位置时,如何保留dom操作

Meteor: How do you preserve dom manipulations when page refreshes or another page location?

本文关键字:何保留 保留 操作 dom 刷新 其他 Meteor 位置      更新时间:2023-09-26

我有一个无限滚动,可以添加新元素(链接),当我导航到链接并在浏览器中返回时,我会返回到结果的第一页,而不是之前滚动操作添加的扩展元素。

你做错了!需要用流星的方式。

您只需使用Deps.autorun()并更改页码Session.set('currentPage'); 即可增加客户端的文档数量

main.js

//when scrollbar reaches end of page, just change the 'currentPage' session variable to 'grow' the list template
    if ($(window).scrollTop() + $(window).height() == $(document).height()) {
var nowPage = Session.get('pageNumber');
Session.set('pageNumber', parseInt(nowPage) + 1);
e.stopImmediatePropagation();
}

客户端/订阅.js

Deps.autorun(function(){
  Meteor.subscribe('huge-list', Session.get('currentPage'); //whenever currentPage changes, so will your subscription if you set up your publish() on the server side;
});

server/publication.js

Meteor.publish('huge-list', function(page){ //when session changes on client, this changes
return Requests.find({}, {limit:page});
});

唯一的方法是保存滚动的状态,然后在Meteor渲染模板后重新应用DOM操作。

例如,类似这样的东西:

Template.my_template.rendered = function() {
    $('#my-scrolling-element').scroll(function(e) {scrollPosition = e.target.scrollTop()})
    if(scrollPosition) {
        $('#my-scrolling-element').scrollTo(scrollPosition);
    }
}