Reload the dashboard page every 60 seconds and on visibility state change.

This commit is contained in:
Christopher Charbonneau Wells 2017-10-29 16:01:13 -04:00
parent 8168c74034
commit 22664ecc0f
2 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,36 @@
/* Baby Buddy Dashboard
*
* Provides a "watch" function to update the dashboard at one minute intervals
* and/or on visibility state changes.
*/
BabyBuddy.Dashboard = function ($) {
var runIntervalId = null;
var dashboardElement = null;
var lastUpdate = moment();
var Dashboard = {
watch: function(element_id) {
dashboardElement = $('#' + element_id);
if (dashboardElement.length == 0) {
console.error('Baby Buddy: Dashboard element not found.');
return false;
}
runIntervalId = setInterval(this.update, 60000);
Visibility.change(function (e, state) {
if (state == 'visible' && moment().diff(lastUpdate) > 60000) {
Dashboard.update();
}
});
},
update: function() {
// TODO: Someday maybe update in place?
location.reload();
}
};
return Dashboard;
}(jQuery);

View File

@ -10,7 +10,7 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div class="row align-items-start"> <div id="dashboard-child" class="row align-items-start">
<div class="col-lg-4 col-md-6 col-sm-12"> <div class="col-lg-4 col-md-6 col-sm-12">
{% card_feeding_last object %} {% card_feeding_last object %}
{% card_feeding_last_method object %} {% card_feeding_last_method object %}
@ -27,4 +27,10 @@
{% card_diaperchange_types object %} {% card_diaperchange_types object %}
</div> </div>
</div> </div>
{% endblock %}
{% block javascript %}
<script type="application/javascript">
BabyBuddy.Dashboard.watch('dashboard-child');
</script>
{% endblock %} {% endblock %}