Skip to content

Instantly share code, notes, and snippets.

@scott-w
Last active August 19, 2017 16:25
Show Gist options
  • Save scott-w/ae963b7195b8a1a2f2ab2bfd99df95f2 to your computer and use it in GitHub Desktop.
Save scott-w/ae963b7195b8a1a2f2ab2bfd99df95f2 to your computer and use it in GitHub Desktop.
Backend Poller
import { Application } from "backbone.marionette";
import { IndexView } from "./view";
import { UpdateModel } from "./model";
const App = Application.extend({
channelName: "global",
region: "#app-root",
onStart() {
this.showView(new IndexView({ model: new UpdateModel() }));
}
});
import { Model } from "backbone";
export const UpdateModel = Model.extend({
url: "/application/version/",
defaults: {
version: "0.0.0"
}
});
import _ from "lodash";
import root from "window-or-global";
import { View } from "backbone.marionette";
import Poller from "backbone-poller";
/**
* The View that updates your App.
*/
const UpdaterView = View.extend({
className: "alert alert-info",
template: _.template(
'A new update is available. <a class="alert-link" href="">Update Now</a>'
),
ui: {
refresh: ".alert-link"
},
triggers: {
"click @ui.refresh": "reload:app"
}
});
export const IndexView = View.extend({
regions: {
main: "#main",
notification: "#notification",
updater: "#updater"
},
childViewEvents: {
"reload:app": "reload"
},
modelEvents: {
"change:version": "showUpdate"
},
initialize() {
const THIRTY_MINUTES = 1000 * 60 * 30;
this.poller = Poller.get(this.model, { delay: THIRTY_MINUTES });
},
/**
* Start polling for updates.
*/
onRender() {
this.poller.start();
},
/**
* Show the update view when a new version is available.
*/
showUpdate() {
this.poller.stop();
this.showChildView("updater", new UpdaterView());
},
/**
* Reload the main app.
*/
reload() {
root.location.reload();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment