Skip to content

Commit

Permalink
Add a snapshot model and collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mjumbewu committed Aug 15, 2014
1 parent 9f79170 commit 2cca205
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,53 @@ var Shareabouts = Shareabouts || {};
comparator: 'created_datetime'
});

NS.SnapshotModel = Backbone.Model.extend({
sync: syncWithCredentials,

download: function(options) {
options = options || {};

var self = this,
delay = 50,
url = self.get('url') + '.' + (options.format || 'json');

var getFilename = function(url) {
var filename = url.substring(url.lastIndexOf('/')+1);
return filename;
};

var tryDownload = function() {
$.ajax({
type: 'HEAD',
url: url,
data: options.data,

success: function() {
var args = Array.prototype.splice.call(arguments, 0),
filename = getFilename(url);
if (options.success) { options.success.apply(this, [url, filename].concat(args)); }
},

error: function($xhr) {
if ($xhr.status === 503) {
if (delay < 5000) { delay *= 2; }
_.delay(tryDownload, delay);
} else {
if (options.error) { options.error.apply(this, arguments); }
}
}
});
};

tryDownload();
}
});

NS.SnapshotCollection = Backbone.Collection.extend({
url: '/api/places/snapshots',
model: NS.SnapshotModel
});

NS.PlaceModel = Backbone.Model.extend({
initialize: function() {
var attachmentData;
Expand Down

0 comments on commit 2cca205

Please sign in to comment.