2015-03-29 21:10:53 +03:00
|
|
|
import Ember from 'ember';
|
2015-05-27 23:10:47 +03:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-05-27 23:10:47 +03:00
|
|
|
var DownloadCountPoller = Ember.Object.extend({
|
|
|
|
url: null,
|
2015-09-02 13:03:01 +03:00
|
|
|
count: '',
|
2015-05-27 23:10:47 +03:00
|
|
|
runId: null,
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-05-27 23:10:47 +03:00
|
|
|
init: function () {
|
|
|
|
this.downloadCounter();
|
|
|
|
this.poll();
|
|
|
|
},
|
|
|
|
|
|
|
|
poll: function () {
|
2015-09-02 13:03:01 +03:00
|
|
|
var interval = 2000,
|
2015-05-27 23:10:47 +03:00
|
|
|
runId;
|
|
|
|
|
|
|
|
runId = Ember.run.later(this, function () {
|
|
|
|
this.downloadCounter();
|
|
|
|
this.poll();
|
|
|
|
}, interval);
|
|
|
|
|
|
|
|
this.set('runId', runId);
|
|
|
|
},
|
|
|
|
|
|
|
|
downloadCounter: function () {
|
|
|
|
var self = this;
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-05-27 23:10:47 +03:00
|
|
|
ajax(this.get('url')).then(function (data) {
|
2015-08-25 16:48:02 +03:00
|
|
|
var count = data.count.toString(),
|
|
|
|
pattern = /(-?\d+)(\d{3})/;
|
|
|
|
|
|
|
|
while (pattern.test(count)) {
|
|
|
|
count = count.replace(pattern, '$1,$2');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.set('count', count);
|
2015-05-27 23:10:47 +03:00
|
|
|
}).catch(function () {
|
2015-09-02 13:03:01 +03:00
|
|
|
self.set('count', '');
|
2015-05-27 23:10:47 +03:00
|
|
|
});
|
2015-03-29 21:10:53 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-05-27 23:10:47 +03:00
|
|
|
export default Ember.Route.extend({
|
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
|
|
|
|
|
|
model: function () {
|
|
|
|
return DownloadCountPoller.create({url: this.get('ghostPaths.count')});
|
|
|
|
},
|
|
|
|
|
|
|
|
resetController: function (controller, isExiting) {
|
|
|
|
if (isExiting) {
|
|
|
|
Ember.run.cancel(controller.get('model.runId'));
|
|
|
|
controller.set('model', null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|