简体   繁体   English

如何在页面刷新时保留查看数据

[英]How to retain View Data on page refresh

I am trying to use the backbone architecture in my MVC application. 我正在尝试在我的MVC应用程序中使用主干体系结构。 I created the templates using HandleBars/Mustache and my model contains information that I display in a table on the UI (within a div). 我使用HandleBars / Mustache创建了模板,并且我的模型包含在UI的一个表中(在div内)显示的信息。

I would like to reload the page after every 5 mins to refresh the data in the table. 我想每5分钟刷新一次页面以刷新表中的数据。 I can use <meta http-equiv="refresh" content="some value" /> , 我可以使用<meta http-equiv="refresh" content="some value" />

but it won't hold the view data in the table. 但它不会在表格中保存视图数据。 How can I refresh the page in this scenario? 在这种情况下如何刷新页面?

EDIT: Basically, on the page I have a search box and the search items are loaded on a table. 编辑:基本上,在页面上我有一个搜索框,并且搜索项加载在表上。 If I refresh the page using the meta tag, the page is reloaded and the data in the table is gone. 如果我使用meta标记刷新页面,则会重新加载页面,并且表中的数据也将消失。

I would like to reload the page after every 5 mins to refresh the data in the table. 我想每5分钟刷新一次页面以刷新表中的数据。

Not quite. 不完全的。 You want to reload the data every five minutes so that you can refresh the table in the page. 您希望每五分钟重新加载一次数据,以便刷新页面中的表。

You should use setTimeout somewhere, perhaps even in your view's initialize : 您应该在某个地方使用setTimeout ,甚至在视图的initialize

start_reloader: function() {
    var _this = this;
    this.timer = setTimeout(function() {
        $.ajax({
            // Load the data from your server...
            success: function(data) {
                _this.redraw_the_table(data);
                _this.start_reloader();
            }
        });
    }, 5*60*1000);
},

initialize: function() {
    this.start_reloader();
}

And then you'd want something in your remove to kill the timer: 然后,您希望remove某些内容以杀死计时器:

remove: function() {
    if(this.timer)
        clearTimeout(this.timer);
    return Backbone.View.prototype.remove.apply(this);
}

You could also use setInterval and clearInterval but there is a (small) chance that they can pile up on each other if there are delays in the AJAX calls. 您也可以使用setIntervalclearInterval但是如果AJAX调用存在延迟,则它们有可能彼此堆积(很小)。

You don't have to manage this in the view of course. 当然,您不必对此进行管理。 Setting up a model or collection to reload itself from the server every five minutes might make more sense in your case; 设置模型或集合以每五分钟从服务器重新加载一次可能更有意义。 then, your view would bind to the model's or collection's events as usual and redraw the table in response to 'reset' , 'change' , ... events from the model/collection. 然后,您的视图将照常绑定到模型或集合的事件,并响应来自模型/集合的'reset''change' ,...事件重新绘制表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM