BB Skeleton
by kyllle
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.2/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
JavaScript
console.clear();
var jsonData = {
"id": 1,
"title": "Data title 1"
}
var PollingModel = Backbone.Model.extend({
url: '/echo/json/',
polling: false,
interval: 3000,
startPolling: function() {
this.polling = true;
this.poll();
},
stopPolling: function() {
this.polling = false;
},
poll: function() {
this.fetch({
data: {
json: JSON.stringify(jsonData)
},
type: 'POST',
success: function(model, response, options) {
console.log('✓✓✓ SUCCESS::', model, response, options);
this.onFetch();
}.bind(this),
error: function() {
console.log('✖✖✖ ERROR::', model, response, options);
this.onFetchError();
}
});
},
onFetch: function() {
if (this.polling) {
setTimeout(_.bind(this.poll, this), this.interval);
}
},
onFetchError: function() {
// increment counter
}
});
var pollingModel = new PollingModel().startPolling();