Last active
August 19, 2017 16:25
-
-
Save scott-w/ae963b7195b8a1a2f2ab2bfd99df95f2 to your computer and use it in GitHub Desktop.
Backend Poller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() })); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Model } from "backbone"; | |
export const UpdateModel = Model.extend({ | |
url: "/application/version/", | |
defaults: { | |
version: "0.0.0" | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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